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

时间:2015-08-31 13:22:22

标签: c# .net compression 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;
    }

2 个答案:

答案 0 :(得分:1)

尝试使用此方法压缩文件:

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();
    }
}
}

}

告诉我它是否有效。

古德勒克。

答案 1 :(得分:1)

认为使用最简单的API调用会更好,请尝试Stream.CopyTo()。我在代码中找不到错误。如果我正在处理它,我可能会确保所有内容都正常刷新..无法回想起GZipStream是否会在使用块关闭时将其输出刷新到FileStream ...但是你也说了最后文件更大,而不是更小。

无论如何,根据我的经验,最好的政策是......当你不需要时,不要重写易陷码。至少你测试了它;)

相关问题