未压缩的文件比 GZIP 中的原始文件大

uncompressed file is bigger than original file in GZIP

我正在使用以下函数进行压缩(感谢 http://www.dotnetperls.com/):

public static void CompressStringToFile(string fileName, string value)
    {
        // A.
        // Write string to temporary file.
        string temp = Path.GetTempFileName();
        File.WriteAllText(temp, value);

        // B.
        // Read file into byte array buffer.
        byte[] b;
        using (FileStream f = new FileStream(temp, FileMode.Open))
        {
            b = new byte[f.Length];
            f.Read(b, 0, (int)f.Length);
        }

        // C.
        // Use GZipStream to write compressed bytes to target file.
        using (FileStream f2 = new FileStream(fileName, FileMode.Create))
        using (GZipStream gz = new GZipStream(f2, CompressionMode.Compress, false))
        {
            gz.Write(b, 0, b.Length);
        }
    }

解压缩:

static byte[] Decompress(byte[] gzip)
    {
        // Create a GZIP stream with decompression mode.
        // ... Then create a buffer and write into while reading from the GZIP stream.
        using (GZipStream stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress))
        {
            const int size = 4096;
            byte[] buffer = new byte[size];
            using (MemoryStream memory = new MemoryStream())
            {
                int count = 0;
                do
                {
                    count = stream.Read(buffer, 0, size);
                    if (count > 0)
                    {
                        memory.Write(buffer, 0, count);
                    }
                }
                while (count > 0);
                return memory.ToArray();
            }
        }
    }

所以我的目标实际上是压缩日志文件,而不是在内存中解压缩它们并将未压缩的文件与原始文件进行比较,以检查压缩是否成功,并且我能够成功打开压缩文件。 问题是未压缩的文件大部分时间都比原始文件大,虽然压缩可能成功,但我的比较检查失败了。

知道为什么吗?

顺便说一下,我如何比较未压缩的文件和原始文件:

static bool FileEquals(byte[] file1, byte[] file2)
    {
        if (file1.Length == file2.Length)
        {
            for (int i = 0; i < file1.Length; i++)
            {
                if (file1[i] != file2[i])
                {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

试试这个压缩文件的方法:

public static byte[] Compress(byte[] raw)
{
using (MemoryStream memory = new MemoryStream())
{
    using (GZipStream gzip = new GZipStream(memory,
    CompressionMode.Compress, true))
    {
    gzip.Write(raw, 0, raw.Length);
    }
    return memory.ToArray();
   }
  }
}

解压缩:

 static byte[] Decompress(byte[] gzip)
{
// Create a GZIP stream with decompression mode.
// ... Then create a buffer and write into while reading from the GZIP stream.
using (GZipStream stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress))
{
    const int size = 4096;
    byte[] buffer = new byte[size];
    using (MemoryStream memory = new MemoryStream())
    {
    int count = 0;
    do
    {
        count = stream.Read(buffer, 0, size);
        if (count > 0)
        {
        memory.Write(buffer, 0, count);
        }
    }
    while (count > 0);
    return memory.ToArray();
    }
}
}

}

告诉我它是否有效。

祝你好运。

你认为最简单的 API 调用会更好,试试 Stream.CopyTo()。我在你的代码中找不到错误。如果我正在处理它,我可能会确保一切都被正确刷新。不记得 GZipStream 是否会在 using 块关闭时将其输出刷新到 FileStream ..但是你也说最后文件更大,而不是更小。

无论如何,根据我的经验,最好的策略是.. 不要在不需要时重写容易出错的代码。至少你测试过它 ;)