GZIPStream 压缩总是 Returns 10 字节
GZIPStream Compression Always Returns 10 Bytes
我正在尝试压缩我的 UWP 应用程序中的一些文本。我创建此方法是为了以后更容易:
public static byte[] Compress(this string s)
{
var b = Encoding.UTF8.GetBytes(s);
using (MemoryStream ms = new MemoryStream())
using (GZipStream zipStream = new GZipStream(ms, CompressionMode.Compress))
{
zipStream.Write(b, 0, b.Length);
zipStream.Flush(); //Doesn't seem like Close() is available in UWP, so I changed it to Flush(). Is this the problem?
return ms.ToArray();
}
}
但不幸的是,无论输入文本是什么,这总是 returns 10 个字节。是因为我没有在 GZipStream
上使用 .Close()
吗?
您返回字节数据的时间过早。
Close() 方法被 Dispose() 方法取代。因此 GZIP 流仅在 在 离开 using(GZipStream) {}
块后被写入。
public static byte[] Compress(string s)
{
var b = Encoding.UTF8.GetBytes(s);
var ms = new MemoryStream();
using (GZipStream zipStream = new GZipStream(ms, CompressionMode.Compress))
{
zipStream.Write(b, 0, b.Length);
zipStream.Flush(); //Doesn't seem like Close() is available in UWP, so I changed it to Flush(). Is this the problem?
}
// we create the data array here once the GZIP stream has been disposed
var data = ms.ToArray();
ms.Dispose();
return data;
}
我正在尝试压缩我的 UWP 应用程序中的一些文本。我创建此方法是为了以后更容易:
public static byte[] Compress(this string s)
{
var b = Encoding.UTF8.GetBytes(s);
using (MemoryStream ms = new MemoryStream())
using (GZipStream zipStream = new GZipStream(ms, CompressionMode.Compress))
{
zipStream.Write(b, 0, b.Length);
zipStream.Flush(); //Doesn't seem like Close() is available in UWP, so I changed it to Flush(). Is this the problem?
return ms.ToArray();
}
}
但不幸的是,无论输入文本是什么,这总是 returns 10 个字节。是因为我没有在 GZipStream
上使用 .Close()
吗?
您返回字节数据的时间过早。
Close() 方法被 Dispose() 方法取代。因此 GZIP 流仅在 在 离开 using(GZipStream) {}
块后被写入。
public static byte[] Compress(string s)
{
var b = Encoding.UTF8.GetBytes(s);
var ms = new MemoryStream();
using (GZipStream zipStream = new GZipStream(ms, CompressionMode.Compress))
{
zipStream.Write(b, 0, b.Length);
zipStream.Flush(); //Doesn't seem like Close() is available in UWP, so I changed it to Flush(). Is this the problem?
}
// we create the data array here once the GZIP stream has been disposed
var data = ms.ToArray();
ms.Dispose();
return data;
}