473,387 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

GZipStream and DeflateStream Different Compressed Sizes

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
1 10174
Plater
7,872 Expert 4TB
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

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

Similar topics

10
by: Asaf | last post by:
Hi, I am trying to Compress & Decompress a DataSet. When running this code I am receiving the error: System.Xml.XmlException was unhandled Message="Root element is missing."...
0
by: Craig Buchanan | last post by:
when i try to open a file that has been compressed with GZipStream (source from microsoft's 101 Samples for Visual Studio 2005), i get an "invalid archive" message from winzip, zipgenius and xp's...
1
by: KJ | last post by:
I am using the System.Compression.GZipStream class, and I noticed that in certain cases, the resultant compressed file is actually larger than the original. This is almost a constant when...
2
by: Flpit | last post by:
Hi, Can anybody tell me how to use the System.IO.Compression Deflatestream Class. I have a byte which is a compressed set of bytes that form a block. I want to inflate the byte and return...
1
by: bthetford | last post by:
I am trying to roll a simple compression scheme for network communication in an app I am developing. On the sender side, I compress each block of a set amount of bytes individually, then send it...
1
by: prince.matt | last post by:
Hi, I am using GZipStream to compress a simple text file approx 24MB in size. I am finding that the destination "compressed" file is several MB larger than the source file (31MB). The code is...
1
by: hakan.thornqvist | last post by:
I am using GZipStream for compression, and the size and contents of the compressed byte array differs from time to time, using the same source. byte uncompressed =...
4
by: Arnie | last post by:
Folks, The GZipStream class in .NET is throwing an exception during an OS unseal/power up (first boot experience). The .NET system is fully up as other apps seem to run fine.
5
by: DR | last post by:
Why is its substantialy slower to load 50GB of gzipped file (20GB gzipped file) then loading 50GB unzipped data? im using System.IO.Compression.GZipStream and its not maxing out the cpu while...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.