PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<yp_orderzip_close>
view the version of this page
Last updated: Thu, 21 Aug 2003

CXIV. Zip File Functions (Read Only Access)

Introduction

This module enables you to transparently read ZIP compressed archives and the files inside them.

Requirements

This module uses the functions of the ZZIPlib library by Guido Draheim. You need ZZIPlib version >= 0.10.6.

Note that ZZIPlib only provides a subset of functions provided in a full implementation of the ZIP compression algorithm and can only read ZIP file archives. A normal ZIP utility is needed to create the ZIP file archives read by this library.

Installation

Zip support in PHP is not enabled by default. You will need to use the --with-zip[=DIR] configuration option when compiling PHP to enable zip support.

Note: Zip support before PHP 4.1.0 is experimental. This section reflects the Zip extension as it exists in PHP 4.1.0 and later.

Runtime Configuration

This extension has no configuration directives defined in php.ini.

Resource Types

This extension has no resource types defined.

Predefined Constants

This extension has no constants defined.

Examples

This example opens a ZIP file archive, reads each file in the archive and prints out its contents. The test2.zip archive used in this example is one of the test archives in the ZZIPlib source distribution.

Example 1. Zip Usage Example

<?php

$zip = zip_open("/tmp/test2.zip");

if ($zip) {

    while ($zip_entry = zip_read($zip)) {
        echo "Name:               " . zip_entry_name($zip_entry) . "\n";
        echo "Actual Filesize:    " . zip_entry_filesize($zip_entry) . "\n";
        echo "Compressed Size:    " . zip_entry_compressedsize($zip_entry) . "\n";
        echo "Compression Method: " . zip_entry_compressionmethod($zip_entry) . "\n";

        if (zip_entry_open($zip, $zip_entry, "r")) {
            echo "File Contents:\n";
            $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
            echo "$buf\n";

            zip_entry_close($zip_entry);
        }
        echo "\n";

    }

    zip_close($zip);

}

?>
Table of Contents
zip_close -- Close a Zip File Archive
zip_entry_close -- Close a Directory Entry
zip_entry_compressedsize -- Retrieve the Compressed Size of a Directory Entry
zip_entry_compressionmethod -- Retrieve the Compression Method of a Directory Entry
zip_entry_filesize -- Retrieve the Actual File Size of a Directory Entry
zip_entry_name -- Retrieve the Name of a Directory Entry
zip_entry_open -- Open a Directory Entry for Reading
zip_entry_read -- Read From an Open Directory Entry
zip_open -- Open a Zip File Archive
zip_read -- Read Next Entry in a Zip File Archive


add a note add a note User Contributed Notes
Zip File Functions (Read Only Access)
Joris P.
04-Sep-2003 11:36
"If you just want to unzip everything of a zip file in a directory"

Watch out with that function, your fopen function uses 'r' instead of 'rb' and the fwrite does not include a filesize. So if you have binary files into that zip file (what you probably do), it will go wrong...
phpContrib (A T) esurfers d o t c o m
19-Jul-2003 04:41
Sometimes you may not be able to install the necessary resources to run the ZIP lib. Maybe because you have no control of the server, or maybe because you cannot find the correct libraries for your php...

Anyway do not despair, there is another solution!

I was in this situation (my client could not install it on the server) so I developed a small libraryin pure PHP that is compatible with PHP's own lib but uses the commandline 'unzip' command directly form PHP. You can configure it to use any commandline unzipping tool (so it should work even with other compressed files, as long as there is a commandline tool). It should work on most platforms with an unzip command (Win32 included) although I have tested only under unix.

What it does exactly, is replacing all the functions of the Zip library with equivalent ones. So the example in this page will work (just rename every zip_... function m_zip_...):

<?php

$zip 
=m_zip_open("/tmp/test2.zip");

if (
$zip) {

    while (
$zip_entry m_zip_read($zip)) {
        echo 
"Name:               " m_zip_entry_name($zip_entry) . "\n";
        echo 
"Actual Filesize:    " m_zip_entry_filesize($zip_entry) . "\n";
        echo 
"Compressed Size:    " m_zip_entry_compressedsize($zip_entry) . "\n";
        echo 
"Compression Method: " m_zip_entry_compressionmethod($zip_entry) . "\n";

        if (
m_zip_entry_open($zip$zip_entry"r")) {
            echo 
"File Contents:\n";
           
$buf m_zip_entry_read($zip_entryzip_entry_filesize($zip_entry));
            echo 
"$buf\n";

           
m_zip_entry_close($zip_entry);
        }
        echo 
"\n";

    }

   
m_zip_close($zip);

}

?>

The library will create a new folder and call unzip on the file when you call m_zip_open().
It will get rid of all the unzipped things when you call m_zip_close().
It will walk the directory structure of the ZIP file as you call m_zip_read($zip) getting into directories and then out again.
Then you will be able to read chunks of (or th whole) files with m_zip_entry_read()

: : :   INCOMPATIBILITIES  : : :
Once the zipfile is unzipped, I will have no way to know the size of the entries or the compression methods, so the functions m_zip_entry_compressedsize() and m_zip_entry_compressionmethod() just return a '?'.

Of course what zipfiles will be compatible and what not will dependo on the command line tool you choose to use.

: : :   CONFIGURATION  : : :
There are two things to setup:

A path to a directory where we can create subfolders and unzip things:

define('UNZIP_DIR','/data/fandango/html/update/unzipped/');

The command to invoke to unzip the files:

define('UNZIP_CMD','unzip -o @_SRC_@ -x -d @_DST_@');

DO NOT REMOVE THE @_SRC_@ and @ _DST_@ markers. This is where the library will put the source file name you pass and the destination subfolder (it will create a subfolder of UNZIP_DIR and pass that)

: : :   DOWNLOAD  : : :
You will find the library and a demo file (with the example on this page) at http://esurfers.com/m-zip/
travis
18-Jun-2003 12:06
just a not of caution--using the dynamic zip class mentioned earlier seems to cause issues with high ascii characters (their values are not preserved correctly, and the file will not unzip)
NOSP at M dot patrick_allaert(AROBAS)pandora dot be
09-Apr-2003 12:08
If you just want to unzip everything of a zip file in a directory (and create unexisting directories), let's try this:

function unzip($file, $path) {
  $zip = zip_open($file);
  if ($zip) {
    while ($zip_entry = zip_read($zip)) {
      if (zip_entry_filesize($zip_entry) > 0) {
        // str_replace must be used under windows to convert "/" into "\"
        $complete_path = $path.str_replace('/','\\',dirname(zip_entry_name($zip_entry)));
        $complete_name = $path.str_replace ('/','\\',zip_entry_name($zip_entry));
        if(!file_exists($complete_path)) {
          $tmp = '';
          foreach(explode('\\',$complete_path) AS $k) {
            $tmp .= $k.'\\';
            if(!file_exists($tmp)) {
              mkdir($tmp, 0777);
            }
          }
        }
        if (zip_entry_open($zip, $zip_entry, "r")) {
          $fd = fopen($complete_name, 'w');
          fwrite($fd, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
          fclose($fd);
          zip_entry_close($zip_entry);
        }
      }
    }
    zip_close($zip);
  }
}

To use this function:
unzip('c:\\file.zip','c:\\temp\\'); // BE CAREFULL: second argument *MUST* finish by a "\" ! ("/" under *nix)

Patrick
chris
23-Mar-2003 02:32
Watch out with archives that contain subfolders if you're using the zip functions to extract archives to actual files.  Let's say you're trying to extract foldername/filename.txt from the archive.  You can't fopen a directory that doesn't exist, so you'll have to check for the existance of directory foldername and create it if it isn't found, then fopen foldername/filename.txt and begin writing to that.
ottawasixtyseven at hotmail dot com
30-Oct-2002 04:04
WOW ... after weeks and weeks of research I thought I'd make somebody elses life a little easier. If you're wondering how to make PHP 4.2.3 read windows zip files (winzip, pkzip, etc) do this:

NOTE: THIS IS FOR WINDOWS SERVERS NOT LINUX OR OTHER. You need zziplib for Linux. http://zziplib.sourceforge.net/

ON PHP WINDOWS SERVER

1) Grab the php_zip.dll from the extensions dir in php-4.3.0pre2-Win32.zip

2) Add a line extension=php_zip.dll to your php.ini

3) Restart your web server

php_zip.dll obviously works on PHP 4.3.0pre2 but you can't run Zend Optimizer on PHP 4.3 (yet). You can run Zend Optimizer on PHP 4.2.3 but it doesn't ship with php_zip.dll. The php_zip.dll that ships with PHP 4.3.0pre2 may even work with older version but I haven't tested that.

For documentation on how to use the zip functions (not the gzip functions that are documented on the php site) go here:

http://zziplib.sourceforge.net/zziplib.html

Newbie Von Greenhorn
a1cypher at shaw dot ca
11-Oct-2002 07:28
For creating zip files, I highly suggest that you have a look at the class used in phpMyAdmin.  It makes it very easy to do and it appears to be opensource.
bermi[arroba]akelos.com
04-Oct-2002 08:27
Here is a link to a class for creating and reading zip files
http://www.phpconcept.net/pclzip/
guidod at gmx dot de
17-Apr-2002 01:22
- look at - http://zipios.sf.net -
it is a C++ class that can also write zip files.
it is modelled after the resp. java interface.

Please also note that I (Guido Draheim) can not
answer questions on compiling zziplib-support
into php, I did not add it, and I do not know
anything about the php module interface. If you
find bugs or have suggestions for more features
then I would be pleased to hear about it. TIA, guido
fbiggun at hotmail dot com
04-Jan-2002 05:29
Check out these pages on the Zend Web Site. The author of the zipfile class explains how his class runs!

Have fun ;)

http://www.zend.com/zend/spotlight/creating-zip-files1.php
http://www.zend.com/zend/spotlight/creating-zip-files2.php
http://www.zend.com/zend/spotlight/creating-zip-files3.php
vangoethem at hotmail dot com
28-Dec-2001 03:51
If you are looking for a way to create ZIP files dynamically in PHP, you should look at the wonderful zipfile class.
It seems there is no official page for this class. You may get it by retrieving the zip.lib.php from the PhpMyAdmin 2.5.2:
http://www.phpmyadmin.net/

<yp_orderzip_close>
 Last updated: Thu, 21 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: http://php.mirrors.ilisys.com.au/
Last updated: Sat 01 Nov 2003 04:13:36 EST EST