.net gzip解压缩流的问题

问题描述:

这些方法集有什么问题?.net gzip解压缩流的问题

 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()压缩成功压缩它)。

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

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

你忘了冲水。 :)请注意,Encoding.Default通常不应用于生产。在下面,将其替换为Encoding.UTF8(或其他合适的)。最后,当然,下面的santiy检查只适用于一切都适用于单个缓冲区。但现在你应该明白了。

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

    } 
} 

它编译: 的GMC -d:DEBUG -langversion:LINQ -debug + GzipBug。 CS 和运行: MONO_TRACE_LISTENER = Console.Out GzipBug.exe

(你可以删除MONO_TRACE_LISTENER位)

+0

好吧,我用gzip流你的观点冲洗并用Encoding.Default/Encoding.UTF8。 B ut我的问题从来没有涉及gzip压缩,它工作正常... 我做了你的修改,但总读_stills_ 0 所以主要问题仍然存在 – kementeus 2009-04-26 19:35:19