Read Zip Files In PHP (Simple Examples)

Welcome to a quick tutorial on how to read zip files in PHP. So the old zip functions are deprecated in PHP 8, how do we read files using Zip Archive then? This has caused some confusion, but it is actually very straightforward.

To read zip files in PHP, we can use statIndex() to get the file statistics, and getFromName() to directly read a file.

  • $zip = new ZipArchive;
  • $zip->open("FILE.ZIP", ZipArchive::RDONLY);
  • $entries = $zip->count();
  • for ($i=0; $i<$entries; $i++) { $stat = $zip->statIndex($i); }
  • $content = $zip->getFromName("FILE-IN-ZIP.TXT");
  • $zip->close();

That should cover the basics, but if you need more concrete examples – Read on!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

PHP READ ZIP FILE

All right, let us now get into the examples of how to read a zip file in PHP.

 

1) READ FILE LIST OR INFORMATION

1-info.php
<?php
// (A) OPEN ZIP FILE
$file = "test.zip";
$zip = new ZipArchive;
if ($zip->open($file, ZipArchive::RDONLY) !== true) {
  exit("Failed to open $file");
}

// (B) LOOP THROUGH EVERY FILE & FOLDER - DISPLAY INFORMATION
$total = $zip->count();
for ($i=0; $i<$total; $i++) {
  print_r($zip->statIndex($i));
}

// (C) CLOSE ZIP
$zip->close();

This should be pretty self-explanatory, and how Zip Archive works is actually somewhat like an array – Each file or folder inside the zip archive will have an index number. To get a whole list of files/folders inside the archive, we can simply use a for loop to run through them and statIndex() to fetch the information.

 

 

2) READ FILE IN ZIP ARCHIVE

2-read.php
<?php
// (A) OPEN ZIP FILE
$file = "test.zip";
$zip = new ZipArchive;
if ($zip->open($file, ZipArchive::RDONLY) !== true) {
  exit("Failed to open $file");
}

// (B) DIRECTLY READ FILE USING INDEX
$content = $zip->getFromIndex(0);
echo "$content<br>";

// (C) OR USE GET FROM FILE NAME
$content = $zip->getFromName("second.txt");
echo "$content<br>";

// (D) CLOSE ZIP
$zip->close();

To read the contents of a file inside a zip archive, we can either use getFromIndex() or getFromName(). But there will be a problem dealing with large files in this one. While both functions accept a second parameter to restrict the number of bytes read, there is just no way to open it as a file stream – The best way to deal with large files is still to unzip it and use fgets() instead.

 

 

3) LEGACY ZIP READ

3-legacy.php
<?php
// (A) OPEN ZIP FILE
$file = "test.zip";
$zip = zip_open($file);
if (is_numeric($zip) || $zip===false) {
  exit("Failed to open $file");
}

// (B) READ FILES
while ($entry = zip_read($zip)) {
  // (B1) GET FILE NAME & SIZE
  $name = zip_entry_name($entry);
  $size = zip_entry_filesize($entry);
  echo "$name - $size bytes<br>";

  // (B2) READ FILE DIRECTLY
  if ($name == "first.txt") {
    if (zip_entry_open($zip, $entry)) {
      $contents = zip_entry_read($entry);
      echo "$contents<br>";
      zip_entry_close($entry);
    } else { echo "Cannot open first.txt"; }
  }
}
zip_close($zip);

Yes, the old zip functions are deprecated in PHP 8 (they still work but will eventually disappear). I shall not explain too much on this example but leave it here as “legacy support”… Do not use this method anymore, unless you have to support the older PHP versions.

 

 

EXTRA) HOW TO UNZIP FILE

<?php
$zip = new ZipArchive;
if ($zip->open("TEST.ZIP") === TRUE) {
  $zip->extractTo("DESTINATION/FOLDER/");
  $zip->close();
}

 

EXTRAS

That’s all for the main tutorial, and here is a small section on some extras and links that may be useful to you.

 

SUMMARY & REFERENCES

Function Description Link
$zip->open(FILE, MODE) Open a zip archive. Click Here
$zip->count() Get a count of the total number of files/folders. Click Here
$zip->statIndex(INDEX) Get the file/folder information at the specified index slot. Click Here
$zip->getFromIndex(INDEX) Read the file/folder content at the specified index slot. Click Here
$zip->getFromName(FILE NAME) Read the file/folder content of the specified file name. Click Here
$zip->close() Close the zip archive. Click Here

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *