compressing pdf file makes it bigger

2019-08-29 06:01发布

I compress the uploaded .pdf files and save them to server's file system. Everything works well, but the file gets bigger, from 30kb to 48kb. What could I be doing wrong? Here's the code part I compress the uploaded file:

FileStream sourceFile = System.IO.File.OpenRead(filePath);
FileStream destFile = System.IO.File.Create(zipPath);
GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress);

try
{
    int theByte = sourceFile.ReadByte();
    while (theByte != -1)
    {
        compStream.WriteByte((byte)theByte);
        theByte = sourceFile.ReadByte();
    }
}

3条回答
Rolldiameter
2楼-- · 2019-08-29 06:33

What could I be doing wrong?

The PDF file standard already provides compression of different parts. So what you get is probably an already compressed file. And you know what happens when you try to compress a compressed file? It's the same as if you were trying to compress a ZIP file: kinda useless effort.

查看更多
Luminary・发光体
3楼-- · 2019-08-29 06:48

I guess the problem is with GZipLib here. I used DotNetZip instead and file gets smaller as expected now. It can be downloaded here.

查看更多
三岁会撩人
4楼-- · 2019-08-29 06:53

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.

It has been fixed in .NET 4.

查看更多
登录 后发表回答