解压缩问题,GZipStream

发布于 2024-11-07 08:04:27 字数 748 浏览 4 评论 0原文

我在解压缩 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());
    }
}

在调试器中,count 始终等于 0。问题出在哪里?
谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻了热茶 2024-11-14 08:04:27

不确定这对你是否有很大帮助,但我会尝试。这是我在示例项目中使用 GZIP 压缩/解压缩文件的方法。也许您可以根据您的需要调整此代码?

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

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);
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文