473,326 Members | 2,182 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,326 software developers and data experts.

DeflateStream question

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 are created of exactly the required sizes.
The problem comes during decompression.
In the decompression routine, I do:

....
while(true)
{

if ( rdr.ReadByte() == 0 )
break;
int compressedSize = rdr.ReadInt32( );
byte[ ] compressedBytes = rdr.ReadBytes( compressedSize );
int decompressedSize = rdr.ReadInt32( );
byte[ ] decompressedBytes = new byte[ decompressedSize ];
MS.SetLength( 0 );
MS.Write( compressedBytes, 0, compressedSize );
MS.Position = 0;
int thisRead = DS.Read( decompressedBytes, 0, decompressedSize );
writer.Write( decompressedBytes, 0, thisRead );
MS.Position = 0;
}

All this ends up doing is making a blank file. thisRead always ends
up being 0, which means nothing gets written to the file.
It wouldn't matter, though, because the decompressedBytes array is a
bunch of zeros anyway after the call to DS.Read.

Just for reference, MS is my MemoryStream, DS is the DeflateStream,
rdr is a BinaryReader reading the received data, and writer is a
BinaryWriter writing to the destination FileStream.

I know the compressed data is making it to the receiving end because
I've dumped the buffers before decompression and they are the same on
both ends.

What am I doing wrong?

Feb 21 '07 #1
1 2307
The problem, for anyone interested, was the same that others have had
in more traditional applications of the DeflateStream class.
The DeflateStream has to be closed before it will flush.

Therefore, I create a new DeflateStream on every iteration of the loop
and close it after I've written the block to the NetworkStream.

DeflateStream badly needs a flush method to make it more network
friendly.

Feb 23 '07 #2

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

Similar topics

3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
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."...
2
by: Brent Rogers | last post by:
I am having trouble with the DeflateStream.Read() method. For some reason it wants to return Zero. I have this set of classes: =================== using System; using...
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...
4
by: =?Utf-8?B?Sm9uIEphY29icw==?= | last post by:
The compression part of this code works fine for me, but the decompression part of this code does not. The output file from that is the same as the compressed file. What is wrong with my code?...
1
by: sedwick | last post by:
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...
1
by: Smithers | last post by:
IIRC, we can use only System.IO.Compression.InflateStream to uncompress files compressed with System.IO Compression.DeflateStream. If that is in fact true, what are some alternatives you might...
4
by: Grok | last post by:
Trying to zip/unzip files from VB.NET 2005 with 2.0 .NET Framework ... but the file created "SampleFile.zip" is not a readable zip file. (Using GZipStream works correctly to produce a readable .gz...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.