在运行时将.zip文件解压缩到内存中(.NET)

时间:2014-02-09 08:29:42

标签: vb.net

有没有办法在运行时将zip文件解压缩到计算机的内存中(例如,提取图片但暂时将其放入内存并将其放在PictureBox上) 允许的是图书馆。

1 个答案:

答案 0 :(得分:2)

我认为这应该有用。(未经测试)

需要.net 4.5(System.IO.Compression.dll)

    using System.IO.Compression;


    ...

        byte [] pictureBuffer = null;

        using (ZipArchive archive = ZipFile.OpenRead(zipPath))
        {
            foreach (var entry in archive.Entries)
            {
                if(entry.FullName == "SomePicture")
                {
                    using (zipEntryStream = entry.Open())
                    using (MemoryStream memStream = new MemoryStream())
                    {
                        zipEntryStream.CopyTo(memStream);
                        pictureBuffer = zipEntryStream.ToArray;
                    }
                }
            }

我认为这应该有效。(未经测试) 使用DotNetZip库http://www.nuget.org/packages/DotNetZip/

using (ZipFile zip = new ZipFile(zipPath))
{
            foreach (var entry in zip.Entries)
            {
                if(entry.FileName == "SomePicture")
                {
                    using (var zipEntryStream = entry.OpenReader())
                    using (var memStream = new MemoryStream())
                    {
                        zipEntryStream.CopyTo(memStream);
                        pictureBuffer = zipEntryStream.ToArray;
                    }
                }
            }
}