如何使用 GZipStream.Read 方法读取数据
How can i read the data using the GZipStream.Read method
我无法使用GZipStream.Read方法读取数据。但是我可以直接从 MemoryStream 读取。我做错了什么?
public static void Main(string[] args)
{
var memStr = new MemoryStream();
//Write
var data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var gzipWr = new GZipStream(memStr, CompressionMode.Compress);
gzipWr.Write(data, 0, data.Length);
//Read
var array1 = new byte[10];
memStr.Position = 0;
var gzipRd = new GZipStream(memStr, CompressionMode.Decompress);
var res1 = gzipRd.Read(array1, 0, array1.Length); // => res1 = 0
//Read
var array2 = new byte[10];
memStr.Position = 0;
var res2 = memStr.Read(array2, 0, array2.Length); // => res2 = 10
}
尝试使用 using
.. 并像这样尝试:
var memStr = new MemoryStream();
////Write
var data = new byte[] { 0, 11, 22, 3, 4, 5, 6, 7, 8, 9 };
using (GZipStream compressionStream = new GZipStream(memStr, CompressionMode.Compress))
{
compressionStream.Write(data, 0, data.Length);
}
////Read
var array1 = new byte[10];
using (MemoryStream mem = new MemoryStream(memStr.ToArray()))
using (GZipStream decompressionStream = new GZipStream(mem, CompressionMode.Decompress))
{
decompressionStream.Read(array1, 0, array1.Length);
}
谢谢!这段代码也有效,看来我必须在阅读之前关闭 gzipWr:
public static void Main(string[] args)
{
var memStr = new MemoryStream();
//Write
var data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
using (var gzipWr = new GZipStream(memStr, CompressionMode.Compress, true))
gzipWr.Write(data, 0, data.Length);
//Read
var array = new byte[10];
memStr.Position = 0;
using (var gzipRd = new GZipStream(memStr, CompressionMode.Decompress))
gzipRd.Read(array, 0, array.Length); // => res = 10
}
我无法使用GZipStream.Read方法读取数据。但是我可以直接从 MemoryStream 读取。我做错了什么?
public static void Main(string[] args)
{
var memStr = new MemoryStream();
//Write
var data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var gzipWr = new GZipStream(memStr, CompressionMode.Compress);
gzipWr.Write(data, 0, data.Length);
//Read
var array1 = new byte[10];
memStr.Position = 0;
var gzipRd = new GZipStream(memStr, CompressionMode.Decompress);
var res1 = gzipRd.Read(array1, 0, array1.Length); // => res1 = 0
//Read
var array2 = new byte[10];
memStr.Position = 0;
var res2 = memStr.Read(array2, 0, array2.Length); // => res2 = 10
}
尝试使用 using
.. 并像这样尝试:
var memStr = new MemoryStream();
////Write
var data = new byte[] { 0, 11, 22, 3, 4, 5, 6, 7, 8, 9 };
using (GZipStream compressionStream = new GZipStream(memStr, CompressionMode.Compress))
{
compressionStream.Write(data, 0, data.Length);
}
////Read
var array1 = new byte[10];
using (MemoryStream mem = new MemoryStream(memStr.ToArray()))
using (GZipStream decompressionStream = new GZipStream(mem, CompressionMode.Decompress))
{
decompressionStream.Read(array1, 0, array1.Length);
}
谢谢!这段代码也有效,看来我必须在阅读之前关闭 gzipWr:
public static void Main(string[] args)
{
var memStr = new MemoryStream();
//Write
var data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
using (var gzipWr = new GZipStream(memStr, CompressionMode.Compress, true))
gzipWr.Write(data, 0, data.Length);
//Read
var array = new byte[10];
memStr.Position = 0;
using (var gzipRd = new GZipStream(memStr, CompressionMode.Decompress))
gzipRd.Read(array, 0, array.Length); // => res = 10
}