By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,678 Members | 2,377 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,678 IT Pros & Developers. It's quick & easy.

GZipStream and DeflateStream Different Compressed Sizes

P: 1
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?
Jul 18 '07 #1
Share this Question
Share on Google+
1 Reply


Plater
Expert 5K+
P: 7,872
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.
Jul 18 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.