views:

405

answers:

2

I have code that should do the compression:

        FileStream fs = new FileStream("g:\\gj.txt", FileMode.Open);
        FileStream fd = new FileStream("g:\\gj.zip", FileMode.Create);
        GZipStream csStream = new GZipStream(fd, CompressionMode.Compress);


        byte[] compressedBuffer = new byte[500];
        int offset = 0;
        int nRead;

        nRead = fs.Read(compressedBuffer, offset, compressedBuffer.Length);
        while (nRead > 0)
        {
            csStream.Write(compressedBuffer, offset, nRead);
            offset = offset + nRead;
            nRead = fs.Read(compressedBuffer, offset, compressedBuffer.Length);
        }


        fd.Close();
        fs.Close();

and I think it does, but I want to decompress what was compressed the way above. I do somethink like that:

        FileStream fd = new FileStream("g:\\gj.new", FileMode.Create);
        FileStream fs = new FileStream("g:\\gj.zip", FileMode.Open);
        GZipStream csStream = new GZipStream(fs, CompressionMode.Decompress);

        byte[] decompressedBuffer = new byte[500];
        int offset = 0;
        int nRead;

        nRead=csStream.Read(decompressedBuffer, offset, decompressedBuffer.Length);
        while (nRead > 0)
        {
            fd.Write(decompressedBuffer, offset, nRead);
            offset = offset + nRead;
            nRead = csStream.Read(decompressedBuffer, offset, decompressedBuffer.Length);
        }

        fd.Close();
        fs.Close();

and here it doesn't... I've got nRead = 0 befeore entering the loop... What I do wrong?? The test file I use is the simpliest TEXT file (size: 104 bytes)...

+1  A: 

My first thought is that you haven't closed csStream. If you use using this happens automatically. Since gzip buffers data, you could be missing some.

Secondly; don't increment offset; that is the offset in the buffer (not the stream). Leave at 0:

using (Stream fs = File.OpenRead("gj.txt"))
using (Stream fd = File.Create("gj.zip"))
using (Stream csStream = new GZipStream(fd, CompressionMode.Compress))
{
    byte[] buffer = new byte[1024];
    int nRead;
    while ((nRead = fs.Read(buffer, 0, buffer.Length))> 0)
    {
        csStream.Write(buffer, 0, nRead);
    }
}

using (Stream fd = File.Create("gj.new.txt"))
using (Stream fs = File.OpenRead("gj.zip"))
using (Stream csStream = new GZipStream(fs, CompressionMode.Decompress))
{
    byte[] buffer = new byte[1024];
    int nRead;
    while ((nRead = csStream.Read(buffer, 0, buffer.Length)) > 0)
    {
        fd.Write(buffer, 0, nRead);
    }
}
Marc Gravell
The problem is that commression works well, but cant make it to decompress.
ergen
It works!!! Thanks!
ergen
A: 

How do I know that the DeCompressed values will fit in the buffer[1024]? What if the decomprssed values (byte array) is 1500 byte long, it will not fit in 1024 byte long Buffer.

Hypothetical scenerio: 1. I have a file abc.txt with 5000 characters in it 2. I read the file and compress it, saved in new file abc.zzz 3. I want to decompress the abc.zzz file in ONE COMMAND, NOT USING WHILE:-

using (Stream fs = File.OpenRead("abc.zzz")) 
using (Stream csStream = new GZipStream(fs, CompressionMode.Decompress)) 
{ 
    byte[] buffer = new byte[1024]; //How do I know 1024 is enough?
    int nRead; 
    csStream.Read(buffer, 0, buffer.Length)

    //Do whatever I want to do with buffer now
}

Questions: 1. In the above scenerio How do I know 1024 is enough to hold the decompressed data?

  1. On the otherhand, if I had comprssed 5000 chars in one command, can I use while loop and DeCompress 1024 at a time? Does it work?

I will appreciate if anyone can reply to my problem. Thanks.

Mehdi Anis