Reading a ZIP without extracting C#

6k Views Asked by At

I have a large ZIP file, containing many directories with tens of thousands of small files that each need to be read.

The process of extracting them from the ZIP using 7-Zip takes quite a long time. In C# I could first extract the entire ZIP somewhere before operating on it, but ideally I'd like to just read one file at a time from the ZIP into memory, without having to extract it to a location on disk.

I have looked at the native C# ZIP utilities along with DotNetZip and SharpZipLib but haven't seen anything directly relating to what I am after.

Can this be done & will it actually offer any performance benefits over simply extracting in the first place?

2

There are 2 best solutions below

0
On

You can use DotNetZip to extract every fileName(and it's properties) out of a zip file.

using (ZipFile zipFile = ZipFile.Read(ExistingZipFile))
{
  foreach (ZipEntry zip in zipFile)
  {
      System.Console.WriteLine("Zipfile: {0}", zip.FileName);
      System.Console.WriteLine("Zipfile: {0}", zip.Comment);
  }
}

See: https://www.nuget.org/packages/DotNetZip/ Old Archive: https://archive.codeplex.com/?p=dotnetzip

0
On
using (ZipArchive zipFile = ZipFile.OpenRead(zipPath))
        {
            foreach(ZipArchiveEntry zip in zipFile.Entries)
            {
                System.Console.WriteLine("Zipfile: {0}", zip.FullName);
                System.Console.WriteLine("Zipfile: {0}", zip.Length);
            }
        }