473,614 Members | 2,089 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

GZipStream and DeflateStream Different Compressed Sizes

1 New Member
I'm using .NET 2.0.50727 in VS 2005 Pro, ENU Service Pack 1 (KB926601). I've been experimenting with the System.IO.Compr ession 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 10196
Plater
7,872 Recognized Expert Expert
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
17835
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." Source="System.Xml" The code:
0
2186
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 compressed folder app. using DeflateStream resulted in the same error. am i missing something simple? thanks, Craig Buchanan
1
3299
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 compressing already-compressed formats such as jpeg. Another less-consistent example of this behavior is found in the case of compressing TIFFs, wherein some files end up greatly compressed (the larger ones up to 93% compression ratio) and others end up...
2
3580
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 the new inflated byte.
1
2330
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 over the NetworkStream along with the size of the original data, the size of the compressed data, and a byte telling the receiving end whether to expect more. On the receiving end, these blocks are received along with that size data and buffers...
1
2115
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 as follows: Dim sourcefile As FileStream = File.OpenRead(inFileName) Dim destFile As FileStream = File.Open(outFilename, FileMode.Create, FileAccess.Write)
1
3626
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 = Enc.GetBytes(xmlNode.OuterXml); byte compressed = compress(uncompressed); private byte compress(byte uncompressed) { MemoryStream ms = new MemoryStream();
4
3450
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
2933
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 loading the gzip data! Im using the default buffer of the stream that i open on the 20GB gzipped file and pass it into the GZipStream ctor. then System.IO.Compression.GZipStream takes an hour! when just loading 50GB file of data takes a few minutes!
0
8182
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8130
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8627
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8579
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8279
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7093
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4052
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1425
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.