Get files from a Zip file

suggest change

This example reads each entry in a zip archive into a byte array. ToDictionary throws if two entries have the same FullName.

public static Dictionary<string, byte[]> GetFiles(byte[] zippedFile) 
{
    using (MemoryStream ms = new MemoryStream(zippedFile))
    using (ZipArchive archive = new ZipArchive(ms, ZipArchiveMode.Read)) 
    {
        return archive.Entries.ToDictionary(x => x.FullName, x => ReadStream(x.Open()));
    }
}

private static byte[] ReadStream(Stream stream) 
{
    using (stream)
    using (var ms = new MemoryStream()) 
    {
        stream.CopyTo(ms);
        return ms.ToArray();
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents