How to read data from a zip file without having to unzip the entire file

Is there a .Net (C #) for extracting data from a zip file without unpacking the full file?

I just probably want to extract the data (file) from the beginning of the zip file, obviously, it depends if the compression algorithm compresses the file in a deterministic order.

+63
c # compression zip
May 11 '11 at 16:54
source share
6 answers

DotNetZip is your friend here.

Simply:

using (ZipFile zip = ZipFile.Read(ExistingZipFile)) { ZipEntry e = zip["MyReport.doc"]; e.Extract(OutputStream); } 

(you can also extract the file or other destinations).

Reading the zip file's table of contents is as simple as:

 using (ZipFile zip = ZipFile.Read(ExistingZipFile)) { foreach (ZipEntry e in zip) { if (header) { System.Console.WriteLine("Zipfile: {0}", zip.Name); if ((zip.Comment != null) && (zip.Comment != "")) System.Console.WriteLine("Comment: {0}", zip.Comment); System.Console.WriteLine("\n{1,-22} {2,8} {3,5} {4,8} {5,3} {0}", "Filename", "Modified", "Size", "Ratio", "Packed", "pw?"); System.Console.WriteLine(new System.String('-', 72)); header = false; } System.Console.WriteLine("{1,-22} {2,8} {3,5:F0}% {4,8} {5,3} {0}", e.FileName, e.LastModified.ToString("yyyy-MM-dd HH:mm:ss"), e.UncompressedSize, e.CompressionRatio, e.CompressedSize, (e.UsesEncryption) ? "Y" : "N"); } } 
+59
May 11 '11 at 17:54
source share

With .Net Framework 4.5 (using ZipArchive ):

 using (ZipArchive zip = ZipFile.Open(zipfile, ZipArchiveMode.Read)) foreach (ZipArchiveEntry entry in zip.Entries) if(entry.Name == "myfile") entry.ExtractToFile("myfile"); 

Find "myfile" in the zipfile and extract it.

+71
Feb 07 '13 at 14:45
source share

Something like this will list and extract files one at a time if you want to use SharpZipLib:

 var zip = new ZipInputStream(File.OpenRead(@"C:\Users\Javi\Desktop\myzip.zip")); var filestream = new FileStream(@"C:\Users\Javi\Desktop\myzip.zip", FileMode.Open, FileAccess.Read); ZipFile zipfile = new ZipFile(filestream); ZipEntry item; while ((item = zip.GetNextEntry()) != null) { Console.WriteLine(item.Name); using (StreamReader s = new StreamReader(zipfile.GetInputStream(item))) { // stream with the file Console.WriteLine(s.ReadToEnd()); } } 

Based on this example: content inside a zip file

+14
May 11 '11 at 17:01
source share

Here's how a UTF8 text file can be read from a zip archive into a string variable (.NET Framework 4.5 and later):

 string zipFileFullPath = "{{TypeYourZipFileFullPathHere}}"; string targetFileName = "{{TypeYourTargetFileNameHere}}"; string text = new string( (new System.IO.StreamReader( System.IO.Compression.ZipFile.OpenRead(zipFileFullPath) .Entries.Where(x => x.Name.Equals(targetFileName, StringComparison.InvariantCulture)) .FirstOrDefault() .Open(), Encoding.UTF8) .ReadToEnd()) .ToArray()); 
+1
Apr 21 '16 at 7:36
source share

Mail files have a table of contents. Each zip utility should be able to request only TOC. Or you can use a command-line tool such as 7zip -t to print a table of contents and redirect it to a text file.

0
May 11 '11 at 17:06
source share

In this case, you will need to parse the local zip header entries. Each file stored in a zip file has a previous entry in the header of the local file, which (as a rule) contains sufficient information for decompression. In general, you can do a simple analysis of such records in the stream, select the desired file, copy the header + compressed file data to another file and call unzip to this part (if you do not want to deal with all the code or the Zip decompression library).

0
May 11 '11 at 18:09
source share



All Articles