2010-08-09 77 views
9

我有以下方法,由于某种原因,第一次调用Copy to似乎什么都不做?有人知道为什么 在输入的方法是压缩和base64可以提供该方法,如果需要。为什么我需要两个调用来传送CopyTo?

private byte[] GetFileChunk(string base64) 
    { 
     using (
      MemoryStream compressedData = new MemoryStream(Convert.FromBase64String(base64), false), 
      uncompressedData = new MemoryStream()) 
     { 

      using (GZipStream compressionStream = new GZipStream(compressedData, CompressionMode.Decompress)) 
      { 
       // first copy does nothing ?? second works 
       compressionStream.CopyTo(uncompressedData); 
       compressionStream.CopyTo(uncompressedData); 
      } 

      return uncompressedData.ToArray(); 
     } 
    } 

回答

2

如果第一次调用Read()返回0,那么Stream.CopyTo()也不会起作用。虽然这指向了GZipStream的问题,但它很可能是很难它有这样的错误。当您创建压缩数据时,更可能出现问题。像先压缩0字节,然后压缩实际数据。

2

只是一个猜测,但它是因为新GZipStream构造离开该指数在数组的末尾,和第一CopyTo重置它来启动,这样当你调用第二CopyTo它现在开始并正确复制数据?

1

你有多确定第一个副本什么都不做,第二个工作是 ,这是GZipStream类中的一个错误。您的代码应该正常工作,而不需要调用CopyTo两次。

+0

第一个副本不会填充流;读取方法得到相同的行为。 GZipStream是一个微软的类,在课堂上没有错误吗? – 2010-08-09 13:28:20

+0

嗯,你是100%肯定。 Read是完全不同的事情,完全read方法的契约说它不能保证返回请求的字节数量,但CopyTo文档没有提出相同的声明,但也许它在内部使用Read方法。你有没有尝试过使用它在Stream.Read MSDN页面的每个例子的循环http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx – 2010-08-09 13:38:31

+0

雅开始一个循环,第一次读取调用返回0,后续调用读取返回字节直到结束。 – 2010-08-09 13:57:43

0

嗨谢谢大家的意见。事实证明,错误是由编码方法中的错误引起的。方法是

/// <summary> 
    /// Compress file data and then base64s the compressed data for safe transportation in XML. 
    /// </summary> 
    /// <returns>Base64 string of file chunk</returns> 
    private string GetFileChunk() 
    { 
     // MemoryStream for compression output 
     using (MemoryStream compressed = new MemoryStream()) 
     { 
      using (GZipStream zip = new GZipStream(compressed, CompressionMode.Compress)) 
      { 

       // read chunk from file 
       byte[] plaintext = new byte[this.readSize]; 
       int read = this.file.Read(plaintext, 0, plaintext.Length); 

       // write chunk to compreesion 
       zip.Write(plaintext, 0, read); 
       plaintext = null; 

       // Base64 compressed data 
       return Convert.ToBase64String(compressed.ToArray()); 
      } 
     } 
    } 

返回行应低于使用允许压缩流关闭和刷新,这导致解压缩流时不一致的行为。

 /// <summary> 
    /// Compress file data and then base64s the compressed data for safe transportation in XML. 
    /// </summary> 
    /// <returns>Base64 string of file chunk</returns> 
    private string GetFileChunk() 
    { 
     // MemoryStream for compression output 
     using (MemoryStream compressed = new MemoryStream()) 
     { 
      using (GZipStream zip = new GZipStream(compressed, CompressionMode.Compress)) 
      { 

       // read chunk from file 
       byte[] plaintext = new byte[this.readSize]; 
       int read = this.file.Read(plaintext, 0, plaintext.Length); 

       // write chunk to compreesion 
       zip.Write(plaintext, 0, read); 
       plaintext = null; 
      } 

      // Base64 compressed data 
      return Convert.ToBase64String(compressed.ToArray()); 
     } 
    } 

谢谢大家的帮助。

相关问题