Read ionic Zip as Memory Stream C#

主宰稳场 提交于 2019-12-09 15:50:18

问题


I am using Ionic.Zip to extract ZipFile to memory stream with this method:

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;
}

Once I am done extracting, I want to read the streams and get some of the entries based on the entry filename. How can I do that?

I tried by calling with the code below, but I get error on the ZipFile.Read(ms) which said:

Cannot read that as a 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;
    }
}

Thank you in advance for the help!


回答1:


This may be little bit old question and late answer but I have did something to get the files as bytes collections and its file names like this

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;
}


来源:https://stackoverflow.com/questions/27599474/read-ionic-zip-as-memory-stream-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!