1
votes

I have problem with decomress gzip:

string fileData = string.Empty;
// byte[] starts with 31 and 139
var gzBuffer = entity.Data.Skip(pos).ToArray(); 
using (GZipStream stream = new GZipStream(new MemoryStream(gzBuffer),CompressionMode.Decompress))
{
    const int size = 4096;
    byte[] buffer = new byte[size];
    using (MemoryStream memory = new MemoryStream())
    {
        int count = 0;
        do
        {
            count = stream.Read(buffer, 0, size);
            if (count > 0)
            {
                memory.Write(buffer, 0, count);
            }
        } while (count > 0);
        fileData = Encoding.UTF8.GetString(memory.ToArray());
    }
}

In the debugger, count allways equal 0. Where is the problem?
Thanks.

1
How was this stream compressed? I'v also experienced this problem in the past and the error was actually in the compression logic.Jan-Peter Vos
did you check gzbuffer? Maybe it is empty.Saleh
@Jan-Peter Vos: I can not say for sure because the message comes from the web.user348173
@LightWing: yes, i did check. it's contain datauser348173
Hmmm... I can't see anything obvious... "the data" is the first thing that comes to mind. Can you (for example) show what gzBuffer is?Marc Gravell

1 Answers

0
votes

not sure if this will be of much help to you, but I will try. This is what I used in a sample project to compress/decompress files with GZIP. Maybe you can adapt this code for your needs?

public string Compress(FileInfo fi)
{
    string outPath;

    using (FileStream inFile = fi.OpenRead())
    {
        outPath = fi.FullName + ".gz";
        using (FileStream outFile = File.Create(outPath))
        {
            using (var compress = new GZipStream(outFile, 
                                                 CompressionMode.Compress))
            {
                inFile.CopyTo(compress);
            }
        }
    }

    return outPath;
}

public void Decompress(FileInfo fi)
{
    using (FileStream inFile = fi.OpenRead())
    {
        string curFile = fi.FullName;
        string origName = curFile.Remove(curFile.Length - fi.Extension.Length);

        using (FileStream outFile = File.Create(origName))
        {
            using (var decompress = new GZipStream(inFile, 
                                                   CompressionMode.Decompress))
            {
                decompress.CopyTo(outFile);
            }
        }
    }
}