How to Read a Single File Inside a Zip Archive

How to read a single file inside a zip archive

Try using the zip:// wrapper:

$handle = fopen('zip://test.zip#test.txt', 'r'); 
$result = '';
while (!feof($handle)) {
$result .= fread($handle, 8192);
}
fclose($handle);
echo $result;

You can use file_get_contents too:

$result = file_get_contents('zip://test.zip#test.txt'); 
echo $result;

How to read a single file inside a folder inside a zip file with DotNetZip?

You can iterate through the contents of the zip file using foreach and then find your file.

            using (ZipFile zip = ZipFile.Read(modPath))
{
ZipEntry e;
foreach (ZipEntry k in zip)
{
if (k.FileName.Contains("info.json"))
{
e = k;
break;
}
}
}

How to read a file inside a folder which itself is inside a zip file

You can add the path name to the filename that you are extracting with ZipFile.open(). Here's how to do it automatically using the naming scheme that you describe in your question:

for zip_name in glob.glob('[0-9].zip'):
# the zip file name one numeric digit only.
z=zipfile.ZipFile(zip_name)
subdir = zip_name[:-4] # the glob pattern ensures that the file name ends with ".zip", so strip off the extension
with z.open('{}/atextfile.txt'.format(subdir)) as f:
for line in f:
for word in line:
print word

How to unzip a single file?

zip.Reader provides you the content of the archive, the files as a slice (of zip.File). There is no helper method to get a file by name, you have to iterate over the files with a loop. You don't need to open / extract the files, but to find a file by name, you have to use a loop.

For example:

r, err := zip.OpenReader("testdata/readme.zip")
if err != nil {
log.Fatal(err)
}
defer r.Close()

for _, f := range r.File {
if f.Name != "folder2/file1.txt" {
continue
}

// Found it, print its content to terminal:
rc, err := f.Open()
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(os.Stdout, rc)
if err != nil {
log.Fatal(err)
}
rc.Close()
fmt.Println()
break
}

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/



Related Topics



Leave a reply



Submit