473,667 Members | 2,530 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Built in compression on .net looses data, maybe?

Thanks to anyone who reads this.

Below is some C# that compresses an array of bytes, and then
decompresses, and compares the original data with the new.

Firstly, the length of the decompressed data is shorter than the
original. So some loss of data has occured. But the content up until
the early truncation matches. So am I flushing correctly? This error
only occurs for particular combinations of bytes in the original
buffer.

Secondly, when I read the decompressed data from the zip-stream, the
first read returns zero bytes. After that I perform a second read and
the data can be read. Why is that?

using System;
using System.Collecti ons.Generic;
using System.Diagnost ics;
using System.IO;
using System.IO.Compr ession;
using System.Text;

namespace lab02
{

class Program
{

static void Main(string[] args)
{

// declaration of local variables
int i1 = 0;

// create a buffer of data for compressing
byte[] bufferData = new byte[10];
for (i1 = 0; i1 < 10; i1++)
bufferData[i1] = Convert.ToByte( i1);

// PART 1 - Compression
byte[] bufferCompresse d = null;
{

// compress buffer into a memory stream (ms)
MemoryStream msCompressed = new MemoryStream();
DeflateStream zipStream = new DeflateStream(m sCompressed,
CompressionMode .Compress);
zipStream.Write (bufferData, 0, bufferData.Leng th);
msCompressed.Fl ush();

// get the compressed memory stream into a buffer
bufferCompresse d = new byte[msCompressed.Le ngth];
msCompressed.Po sition = 0;
msCompressed.Re ad(bufferCompre ssed, 0, bufferCompresse d.Length);

// close zip stream
zipStream.Close ();

}

// PART 2 - Decompression
byte[] bufferDecompres sed = null;
{

// put the compressed data (bufferCompress ed) into a memory stream
(msCompressed)
MemoryStream msCompressed = new MemoryStream();
msCompressed.Wr ite(bufferCompr essed, 0, bufferCompresse d.Length);
msCompressed.Po sition = 0;

// decompress buffer
DeflateStream zipStream = new DeflateStream(m sCompressed,
CompressionMode .Decompress);
msCompressed.Fl ush();

// read the de-compressed data into a buffer
MemoryStream msDecompressed = new MemoryStream();
int iBytesRead = 0;
byte[] bufferSub = new Byte[1024];
do
{

// read next bytes (problem with the first read, always read zero
first)
iBytesRead = zipStream.Read( bufferSub, 0, bufferSub.Lengt h);
if ((msDecompresse d.Length == 0) && (iBytesRead == 0))
iBytesRead = zipStream.Read( bufferSub, 0, bufferSub.Lengt h);

// if some data was read...
if (iBytesRead 0)
{

// add to stream
msDecompressed. Write(bufferSub , 0, iBytesRead);

}

} while (iBytesRead == bufferSub.Lengt h);

// close zip stream
zipStream.Close ();

// load buffer with unzipped data
bufferDecompres sed = new byte[msDecompressed. Length];
msDecompressed. Position = 0;
msDecompressed. Read(bufferDeco mpressed, 0,
bufferDecompres sed.Length);

}

// PART 3 - Comparision of what was and now is, or is it?
if(bufferData.L ength!=bufferDe compressed.Leng th)
Trace.TraceInfo rmation("Length mismatch!!!");
else
{

// compare contents
bool bMatch = true;
for (i1 = 0; i1 < bufferData.Leng th; i1++)
{

// if bytes do not match...
if (bufferData[i1] != bufferDecompres sed[i1])
{

// update flag
bMatch = false;

// break out of loop
break;

}

}
if(!bMatch)
Trace.TraceInfo rmation("Conten t does not match!!!");

}

}

}

}

Aug 29 '06 #1
2 2485
First - you are flushing the wrong stream; it is zipStream that needs
flushing. However, I have seen the compression classes refuse to Flush
completely until Close is called; presumably this is an optimisation to keep
a few bytes for use in the compression algorithm. So My compression code
would be (note overload to ctor to leave the stream open):

using (MemoryStream msCompressed = new MemoryStream()) {
using (DeflateStream zipStream = new
DeflateStream(m sCompressed, CompressionMode .Compress, true)) {
zipStream.Write (bufferData, 0, bufferData.Leng th);
zipStream.Close ();
}
bufferCompresse d = msCompressed.To Array();
}

I don't know why your code reports zero (I didn't even run it to find out,
I'm afraid) - however, I would do as follows; note the trick is on the while
condition (which captures the count and tests it in one go).

using (MemoryStream msDecompressed = new MemoryStream()) {
using (MemoryStream msCompressed = new
MemoryStream(bu fferCompressed) )
using (DeflateStream zipStream = new
DeflateStream(m sCompressed, CompressionMode .Decompress)) {
int bytesRead;
const int BUFFER_SIZE = 1024;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = zipStream.Read( buffer, 0,
BUFFER_SIZE)) 0) {
msDecompressed. Write(buffer, 0, bytesRead);
}
}
bufferDecompres sed = msDecompressed. ToArray();
}

Third - in this scenario, since I used the ctor to not close the stream
(first block of code) I could actually re-use my MemoryStream between the
two blocks simply by rewinding (.Position = 0); but the above works fine,
and illustrates the point. Also note that short or fairly random blocks of
data can get longer during compression. Ironic, but life, especially with
single-pass compression. Double-pass compression can use a "don't bother"
flag.

Marc
Aug 29 '06 #2
Thanks for the help Marc. Especially the

Stream.ToArray( );

function, which make the code a lot more compact and readable.

I have discovered from another group that the zip stream must be
flushed AND closed before the compressed data can be read.

zipStream.Flush ();
zipStream.Close ();

Solved!

Terry.

Aug 29 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
409
by: Jim Hubbard | last post by:
I went to the compression newsgroups, but all I saw was spam. So, I thought I'd post his question here to get the best info I could from other programmers. Which compression algorithm offers the fastest compression of text data? Which compression algorithm offers the best compression of text data? I need to do in memory compression of text data before sending it over a TCP connection. Zip is the old standby....but are there better...
8
2477
by: Anurag | last post by:
Hi, I am told that Oracle has this "data compression" feature that allows you to store online data ina compressed format. This is different from archived data - you compress only that data which is infrequently accessed but is still available in the online database. I am interested in finding out an equivalent feature in DB2 UDB on LUW. If there exists one, how should I go about implementing this? If there is an alternative strategy to...
10
1319
by: Just D. | last post by:
Who knows beginning from which version of IIS, 5 or 6 only, and what system like 2000 Server, 2000 Advanced Server, we can use the built-in compression? I heard once that it was implemented beginning from Windows 2000 Server and we could use it but I'm not sure that it's true. Right now we're having Win2000AdvServer and want to get this feature. But according to this article we can't use it, but only from IIS 6.0 ...
16
3416
by: Claudio Grondi | last post by:
What started as a simple test if it is better to load uncompressed data directly from the harddisk or load compressed data and uncompress it (Windows XP SP 2, Pentium4 3.0 GHz system with 3 GByte RAM) seems to show that none of the in Python available compression libraries really works for large sized (i.e. 500 MByte) strings. Test the provided code and see yourself.
3
1558
by: Benny Ng | last post by:
Dear All, Now I met some performance problems in my application. Because according to our business. The size of some web forms are larger than 1xxx MB. So it takes a long time for user opening a web page. Surely we are modifying some source code for performance improvment. But now i'm thinking should we improve the application performance by IIS Compression? I saw many mentions from Google/Yahoo. Some people said it can be useful for...
20
2921
by: chance | last post by:
Hello, I want to add compression to a memory stream and save it in an Oracle database. This is the code I have so far: //save the Word document to a binary field, MemoryStream dataStream = new MemoryStream(); doc.Save(dataStream, SaveFormat.Doc); //now compress it GZipStream compressedZipStream = new GZipStream(dataStream,
6
2371
by: mike_dba | last post by:
I have been testing compression for update operations. Can anyone tell me why I require more log for an update of a compressed table than I do for the same table that is not compressed ? I tried an update for the same number of rows for two copies of a table, one compressed and one not. The compressed UOW exceeds my log allocation while the non-compressed does not. Thanks
14
1449
by: Thomas Mlynarczyk | last post by:
Hello, I have a script that generates XML files which are sent to a client app on request via "new SimpleXMLElement( $sUrl, 0, true )". Both client and server run PHP 5.2 and the XML file can get as large as about 150 KB. (The client then generates an HTML page using the data in the XML file and sends it to the browser. So there are 3 parties involved: User requests HTML page - server requests XML data from other server - other server...
3
7511
by: GiJeet | last post by:
Hello, we have an app that scans documents into TIFF format and we need to transfer them over the internet. If anyone knows of a SDK we can use that can compress TIFFs on the fly or even if it can compress them so they take up less space on the server, would be appreciated. Actually any info on handling tiff files programatically would be appreciated as I know very little about tiffs. TIA
0
8457
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
8365
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
8883
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
8788
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
8563
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
7390
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...
1
6203
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
2013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1778
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.