Can GZip compression (via .net) increase file size?

隐身守侯 提交于 2019-12-10 19:28:46

问题


I keep track of the original size of the files that I'm compressing using .Net's GZipStream class, and it seems like the file that I thought I was compressing has increased in size. Is that possible?

This is how I'm doing the compression:

Byte[] bytes = GetFileBytes(file);

using (FileStream fileStream = new FileStream("Zipped.gz", FileMode.Create))
{
    using (GZipStream zipStream = new GZipStream(fileStream, CompressionMode.Compress))
    {
        zipStream.Write(bytes, 0, bytes.Length);
    }
}

回答1:


Yes, it can. It has been fixed in .NET 4.

The compression algorithms for the System.IO.Compression..::.DeflateStream and System.IO.Compression..::.GZipStream classes have improved so that data that is already compressed is no longer inflated. This results in much better compression ratios. Also, the 4-gigabyte size restriction for compressing streams has been removed.

Check: GZipStream/DeflateStream increase file size on compression

Also check here SO: GZipStream and DeflateStream produce bigger files




回答2:


Another reason could be that you include .git folder when you compress, which inflates as you develop and use git.

For me, excluding .git solved the problem.



来源:https://stackoverflow.com/questions/3973485/can-gzip-compression-via-net-increase-file-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!