Search code examples
c#.netsharpziplib

Extracting the contents of a zipped tab delimited file from a byte array


I've seen some answers to this question posted, but nothing exactly like what I am struggling with, and I'm having some trouble.

Basically, I am using an API that returns data in a byte array like so:

byte[] file = Api.getZippedReport(blah, blah);

I'm trying to figure out the best way to spit out the contents of the tab delimited file in C# so I can do something with it.

What is the simplest way to just get the data back so I can use it without actually having to save the file?


Solution

  • In case this is a .net 4.5 application you can use the newly introduced ZipArchive class which offers a GetEntry() method:

    Stream stream = new MemoryStream(file); // file as your byte[]
    ZipArchive archive = new ZipArchive(stream )
    ZipArchiveEntry entry = archive.GetEntry("ExistingFile.txt");
    
    // Do your logic with the file you get from entry.Open()
    
    entry.LastWriteTime = DateTimeOffset.UtcNow.LocalDateTime;
    

    See ZipArchive Class and ZipArchive.GetEntry Method. There is a property on ZipArchive called Entries that contains all the entries in a readonly collection:

    public ReadOnlyCollection<ZipArchiveEntry> Entries { get; }