Connect with Expertise | Find Experts, Get Answers, Share Insights

GZipStream and DeflateStream Different Compressed Sizes

 
Join Date: Jul 2007
Location: Waukesha, WI
Posts: 1
#1: Jul 18 '07
I'm using .NET 2.0.50727 in VS 2005 Pro, ENU Service Pack 1 (KB926601). I've been experimenting with the System.IO.Compression classes GZipStream and DeflateStream, and I found it interesting to note that they'll create different-sized compressed files depending on the procedure you use to take in the source file's data. With one, almost every file I feed them for compression ends up significantly bigger. That code is below, almost verbatim per MS's MCTS Exam 70-536 self-paced training kit. Below it are some sample size results per file type.
Expand|Select|Wrap|Line Numbers
  1.         static void CompressFile(string toCompress, string toBeCompressed)
  2.         {
  3.             FileStream srcFile = File.OpenRead(toCompress);
  4.             FileStream destFile = File.Create(toBeCompressed);
  5.             GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress);
  6.             int lilByte = srcFile.ReadByte();
  7.             while (lilByte != -1)
  8.             {
  9.                 compStream.WriteByte((byte)lilByte);
  10.                 lilByte = srcFile.ReadByte();
  11.             }
  12.  
  13.             compStream.Close();
  14.             srcFile.Close();
  15.             destFile.Close();
  16.  
  17.             //Sample Results, GZipStream/DeflateStream:
  18.             //Text document: 1.98K --> 1.79K/1.77K
  19.             //Word document: 140K  --> 227K
  20.             //Bitmap:        123K  --> 168K
  21.             //PDF document:  248K  --> 347K/346K
  22.         }
  23.  
This code, however, performed much better:
Expand|Select|Wrap|Line Numbers
  1.         static void CompressBetter(string toCompress, string toBeCompressed)
  2.         {
  3.             byte[] fileBytes = File.ReadAllBytes(toCompress);
  4.             FileStream destFile = File.Create(toBeCompressed);
  5.             GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress);
  6.             compStream.Write(fileBytes, 0, fileBytes.Length);
  7.  
  8.             compStream.Close();
  9.             destFile.Close();
  10.  
  11.             //Sample Results, both:
  12.             //Text document: 1.98K --> 1.28K
  13.             //Word document: 140K  --> 21.6K
  14.             //Bitmap:        123K  --> 20.6K
  15.             //PDF document:  248K  --> 301K (Well, I guess they can't all work)
  16.         }
  17.  
Perhaps these classes like having more to work with up front? Anyone else have any opinions/observations?

Plater's Avatar
E
M
C
 
Join Date: Apr 2007
Location: New England
Posts: 7,463
#2: Jul 18 '07

re: GZipStream and DeflateStream Different Compressed Sizes


Open the zip file in some program (like winzip or pkzip) and see what it says for compression values?

This like text files and bitmaps compress very well, PDF and other already-compressed data type don't compress very well.
Reply

Tags
gzipstream deflatestream