简体   繁体   中英

Reading zip file from byte array using Ionic.zip

I have a piece of code that allows to decompress a byte array:

public static byte[] Decompress(this byte[] data)
{
    using (ZipFile zout = ZipFile.Read(data))
    {
        ZipEntry entry = zout.FirstOrDefault();
        Assert.ObjectIsNotNull(entry, "Unable to find default ZIP entry");
        MemoryStream zos = new MemoryStream();
        entry.Extract(zos);
        return zos.ToArray();
    }
}

I upgraded to the latest version of Ionic.zip and now I am getting the following error:

Cannot convert byte[] to string.

The overload ZipFile.Read(byte[]) is no longer available in the most recent version.

How can I read a zip file from a byte array?

The ZipFile.Read method takes either a filename or a stream to read, so you need to provide a stream for it to read:

using (MemoryStream stream = new MemoryStream(data))
using (ZipFile zout = ZipFile.Read(stream))
{
    // ....

You can use the built-in ZipArchive class in System.IO.Commpression .

using(var stream = new MemoryStream(data))
{
    using(var archive = new ZipArchive(stream))
    {
        // Use the archive
    }
 }

ZipArchive https://msdn.microsoft.com/en-us/library/hh158268(v=vs.110).aspx

MemoryStream https://msdn.microsoft.com/en-us/library/e55f3s5k(v=vs.110).aspx

You will need to add a reference to System.IO.Compression , it is not in mscorlib .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM