我正在使用ICSharpCode.SharpZipLib尝试从Web解压缩文件,我需要做的就是获取未压缩的字节数组。但是我收到错误“InvalidOperationException:无法从此流中读取”。我在Unity3D的c#中工作,目标是webplayer。它显然是可读的,所以我不确定问题。这是我的代码,非常感谢任何帮助。
using (MemoryStream s = new MemoryStream(bytes))
{
using (BinaryReader br = new BinaryReader(s))
{
using (ICSharpCode.SharpZipLib.Zip.ZipInputStream zip = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(s))
{
byte[] bytesUncompressed = new byte[32768];
while (true)
{
Debug.Log("can read: " + zip.CanRead);
int read = zip.Read(bytesUncompressed, 0, bytesUncompressed.Length);
if (read <= 0)
break;
zip.Write(bytesUncompressed, 0, read);
}
}
}
}
答案 0 :(得分:0)
我不清楚你如何填充你的流s
,但你可能需要的只是在阅读之前回滚你的流的位置:
s.Seek(0, System.IO.SeekOrigin.Begin);
答案 1 :(得分:0)
示例模式相当痛苦,让我给你一个“更好(tm)”模式来使用。
byte[] GetBytesFromCompressedStream(MemoryStream src)
{
byte[] uncompressedBytes = null;
using (MemoryStream dst = new MemoryStream())
using (ICSharpCode.SharpZipLib.Zip.ZipInputStream zip = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(src))
{
byte[] buffer = new byte[16 * 1024];
int read = -1;
while((read = zip.Read(buffer, 0, buffer.Length)) > 0)
{
dst.Write(buffer, 0, read);
}
uncompressedBytes = dst.ToArray();
}
return uncompressedBytes;
}