【发布时间】:2010-10-22 23:43:29
【问题描述】:
为什么我不能让这段代码在这里工作?我想在之前压缩过的字节数组上调用它......无论如何,它只是返回一个空字符串......
public static string FromGZipToString( this byte[] source )
{
using( MemoryStream stream = new MemoryStream( ) )
{
stream.Write( source, 0, source.Length );
using (var gzipstream = new GZipStream(stream, CompressionMode.Decompress))
using (var reader = new StreamReader(gzipstream))
{
return reader.ReadToEnd( );
}
}
}
顺便说一下压缩代码....
public static byte[] ToGZip( this string source )
{
using( var stream = new MemoryStream( ) )
using( var compressor = new GZipStream( stream, CompressionMode.Compress ) )
{
var bytes = System.Text.UTF8Encoding.UTF8.GetBytes( source );
compressor.Write( bytes, 0, bytes.Length );
return stream.ToArray( );
}
}
【问题讨论】:
-
你忘记重置 MemoryStream 的位置了吗?
-
写入后重置位置不修复=(
-
已将帖子中的代码更新为工作副本...我通过解压缩中的搜索以及压缩中的 toarray 之前的封闭 zip 流重置了位置。
-
用没有问题的更新代码替换原始问题并不能帮助其他人看到问题。如果您认为它有用,请将更新后的代码作为答案发布(附上解释)
标签: c# gzipstream compression