How to Read Data from a Zip File Without Having to Unzip the Entire File

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

DotNetZip is your friend here.

As easy as:

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

(you can also extract to a file or other destinations).

Reading the zip file's table of contents is as easy 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");

}
}

Edited To Note: DotNetZip used to live at Codeplex. Codeplex has been shut down. The old archive is still available at Codeplex. It looks like the code has migrated to Github:

  • https://github.com/DinoChiesa/DotNetZip. Looks to be the original author's repo.
  • https://github.com/haf/DotNetZip.Semverd. This looks to be the currently maintained version. It's also packaged up an available via Nuget at https://www.nuget.org/packages/DotNetZip/

How to read file from ZIP archive to memory without extracting it to file first, by using C# .NET 4.5?

Adapted from the ZipArchive and XmlSerializer.Deserialize() manual pages.

The ZipArchiveEntry class has an Open() method, which returns a stream to the file.

string zipPath = @"c:\example\start.zip";

using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
var sample = archive.GetEntry("sample.xml");
if (sample != null)
{
using (var zipEntryStream = sample.Open())
{
XmlSerializer serializer = new XmlSerializer(typeof(SampleClass));

SampleClass deserialized =
(SampleClass)serializer.Deserialize(zipEntryStream);
}
}
}

Note that, as documented on MSDN, you need to add a reference to the .NET assembly System.IO.Compression.FileSystem in order to use the ZipFile class.

How to recursively explore for zip file contents without extraction

As I pointed to in the question I marked yours basically as a duplicate off, you need to open the inner zip file.

I'd change your "open from file" method to be like this:

// Open ZipArchive from a file
public bool findPng(zipPath) {
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
return findPng(archive);
}
}

And then have a separate method that takes a ZipArchive so that you can call it recursively by opening the entry as a Stream as demonstrated here

// Search ZipArchive for PNG
public bool findPng(ZipArchive archive)
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
string s = entry.FullName;
if (s.EndsWith(".zip"))
{
// Open inner zip and pass to same method
using (ZipArchive innerArchive = new ZipArchive(entry.Open()))
{
if (findPng(innerArchive))
return true;
}
}
/* same as above with folders within the zip */

if(s.EndsWith(".png"))
return true;
}
return false;
}
}

As an optimisation, I would recommend checking all of the filenames before handling nested zip files.

How to access all file without unzip any zip file in android?

I have tried to solve this and i got the following solution.

Zip is a one type of file Which include many directory and files so we can't get it in directory and file format.
We can only get it in Bytes. Which is unused.
So we Have To Unzip it.....

Because Whenever we unzip it, then itself make one type of directory of sub directory and files So then we can access it in directory and file format.

R Reading in a zip data file without unzipping it

If your zip file is called Sales.zip and contains only a file called Sales.dat, I think you can simply do the following (assuming the file is in your working directory):

data <- read.table(unz("Sales.zip", "Sales.dat"), nrows=10, header=T, quote="\"", sep=",")

Reading contents of zip file without extracting

You actually are reading what exactly is in the file.

The /r/n character is the newline character in windows. The question
Difference between \n and \r? goes into a bit more detail, but what it comes down to is that Windows uses /r/n as its newline.

The b' character you seeing is related to python and how it parses the file. The question What does the 'b' character do in front of a string literal? does a good job answering why exactly that is happening, but the documentation quoted is:

Bytes literals are always prefixed with 'b' or 'B'; they produce an
instance of the bytes type instead of the str type. They may only
contain ASCII characters; bytes with a numeric value of 128 or greater
must be expressed with escapes.

EDIT: I actually found a very similar answer you can pull from for reading without the extra characters: py3k: How do you read a file inside a zip file as text, not bytes?. The basic idea was you could use this:

items_file  = io.TextIOWrapper(items_file, encoding='your-encoding', newline='')


Related Topics



Leave a reply



Submit