【问题标题】:Issues with .net gzip decompression stream.net gzip 解压流的问题
【发布时间】:2009-04-26 03:46:43
【问题描述】:

这组方法有什么问题?

        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 流是否在解压缩我的流?我是不是做错了什么?

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

【问题讨论】:

    标签: .net compression gzip


    【解决方案1】:

    你忘记冲水了。 :) 请注意,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 位)

    【讨论】:

    • 好的,我明白了你对 gzip 流 Flush 的看法,并使用 Encoding.Default / Encoding.UTF8...但我的问题与 gzip 压缩无关,它工作正常...我做了你的修改但总读取 stills 0 所以主要问题仍然存在
    • 我刚刚发布了我正在使用的确切代码。如果你取出 Flush,它给出 0,如果你把它放进去,它给出 12。我同意 Encoding.UTF8 是一个附带问题,但它总是值得关注编码。
    猜你喜欢
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多