1

我正在使用 ICSharpCode.SharpZipLib 尝试从网络上解压缩文件,我需要做的就是获取未压缩的字节数组。但是我收到错误“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);
            }
        }
    }
}
4

2 回答 2

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;
}
于 2014-04-28T18:15:29.853 回答
0

我不清楚你是如何填充你的流的s,但你可能需要的是在阅读它之前回滚你的流的位置:

s.Seek(0, System.IO.SeekOrigin.Begin);
于 2014-04-28T17:04:04.627 回答