Unzip a memorystream (Contains the zip file) and get the files

后端 未结 4 494
予麋鹿
予麋鹿 2020-11-27 16:31

I have a memory stream that contains a zip file in byte[] format .

Is there any way I can unzip this memory stream, without any need of writing the file to disk ?

4条回答
  •  暖寄归人
    2020-11-27 16:56

    I've just had a similar issue and the answer I found which I think seems to be fairly elegant is to use #ZipLib (available using nuget) and do the following:

    private byte[] GetUncompressedPayload(byte[] data)
    {
        using (var outputStream = new MemoryStream())
        using (var inputStream = new MemoryStream(data))
        {
            using (var zipInputStream = new ZipInputStream(inputStream))
            {
                zipInputStream.GetNextEntry();
                zipInputStream.CopyTo(outputStream);
            }
            return outputStream.ToArray();
        }
    }
    

    This seems to have worked a treat. Hope this helps.

提交回复
热议问题