2012-06-25 145 views
0

我使用GZipStream来压缩一个字符串,我修改了两个不同的例子来看看有什么作用。第一个代码片段是the example in the documentation的重大修改版本,它只是返回一个空字符串。为什么一个字符串压缩方法返回一个空字符串,但另一个不是?

public static String CompressStringGzip(String uncompressed) 
{ 
    String compressedString; 
    // Convert the uncompressed source string to a stream stored in memory 
    // and create the MemoryStream that will hold the compressed string 
    using (MemoryStream inStream = new MemoryStream(Encoding.Unicode.GetBytes(uncompressed)), 
         outStream = new MemoryStream()) 
    { 
     using (GZipStream compress = new GZipStream(outStream, CompressionMode.Compress)) 
     { 
      inStream.CopyTo(compress); 
      StreamReader reader = new StreamReader(outStream); 
      compressedString = reader.ReadToEnd(); 
     } 
    } 
    return compressedString; 

,当我调试它,我能告诉什么是从reader,这是compressedString是空读取。但是,我写的第二种方法,从CodeProject snippet修改成功。

public static String CompressStringGzip3(String uncompressed) 
{ 
    //Transform string to byte array 
    String compressedString; 
    byte[] uncompressedByteArray = Encoding.Unicode.GetBytes(uncompressed); 

    using (MemoryStream outStream = new MemoryStream()) 
    { 
     using (GZipStream compress = new GZipStream(outStream, CompressionMode.Compress)) 
     { 
      compress.Write(uncompressedByteArray, 0, uncompressedByteArray.Length); 
      compress.Close(); 
     } 
     byte[] compressedByteArray = outStream.ToArray(); 
     StringBuilder compressedStringBuilder = new StringBuilder(compressedByteArray.Length); 
     foreach (byte b in compressedByteArray) 
      compressedStringBuilder.Append((char)b); 
     compressedString = compressedStringBuilder.ToString(); 
    } 
    return compressedString; 
} 

为什么第一个代码段不成功,而另一个是?尽管它们略有不同,但我不知道为什么第二个片段中的细微变化可以让它起作用。我使用的样本串SELECT * FROM foods f WHERE f.name = 'chicken';

+0

任何与流的位置有关?在阅读之前,您是否尝试过在方法1中试图寻找流的开始? – Charleh

+0

我添加了'inStream.Seek(0L,SeekOrigin.Begin);'在行之前:'inStream.CopyTo(compress);',但该方法仍然返回一个空字符串。 –

回答

1

我结束了使用下面的代码压缩及解压缩:

public static String Compress(String decompressed) 
{ 
    byte[] data = Encoding.UTF8.GetBytes(decompressed); 
    using (var input = new MemoryStream(data)) 
    using (var output = new MemoryStream()) 
    { 
     using (var gzip = new GZipStream(output, CompressionMode.Compress, true)) 
     { 
      input.CopyTo(gzip); 
     } 
     return Convert.ToBase64String(output.ToArray()); 
    } 
} 

public static String Decompress(String compressed) 
{ 
    byte[] data = Convert.FromBase64String(compressed); 
    using (MemoryStream input = new MemoryStream(data)) 
    using (GZipStream gzip = new GZipStream(input, CompressionMode.Decompress)) 
    using (MemoryStream output = new MemoryStream()) 
    { 
     gzip.CopyTo(output); 
     StringBuilder sb = new StringBuilder(); 
     return Encoding.UTF8.GetString(output.ToArray()); 

    } 
} 

的解释问题的一部分来自this question。虽然我的代码改变什么,我包括在此回答解决了问题,这些线(在我的原码):

foreach (byte b in compressedByteArray) 
      compressedStringBuilder.Append((char)b); 

是有问题的,因为dlev贴切的话来说:

You are interpreting each byte as its own character, when in fact that is not the case. Instead, you need the line:

string decoded = Encoding.Unicode.GetString(compressedByteArray); 

The basic problem is that you are converting to a byte array based on an encoding, but then ignoring that encoding when you retrieve the bytes.

因此,问题解决了,我使用的新代码比我原来的代码简洁得多。

0

你需要以下移动代码的第二using语句外:

using (GZipStream compress = new GZipStream(outStream, CompressionMode.Compress)) 
{ 
    inStream.CopyTo(compress); 
    outStream.Position = 0; 
    StreamReader reader = new StreamReader(outStream); 
    compressedString = reader.ReadToEnd(); 
} 

CopyTo从()不冲洗,结果到底层的MemoryStream。

更新

似乎GZipStream关闭和处置,当它被设置(不是我会设计类的方式),它的底层流。我已经更新了上面的示例并对其进行了测试。

+0

第二个(内部?)'使用'语句?我得到一个错误,说如果我把代码移到那里,outStream是不可读的。 –

+0

如何只手动使用stream.Flush()手动刷新流? – Charleh

+0

@Charleh也可以工作,但我认为在使用语句之外执行它更具可读性,因为它清楚地区分了两个任务。 – Slugart

相关问题