C#SharpZipLib罐';尝试在Unity3D中解压缩zip时,无法读取流

C#SharpZipLib罐';尝试在Unity3D中解压缩zip时,无法读取流,c#,unity3d,sharpziplib,icsharpcode,C#,Unity3d,Sharpziplib,Icsharpcode,我正在使用ICSharpCode.SharpZipLib尝试从web解压一个文件,我所需要做的就是获取未压缩的字节数组。但是,我得到错误“InvalidOperationException:无法从此流读取”。我在Unity3D的c#中工作,目标是网络播放器。它显然是可读的,所以我不确定这个问题。这是我的代码,任何帮助都将不胜感激 using (MemoryStream s = new MemoryStream(bytes)) { using (BinaryReader br = new

我正在使用ICSharpCode.SharpZipLib尝试从web解压一个文件,我所需要做的就是获取未压缩的字节数组。但是,我得到错误“InvalidOperationException:无法从此流读取”。我在Unity3D的c#中工作,目标是网络播放器。它显然是可读的,所以我不确定这个问题。这是我的代码,任何帮助都将不胜感激

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);
            }
        }
    }
}
使用(MemoryStream s=新的MemoryStream(字节))
{
使用(BinaryReader br=新的BinaryReader)
{               
使用(ICSharpCode.SharpZipLib.Zip.ZipInputStream Zip=new ICSharpCode.SharpZipLib.Zip.ZipInputStream))
{
字节[]字节未压缩=新字节[32768];
while(true)
{
Log(“可以读取:+zip.CanRead”);
int read=zip.read(bytesUncompressed,0,bytesUncompressed.Length);

如果(read我不清楚您是如何填充流的
s
,但您可能需要的只是在读取流之前回滚流的位置:

s.Seek(0, System.IO.SeekOrigin.Begin);

示例模式相当痛苦,让我给你一个“更好(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;
}

为什么这里有一个BinaryReader?因为它缓冲了流,所以很有可能它已经将流的位置移动到了一个你不希望的地方;从而出现了一个损坏或无效的压缩。为什么你都在读写同一个ZipInputStream?这可能吗?我试过了his和它仍然无法从流中读取,尽管.CanRead返回true。@正常功率您确定源流的长度>0吗?您能确认该位置<长度吗?