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();
}
}
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.
I guess the problem is with
GZipLib
here. I usedDotNetZip
instead and file gets smaller as expected now. It can be downloaded here.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.