读取离子Zip作为内存流C#

时间:2014-12-22 09:01:25

标签: c# stream zip memorystream

我正在使用Ionic.Zip使用此方法将ZipFile提取到内存流:

private MemoryStream GetReplayZipMemoryStream()
{
    MemoryStream zipMs = new MemoryStream();
    using (ZipFile zip = ZipFile.Read(myFile.zip))
    {
        foreach (ZipEntry zipEntry in zip)
        {
            if (zipEntry.FileName.StartsWith("Aligning") || zipEntry.FileName.StartsWith("Sensing"))
            {
                zipEntry.Extract(zipMs);
            }
        }
    }

    zipMs.Seek(0, SeekOrigin.Begin);
    return zipMs;
}

一旦我完成解压缩,我想读取流并根据条目文件名获取一些条目。我怎么能这样做?

我尝试使用下面的代码调用,但是我在ZipFile.Read(ms)上收到了错误,其中说:

  

无法将其读作ZipFile

Stream ms = GetReplayZipMemoryStream();
using (ZipFile zip = ZipFile.Read(ms))
{
    ZipEntry imageEntry = zip.Entries.First(x => x.FileName.EndsWith(".png"));
    using (Stream s = imageEntry.OpenReader())
    {
        var image = Image.FromStream(s);
        pictureBox1.Image = image;
    }
}

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:13)

这可能是一个旧问题和迟到的答案,但我已经做了一些事情来获取文件作为字节集合及其文件名这样

public static Dictionary<string, byte[]> Decompress(Stream targFileStream)
{
    Dictionary<string, byte[]> files = new Dictionary<string, byte[]>();

    using(ZipFile z = ZipFile.Read(targFileStream))
    {
        foreach (ZipEntry zEntry in z)
        {
            MemoryStream tempS = new MemoryStream();
            zEntry.Extract(tempS);

            files.Add(zEntry.FileName, tempS.ToArray());
        }
    }

    return files;
}