Problem writing compressed data to disk

A

Author

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);
}
}
 
M

Marc Gravell

Well, that doesn't really make best use of the stream; really you want
to use a small buffer, rather than reading the entire file into
memory. maybe something more like below?

Marc

static void CompressFile(string source, string dest)
{

using(Stream inStream = File.OpenRead(source))
using(Stream outStream = File.Create(dest))
using (DeflateStream zip = new DeflateStream(outStream,
CompressionMode.Compress))
{
const int BUFFER_LEN = 8 * 1024; // 8K
byte[] buffer = new byte[BUFFER_LEN];
int bytesRead;
while ((bytesRead = inStream.Read(buffer, 0,
BUFFER_LEN)) > 0)
{
zip.Write(buffer, 0, bytesRead);
}
inStream.Close();
zip.Close();
outStream.Close();
}
}
 
A

Author

Well, that doesn't really make best use of the stream; really you want
to use a small buffer, rather than reading the entire file into
memory. maybe something more like below?

Marc

static voidCompressFile(string source, string dest)
        {

            using(Stream inStream = File.OpenRead(source))
            using(Stream outStream = File.Create(dest))
            using (DeflateStream zip = new DeflateStream(outStream,
CompressionMode.Compress))
            {
                const int BUFFER_LEN = 8 * 1024; // 8K
                byte[] buffer = new byte[BUFFER_LEN];
                int bytesRead;
                while ((bytesRead = inStream.Read(buffer, 0,
BUFFER_LEN)) > 0)
                {
                    zip.Write(buffer, 0, bytesRead);
                }
                inStream.Close();
                zip.Close();
                outStream.Close();
            }
        }

Thank you very much. I tried your code, and it works perfectly. I am
sorta confused why mine does not work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top