开发者

Problem with decompress, GZipStream

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(gzBuffe开发者_StackOverflow社区r),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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜