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. -
static void CompressFile(string toCompress, string toBeCompressed)
-
{
-
FileStream srcFile = File.OpenRead(toCompress);
-
FileStream destFile = File.Create(toBeCompressed);
-
GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress);
-
int lilByte = srcFile.ReadByte();
-
while (lilByte != -1)
-
{
-
compStream.WriteByte((byte)lilByte);
-
lilByte = srcFile.ReadByte();
-
}
-
-
compStream.Close();
-
srcFile.Close();
-
destFile.Close();
-
-
//Sample Results, GZipStream/DeflateStream:
-
//Text document: 1.98K --> 1.79K/1.77K
-
//Word document: 140K --> 227K
-
//Bitmap: 123K --> 168K
-
//PDF document: 248K --> 347K/346K
-
}
-
This code, however, performed much better: -
static void CompressBetter(string toCompress, string toBeCompressed)
-
{
-
byte[] fileBytes = File.ReadAllBytes(toCompress);
-
FileStream destFile = File.Create(toBeCompressed);
-
GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress);
-
compStream.Write(fileBytes, 0, fileBytes.Length);
-
-
compStream.Close();
-
destFile.Close();
-
-
//Sample Results, both:
-
//Text document: 1.98K --> 1.28K
-
//Word document: 140K --> 21.6K
-
//Bitmap: 123K --> 20.6K
-
//PDF document: 248K --> 301K (Well, I guess they can't all work)
-
}
-
Perhaps these classes like having more to work with up front? Anyone else have any opinions/observations?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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:
|
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
|
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...
|
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.
|
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...
| |
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)
|
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();
|
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.
|
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!
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |