note 34224 deleted from ref.zip by danbrown
| From: | danbrown@php.net | Date: | Sun, 25 Oct 2009 13:19:14 +0000 |
| Subject: | note 34224 deleted from ref.zip by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162080@lists.php.net to get a copy of this message | ||
Note Submitter: phpContrib (A T) esurfers d o t c o m
----
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_entry, zip_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/