.net gzip 解压缩流的问题

发布于 2024-07-18 04:48:16 字数 1034 浏览 0 评论 0原文

这组方法可能有什么问题?

        byte[] bytes;

        using (var memory_stream = new MemoryStream())
        using (var gzip_stream = new GZipStream(memory_stream, CompressionMode.Compress))
        {
            var buffer = Encoding.Default.GetBytes("Hello nurse!");
            gzip_stream.Write(buffer, 0, buffer.Length);
            bytes = memory_stream.ToArray();
        }

        int total_read = 0;

        using (var input_stream = new MemoryStream(bytes))
        using (var gzip_stream = new GZipStream(input_stream, CompressionMode.Decompress, true))
        {
            int read;
            var buffer = new byte[4096];
            while ((read = gzip_stream.Read(buffer, 0, buffer.Length)) != 0) {
                total_read += read;
            }
        }

        Debug.WriteLine(bytes);
        Debug.WriteLine(total_read);

gzipStr 是一个有效的 Gzipped Stream(我可以使用 GzipStream() Compress 成功压缩它)。

为什么total_read总是0??? gzip 流正在解压缩我的流吗? 难道我做错了什么?

我在这里做错了什么???!!!

What could be wrong with those set of methods?

        byte[] bytes;

        using (var memory_stream = new MemoryStream())
        using (var gzip_stream = new GZipStream(memory_stream, CompressionMode.Compress))
        {
            var buffer = Encoding.Default.GetBytes("Hello nurse!");
            gzip_stream.Write(buffer, 0, buffer.Length);
            bytes = memory_stream.ToArray();
        }

        int total_read = 0;

        using (var input_stream = new MemoryStream(bytes))
        using (var gzip_stream = new GZipStream(input_stream, CompressionMode.Decompress, true))
        {
            int read;
            var buffer = new byte[4096];
            while ((read = gzip_stream.Read(buffer, 0, buffer.Length)) != 0) {
                total_read += read;
            }
        }

        Debug.WriteLine(bytes);
        Debug.WriteLine(total_read);

The gzipStr is a valid Gzipped Stream (I could compress it successfully with GzipStream() Compress).

Why is total_read always 0??? is gzip stream decompressing my stream? am I doing something wrong?

What am I doing wrong here???!!!

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

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

发布评论

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

评论(1

jJeQQOZ5 2024-07-25 04:48:16

你忘了冲水。 :) 请注意,Encoding.Default 通常不应在生产中使用。 在下面,将其替换为 Encoding.UTF8 (或任何合适的)。 最后,当然,下面的 santiy-check 仅当所有内容都适合单个缓冲区时才有效。 但现在你应该明白了。

kementeus 表示我之前的代码没有帮助,所以下面是我使用的确切代码:

public class GzipBug
{
    public static void Main(String[] a)
    {
        byte[] bytes;
    byte[] buffer;

    Encoding encoding = Encoding.UTF8;

        using (var memory_stream = new MemoryStream())
        using (var gzip_stream = new GZipStream(memory_stream, CompressionMode.Compress))
        {
            buffer = encoding.GetBytes("Hello nurse!");
            gzip_stream.Write(buffer, 0, buffer.Length);
        gzip_stream.Flush();
        bytes = memory_stream.ToArray();
        }

        int total_read = 0;

        using (var input_stream = new MemoryStream(bytes))
        using (var gzip_stream = new GZipStream(input_stream, CompressionMode.Decompress, true))
        {
        int read;
            buffer = new byte[4096];
            while ((read = gzip_stream.Read(buffer, 0, buffer.Length)) != 0) {
        total_read += read;
            }
        }

        Debug.WriteLine(encoding.GetString(buffer, 0, total_read));
        Debug.WriteLine(total_read);

    }
}

它是用以下代码编译的:
gmcs -d:DEBUG -langversion:linq -debug+ GzipBug.cs
并运行为:
MONO_TRACE_LISTENER=Console.Out GzipBug.exe

(您可以删除 MONO_TRACE_LISTENER 位)

You forgot to flush. :) Note that Encoding.Default should not generally be used in production. In the below, replace it with Encoding.UTF8 (or whatever's appropriate). Finally, of course, the below santiy-check only works if everything fits in a single buffer. But now you should get the idea.

kementeus indicated my previous code here didn't help, so below is the exact code I used:

public class GzipBug
{
    public static void Main(String[] a)
    {
        byte[] bytes;
    byte[] buffer;

    Encoding encoding = Encoding.UTF8;

        using (var memory_stream = new MemoryStream())
        using (var gzip_stream = new GZipStream(memory_stream, CompressionMode.Compress))
        {
            buffer = encoding.GetBytes("Hello nurse!");
            gzip_stream.Write(buffer, 0, buffer.Length);
        gzip_stream.Flush();
        bytes = memory_stream.ToArray();
        }

        int total_read = 0;

        using (var input_stream = new MemoryStream(bytes))
        using (var gzip_stream = new GZipStream(input_stream, CompressionMode.Decompress, true))
        {
        int read;
            buffer = new byte[4096];
            while ((read = gzip_stream.Read(buffer, 0, buffer.Length)) != 0) {
        total_read += read;
            }
        }

        Debug.WriteLine(encoding.GetString(buffer, 0, total_read));
        Debug.WriteLine(total_read);

    }
}

It is compiled with:
gmcs -d:DEBUG -langversion:linq -debug+ GzipBug.cs
and run as:
MONO_TRACE_LISTENER=Console.Out GzipBug.exe

(you can remove the MONO_TRACE_LISTENER bit)

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