Problem writing compressed data to disk

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



I am exploring the DeflateStream class as a practice. I thought it is
fun to compress a text file and write it to a disk file. So I created
the following method. If you don't bother reading the small code
snippet, here is a summary of what I did:

1. Read the text content into a buffer through FileStream.Read.
2. Create a DeflateStream by wrapping up a MemoryStream.
3. Write the compressed data into the underline stream (the
MemoryStream in this case) through DeflateStream.Write
4. Create a FileStream for write.
5. Call the MemoryStream's Write method to dump the compressed data
into the output FileStream.

Pretty straight forward, isn't it? It compiles and runs OK, but the
compressed data isn't correct. All I am getting is this:

http://gnewsgroup.googlepages.com/compressionproblem

And after unzipping this data, I get a text file which seems to
contain only spaces. Just in case you wonder, my UnzipFile method
works fine with correctly zipped data.

Am I going through the right procedure of compressing a file as
indicated 1 through 5 above? Any hint? Thank you.

public static void CompressFile(string source, string dest)
{
FileStream infile, outfile;
DeflateStream zipStream;
try
{
infile = new FileStream(source, FileMode.Open,
FileAccess.Read);
byte[] buffer = new byte[infile.Length];
int count = infile.Read(buffer, 0, buffer.Length);

// Create a DeflateStream instance.
MemoryStream ms = new MemoryStream();
zipStream = new DeflateStream(ms,
CompressionMode.Compress);

// The follwoing call writes the compressed data in buffer
// to the underline stream, in this case, the MemoryStream
ms.
zipStream.Write(buffer, 0, buffer.Length);

// Create a file stream for writing.
outfile = new FileStream(dest, FileMode.Create,
FileAccess.Write);

// Now, dump the zippped data to the file stream just
created.
ms.WriteTo(outfile);

zipStream.Dispose();
outfile.Dispose();
ms.Dispose();
}
catch (IOException ioe)
{
Console.WriteLine(ioe.Message);
}
}



.



Relevant Pages

  • Zlib question : using Z_FULL_FLUSH to build random access index into compressed data
    ... compress each chunk separately using the same zstream with the flush ... I could observe a bit pattern in compressed data sometimes ... pattern exhibits, ... to the fixed chunk size). ...
    (comp.compression)
  • Re: compression - insights into infinite
    ... The only test that will convince anyone is to take an uncompressable ... Adler, Mahoney, etc.), compress it, and then make the decompressor + ... compressed data file available to everyone. ... keeps your methods safe from theft. ...
    (comp.compression)
  • Re: compression - insights into infinite
    ... The only test that will convince anyone is to take an uncompressable ... Adler, Mahoney, etc.), compress it, and then make the decompressor + ... compressed data file available to everyone. ... keeps your methods safe from theft. ...
    (comp.compression)
  • Stream.Read()/Write() v.s StreamReader/Writer using DeflateStream
    ... generated when I use Stream.Read/Writeto write to the DeflateStream is ... the logic I use to compress using Stream.Write: ... Stream ostream = File.Create; ... Decompressing the Stream.Read/Writeversion produces a file that matches ...
    (microsoft.public.dotnet.framework)
  • Re: LZW decode for images - syntax
    ... > I have a monochrome bitmap image that I compress using LZW and send to ... How to specify the compressed data which is in binary format? ... > Is this EOD marker always 257 for LZW? ...
    (comp.lang.postscript)