Location: PHPKode > scripts > The Zipa > zipa.class.php
<?PHP
/*
HDF's Zipa class

Description:
---
This little class allows the easy handling of zip files (for reading only) with the help of php's zip extension.
Can automatically extract everything from a specified zip file, or can read everything in the zip file into an array.

API:
---
string file
array readzip([file])
extracta([path])

file:
The path and name of the zip file. Can be relative.

readzip([file]):
Reads the contents of a zip file into an array. The path and filename is in the index, and the values are the contents of the
files, or false if the index is a directory.
The optional file parameter is supposed to be the same as the public variable "file" in the class.

extracta([path]):
Extracts the zip file, given in the public variable "file", to the given path. Or if not specified, to the current folder.
*/

class zipa {
  public $file;

  public function readzip($file = false) {
    if (!$file) $file = $this->file;
    $zip = zip_open(realpath($file));
    if ($zip === false) return false;
    while ($zip_entry = zip_read($zip)) {
      $path = zip_entry_name($zip_entry);
      if (zip_entry_open($zip, $zip_entry, "r")) {
        $content[$path] = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
        zip_entry_close($zip_entry);
      } else {
        $content[$path] = false;
      }
    }
    zip_close($zip);
    return $content;
  }

  private function extractzip($content, $index, $path = false) {
    if (!$path) $path = ".";
    if (substr($path, -1) == "/") $path = rtrim($path, "/");
    $index = $path . "/" . rtrim($index, "/");
    if (!$content) {
      if (!file_exists($index)) mkdir($index, 0644);
    } else {
      file_put_contents($index, $content);
    }
  }
  
  public function extracta($path = ".") {
    array_walk($this->readzip(), array($this, "extractzip"), $path);
  }
}
?>
Return current item: The Zipa