I wrote a simple function to export a zip file to an array. I tested it and works great to up to 1MB archives. (Didn't need it for larger files.)
I use it to upload files to my server in a zip and have it directly extracted along with directory structure.
This function will attempt to use unzip functions in php otherwise will use PCLzip. Instructions and the functions are explained on http://www.tellinya.com/art2/20/unzip-in-php . The code is quite short but effective. Test it as advised on site to unserstand the format it extracts.
And any bug please mention in the comment form as I rely on this code and I'll address them ASAP. This will satisfy the needs of 95% of those that do unzips on the site and will help those with unzip functions not enabled as PCPLzip will handle it as well.
Cheers and n-joy.
CLXXXVII. Zip ファイル関数
導入
この拡張モジュールにより、ZIP 圧縮されたアーカイブとその内部のファイルに対する透過的な読み書きが可能となります。
要件
PHP 4 に同梱されているバージョンは、Guido Draheim 氏による » ZZIPlib バージョン 0.10.6 以降を必要とします。
この拡張モジュールは、Jean-loup Gailly 氏と Mark Adler 氏による » zlib の機能を利用します。
インストール手順
注意: PHP 4.1.0 以前の Zip サポートは実験的なものです。
PHP 4 の zip 拡張モジュールはメンテナンスされていませんので、 私たちはバンドルされているものよりも PECL 拡張モジュールの利用を推奨します。
これらの関数を使用するには、zip サポートを有効にして PHP をコンパイルしなければなりません。そのためには、設定オプション --with-zip[=DIR] を使用します。 [DIR] は、» ZZIPlib ライブラリのインストールされている場所です。
この PECL 拡張モジュールをインストールする方法は、 マニュアルの PECL 拡張モジュールのインストール という章にあります。 新規リリース・ダウンロード・ソースファイル・管理者情報・CHANGELOG といった関連する情報については、次の場所にあります。 » http://pecl.php.net/package/zip.
この PECL 拡張モジュール用の DLL は、» PHP のダウンロード ページあるいは » http://pecl4win.php.net/ からダウンロードできます。
PHP 4 の場合、この DLL は PHP の Windows ダウンロードバイナリの extensions/ ディレクトリ にあります。
実行時設定
設定ディレクティブは定義されていません。
リソース型
Zip モジュールでは二種類のリソース型が使用されます。 まず最初が Zip アーカイブを表す Zip directory で、 もうひとつはアーカイブのエントリを表す Zip Entry です。
定義済み定数
以下の定数が定義されています。 この関数の拡張モジュールが PHP 組み込みでコンパイルされているか、 実行時に動的にロードされている場合のみ使用可能です。
ZipArchive はクラス定数を使用します。定数には フラグ (FL_)、エラー (ER_) あるいはモード (接頭辞なし) の三種類があります。
- ZIPARCHIVE::CREATE (integer)
- アーカイブが存在しない場合に、作成します。
- ZIPARCHIVE::OVERWRITE (integer)
- 常に新しいアーカイブを開始します。このモードは、 ファイルが既に存在する場合にはそれを上書きします。
- ZIPARCHIVE::EXCL (integer)
- アーカイブが既に存在する場合はエラーとします。
- ZIPARCHIVE::CHECKCONS (integer)
- アーカイブの一貫性チェックを別途行い、 失敗した場合はエラーとします。
- ZIPARCHIVE::FL_NOCASE (integer)
- 名前で検索する際に大文字小文字を区別しません。
- ZIPARCHIVE::FL_NODIR (integer)
- ディレクトリ要素を無視します。
- ZIPARCHIVE::FL_COMPRESSED (integer)
- 圧縮されたデータを読み込みます。
- ZIPARCHIVE::FL_UNCHANGED (integer)
- 元のデータを使用し、変更内容を無視します。
- ZIPARCHIVE::CM_DEFAULT (integer)
- 圧縮あるいは保存のどちらか有効なほうを実行します。
- ZIPARCHIVE::CM_STORE (integer)
- 保存します (圧縮しません)。
- ZIPARCHIVE::CM_SHRINK (integer)
- 圧縮します。
- ZIPARCHIVE::CM_REDUCE_1 (integer)
- reduced with factor 1
- ZIPARCHIVE::CM_REDUCE_2 (integer)
- reduced with factor 2
- ZIPARCHIVE::CM_REDUCE_3 (integer)
- reduced with factor 3
- ZIPARCHIVE::CM_REDUCE_4 (integer)
- reduced with factor 4
- ZIPARCHIVE::CM_IMPLODE (integer)
- imploded
- ZIPARCHIVE::CM_DEFLATE (integer)
- deflated
- ZIPARCHIVE::CM_DEFLATE64 (integer)
- deflate64
- ZIPARCHIVE::CM_PKWARE_IMPLODE (integer)
- PKWARE 方式。
- ZIPARCHIVE::CM_BZIP2 (integer)
- BZIP2 アルゴリズム。
- ZIPARCHIVE::ER_OK (integer)
- エラーはありません。
- ZIPARCHIVE::ER_MULTIDISK (integer)
- 複数ディスクの zip アーカイブはサポートされません。
- ZIPARCHIVE::ER_RENAME (integer)
- 一時ファイルの名前変更に失敗しました。
- ZIPARCHIVE::ER_CLOSE (integer)
- zip アーカイブのクローズに失敗しました。
- ZIPARCHIVE::ER_SEEK (integer)
- シークエラー。
- ZIPARCHIVE::ER_READ (integer)
- 読み込みエラー。
- ZIPARCHIVE::ER_WRITE (integer)
- 書き込みエラー。
- ZIPARCHIVE::ER_CRC (integer)
- CRC エラー。
- ZIPARCHIVE::ER_ZIPCLOSED (integer)
- zip アーカイブはクローズされました。
- ZIPARCHIVE::ER_NOENT (integer)
- そのファイルはありません。
- ZIPARCHIVE::ER_EXISTS (integer)
- ファイルが既に存在します。
- ZIPARCHIVE::ER_OPEN (integer)
- ファイルをオープンできません。
- ZIPARCHIVE::ER_TMPOPEN (integer)
- 一時ファイルの作成に失敗しました。
- ZIPARCHIVE::ER_ZLIB (integer)
- Zlib エラー。
- ZIPARCHIVE::ER_MEMORY (integer)
- メモリの確保に失敗しました。
- ZIPARCHIVE::ER_CHANGED (string)
- エントリが変更されました。
- ZIPARCHIVE::ER_COMPNOTSUPP (integer)
- 圧縮方式がサポートされていません。
- ZIPARCHIVE::ER_EOF (integer)
- 予期せぬ EOF です。
- ZIPARCHIVE::ER_INVAL (integer)
- 無効な引数です。
- ZIPARCHIVE::ER_NOZIP (integer)
- zip アーカイブではありません。
- ZIPARCHIVE::ER_INTERNAL (integer)
- 内部エラー。
- ZIPARCHIVE::ER_INCONS (integer)
- 矛盾した Zip アーカイブです。
- ZIPARCHIVE::ER_REMOVE (integer)
- ファイルを削除できません。
- ZIPARCHIVE::ER_DELETED (integer)
- エントリが削除されました。
例
例 2611. Zip アーカイブの作成
<?php
$zip = new ZipArchive();
$filename = "./test112.zip";
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as testfilephp.txt.\n");
$zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test string added as testfilephp2.txt.\n");
$zip->addFile($thisdir . "/too.php","/testfromfile.php");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
?>
例 2612. アーカイブの詳細の出力および一覧表示
<?php
$za = new ZipArchive();
$za->open('test_with_comment.zip');
print_r($za);
var_dump($za);
echo "numFiles: " . $za->numFiles . "\n";
echo "status: " . $za->status . "\n";
echo "statusSys: " . $za->statusSys . "\n";
echo "filename: " . $za->filename . "\n";
echo "comment: " . $za->comment . "\n";
for ($i=0; $i<$za->numFiles;$i++) {
echo "index: $i\n";
print_r($za->statIndex($i));
}
echo "numFile:" . $za->numFiles . "\n";
?>
例 2613. Zip ストリームラッパーによる OpenOffice メタ情報の読み込み
<?php
$reader = new XMLReader();
$reader->open('zip://' . dirname(__FILE__) . '/test.odt#meta.xml');
$odt_meta = array();
while ($reader->read()) {
if ($reader->nodeType == XMLREADER::ELEMENT) {
$elm = $reader->name;
} else {
if ($reader->nodeType == XMLREADER::END_ELEMENT && $reader->name == 'office:meta') {
break;
}
if (!trim($reader->value)) {
continue;
}
$odt_meta[$elm] = $reader->value;
}
}
print_r($odt_meta);
?>
この例は旧 API (PHP 4 用) を使用します。まず ZIP ファイルアーカイブをオープンし、アーカイブ内の各ファイルを読み込み、 その内容を出力します。この例で使用するアーカイブ test2.zip は、ZZIPlib のソース配布物に含まれているテスト用アーカイブのひとつです。
例 2614. Zip の使用例
<?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);
}
?>
目次
- zip_close — ZIP ファイルアーカイブを閉じる
- zip_entry_close — ディレクトリエントリを閉じる
- zip_entry_compressedsize — ディレクトリエントリの圧縮時のサイズを取得する
- zip_entry_compressionmethod — ディレクトリエントリの圧縮方法を取得する
- zip_entry_filesize — ディレクトリエントリの実際のファイルサイズを取得する
- zip_entry_name — ディレクトリエントリの名前を取得する
- zip_entry_open — 読込み用にディレクトリエントリをオープンする
- zip_entry_read — オープンされたディレクトリエントリから読み込む
- zip_open — Zip ファイルアーカイブをオープンする
- zip_read — Zip ファイルアーカイブの中の次のエントリを読み込む
- ZipArchive::addEmptyDir — 新しいディレクトリを追加する
- ZipArchive::addFile — 指定したパスからファイルを ZIP アーカイブに追加する
- ZipArchive::addFromString — その内容を指定して、ファイルを ZIP アーカイブに追加する
- ZipArchive::close — アクティブな (オープンされた、あるいは新しく作成された) アーカイブを閉じる
- ZipArchive::deleteIndex — インデックスを使用して、アーカイブ内のエントリを削除する
- ZipArchive::deleteName — 名前を使用して、アーカイブからエントリを削除する
- ZipArchive::extractTo — アーカイブの内容を展開する
- ZipArchive::getArchiveComment — ZIP アーカイブのコメントを返す
- ZipArchive::getCommentIndex — エントリのインデックスを使用して、エントリのコメントを返す
- ZipArchive::getCommentName — エントリ名を使用して、エントリのコメントを返す
- ZipArchive::getFromIndex — インデックスを使用して、エントリの内容を返す
- ZipArchive::getFromName — 名前を使用して、エントリの内容を返す
- ZipArchive::getNameIndex — インデックスを使用して、エントリの名前を返す
- ZipArchive::getStream — 名前を使用して、エントリのファイルハンドラ (読み込み専用) を取得する
- ZipArchive::locateName — アーカイブ内のエントリのインデックスを返す
- ZipArchive::open — ZIP ファイルアーカイブをオープンする
- ZipArchive::renameIndex — インデックスを使用してエントリ名を変更する
- ZipArchive::renameName — 名前を使用してエントリ名を変更する
- ZipArchive::setArchiveComment — ZIP アーカイブのコメントを設定する
- ZipArchive::setCommentIndex — インデックスを使用してエントリのコメントを設定する
- ZipArchive::setCommentName — 名前を使用してエントリのコメントを設定する
- ZipArchive::statIndex — インデックスを使用してエントリの詳細を取得する
- ZipArchive::statName — 名前を使用してエントリの詳細を取得する
- ZipArchive::unchangeAll — アーカイブに対するすべての変更を取り消す
- ZipArchive::unchangeArchive — アーカイブ全体に対して行われたすべての変更を取り消す
- ZipArchive::unchangeIndex — 指定したインデックスのエントリに対するすべての変更を取り消す
- ZipArchive::unchangeName — 指定した名前のエントリに対するすべての変更を取り消す
Zip ファイル関数
25-Aug-2007 03:10
26-Jun-2007 02:59
I made a zip stream handler in case your distribution does not have the built in one using the new ZipArchive system. This one also features the ability to grab entries by index as well as by name. It is similar in capabilities to the builtin gzip/bzip2 compression stream handlers (http://us2.php.net/manual/en/wrappers.compression.php) except it does not support writing.
To use:
fopen('zip://absolute/path/to/file.zip?entryname', $mode) or
fopen('zip://absolute/path/to/file.zip#entryindex', $mode) or
fopen('zip://absolute/path/to/file.zip', $mode)
$mode can only be 'r' or 'rb'. In the last case the first entry in the zip file is used.
<?php
class ZipStream {
public $zip; //the zip file
public $entry; //the opened zip entry
public $length; //the uncompressed size of the zip entry
public $position; //the current position in the zip entry read
//Opens the zip file then retrieves and opens the entry to stream
public function stream_open($path, $mode, $options, &$opened_path) {
if ($mode != 'r' && $mode != 'rb') //only accept r and rb modes, no writing!
return false;
$path = 'file:///'.substr($path, 6); //switch out file:/// for zip:// so we can use url_parse
$url = parse_url($path);
//open the zip file
$filename = $url['path'];
$this->zip = zip_open($filename);
if (!is_resource($this->zip))
return false;
//if entry name is given, find that entry
if (array_key_exists('query', $url) && $url['query']) {
$path = $url['query'];
do {
$this->entry = zip_read($this->zip);
if (!is_resource($this->entry))
return false;
} while (zip_entry_name($this->entry) != $path);
} else { //otherwise get it by index (default to 0)
$id = 0;
if (array_key_exists('fragment', $url) && is_int($url['fragment']))
$id = $url['fragment']*1;
for ($i = 0; $i <= $id; $i++) {
$this->entry = zip_read($this->zip);
if (!is_resource($this->entry))
return false;
}
}
//setup length and open the entry for reading
$this->length = zip_entry_filesize($this->entry);
$this->position = 0;
zip_entry_open($this->zip, $this->entry, $mode);
return true;
}
//Closes the zip entry and file
public function stream_close() { @zip_entry_close($this->entry); @zip_close($this->zip); }
//Returns how many bytes have been read from the zip entry
public function stream_tell() { return $this->position; }
//Returns true if the end of the zip entry has been reached
public function stream_eof() { return $this->position >= $this->length; }
//Returns the stat array, only 'size' is filled in with the uncompressed zip entry size
public function url_stat() { return array('dev'=>0, 'ino'=>0, 'mode'=>0, 'nlink'=>0, 'uid'=>0, 'gid'=>0, 'rdev'=>0, 'size'=>$this->length, 'atime'=>0, 'mtime'=>0, 'ctime'=>0, 'blksize'=>0, 'blocks'=>0); }
//Reads the next $count bytes or until the end of the zip entry. Returns the data or false if no data was read.
public function stream_read($count) {
$this->position += $count;
if ($this->position > $this->length)
$this->position = $this->length;
return zip_entry_read($this->entry, $count);
}
}
//Register the zip stream handler
stream_wrapper_register('zip', 'ZipStream'); //if this fails there is already a zip stream handler and we will just use that one
?>
08-Jun-2007 08:40
Lennart Poot's example of using phpMyAdmin's zip library is missing the echo that will actually output the final zip file.
At the end (but before the exit which isn't needed anyway), add this line:
echo $dump_buffer;
15-May-2007 08:19
Hi,
i modified the function from nielsvandenberge, now you
can also use relative path's, i added also error messages
and another small highlights, have fun.
<?php
/**
* Unzip the source_file in the destination dir
*
* @param string The path to the ZIP-file.
* @param string The path where the zipfile should be unpacked, if false the directory of the zip-file is used
* @param boolean Indicates if the files will be unpacked in a directory with the name of the zip-file (true) or not (false) (only if the destination directory is set to false!)
* @param boolean Overwrite existing files (true) or not (false)
*
* @return boolean Succesful or not
*/
function unzip($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true)
{
if(function_exists("zip_open"))
{
if(!is_resource(zip_open($src_file)))
{
$src_file=dirname($_SERVER['SCRIPT_FILENAME'])."/".$src_file;
}
if (is_resource($zip = zip_open($src_file)))
{
$splitter = ($create_zip_name_dir === true) ? "." : "/";
if ($dest_dir === false) $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter))."/";
// Create the directories to the destination dir if they don't already exist
create_dirs($dest_dir);
// For every file in the zip-packet
while ($zip_entry = zip_read($zip))
{
// Now we're going to create the directories in the destination directories
// If the file is not in the root dir
$pos_last_slash = strrpos(zip_entry_name($zip_entry), "/");
if ($pos_last_slash !== false)
{
// Create the directory where the zip-entry should be saved (with a "/" at the end)
create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1));
}
// Open the entry
if (zip_entry_open($zip,$zip_entry,"r"))
{
// The name of the file to save on the disk
$file_name = $dest_dir.zip_entry_name($zip_entry);
// Check if the files should be overwritten or not
if ($overwrite === true || $overwrite === false && !is_file($file_name))
{
// Get the content of the zip entry
$fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
if(!is_dir($file_name))
file_put_contents($file_name, $fstream );
// Set the rights
if(file_exists($file_name))
{
chmod($file_name, 0777);
echo "<span style=\"color:#1da319;\">file saved: </span>".$file_name."<br />";
}
else
{
echo "<span style=\"color:red;\">file not found: </span>".$file_name."<br />";
}
}
// Close the entry
zip_entry_close($zip_entry);
}
}
// Close the zip-file
zip_close($zip);
}
else
{
echo "No Zip Archive Found.";
return false;
}
return true;
}
else
{
if(version_compare(phpversion(), "5.2.0", "<"))
$infoVersion="(use PHP 5.2.0 or later)";
echo "You need to install/enable the php_zip.dll extension $infoVersion";
}
}
function create_dirs($path)
{
if (!is_dir($path))
{
$directory_path = "";
$directories = explode("/",$path);
array_pop($directories);
foreach($directories as $directory)
{
$directory_path .= $directory."/";
if (!is_dir($directory_path))
{
mkdir($directory_path);
chmod($directory_path, 0777);
}
}
}
}
?>
greetings darki777
11-May-2007 06:29
This is the function I use to unzip a file.
It includes the following options:
* Unzip in any directory you like
* Unzip in the directory of the zip file
* Unzip in a directory with the zipfile's name in the directory of the zip file. (i.e.: C:\test.zip will be unzipped in C:\test\)
* Overwrite existing files or not
* It creates non existing directories with the function Create_dirs($path)
You should use absolute paths with slashes (/) instead of backslashes (\).
I tested it with PHP 5.2.0 with php_zip.dll extension loaded
<?php
/**
* Unzip the source_file in the destination dir
*
* @param string The path to the ZIP-file.
* @param string The path where the zipfile should be unpacked, if false the directory of the zip-file is used
* @param boolean Indicates if the files will be unpacked in a directory with the name of the zip-file (true) or not (false) (only if the destination directory is set to false!)
* @param boolean Overwrite existing files (true) or not (false)
*
* @return boolean Succesful or not
*/
function unzip($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true)
{
if ($zip = zip_open($src_file))
{
if ($zip)
{
$splitter = ($create_zip_name_dir === true) ? "." : "/";
if ($dest_dir === false) $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter))."/";
// Create the directories to the destination dir if they don't already exist
create_dirs($dest_dir);
// For every file in the zip-packet
while ($zip_entry = zip_read($zip))
{
// Now we're going to create the directories in the destination directories
// If the file is not in the root dir
$pos_last_slash = strrpos(zip_entry_name($zip_entry), "/");
if ($pos_last_slash !== false)
{
// Create the directory where the zip-entry should be saved (with a "/" at the end)
create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1));
}
// Open the entry
if (zip_entry_open($zip,$zip_entry,"r"))
{
// The name of the file to save on the disk
$file_name = $dest_dir.zip_entry_name($zip_entry);
// Check if the files should be overwritten or not
if ($overwrite === true || $overwrite === false && !is_file($file_name))
{
// Get the content of the zip entry
$fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
file_put_contents($file_name, $fstream );
// Set the rights
chmod($file_name, 0777);
echo "save: ".$file_name."<br />";
}
// Close the entry
zip_entry_close($zip_entry);
}
}
// Close the zip-file
zip_close($zip);
}
}
else
{
return false;
}
return true;
}
/**
* This function creates recursive directories if it doesn't already exist
*
* @param String The path that should be created
*
* @return void
*/
function create_dirs($path)
{
if (!is_dir($path))
{
$directory_path = "";
$directories = explode("/",$path);
array_pop($directories);
foreach($directories as $directory)
{
$directory_path .= $directory."/";
if (!is_dir($directory_path))
{
mkdir($directory_path);
chmod($directory_path, 0777);
}
}
}
}
// Extract C:/zipfiletest/zip-file.zip to C:/zipfiletest/zip-file/ and overwrites existing files
unzip("C:/zipfiletest/zip-file.zip", false, true, true);
// Extract C:/zipfiletest/zip-file.zip to C:/another_map/zipfiletest/ and doesn't overwrite existing files. NOTE: It doesn't create a map with the zip-file-name!
unzip("C:/zipfiletest/zip-file.zip", "C:/another_map/zipfiletest/", true, false);
?>
01-May-2007 04:55
Hi, all
There are lot of functions given below which etracts files, but what they lack is setting file permissions. On some servers file permissions are very important and the script cease to work after creating first directory, So I have added chmod to the code. There is only one limitation to the code, files without file extension are neither treated as files or directories so they are not chmoded, anyway this does not affect the code. Hope this helps.
<?php
function unpackZip($dir,$file) {
if ($zip = zip_open($dir.$file.".zip")) {
if ($zip) {
mkdir($dir.$file);
chmod($dir.$file, 0777);
while ($zip_entry = zip_read($zip)) {
if (zip_entry_open($zip,$zip_entry,"r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
$dir_name = dirname(zip_entry_name($zip_entry));
if ($dir_name != ".") {
$dir_op = $dir.$file."/";
foreach ( explode("/",$dir_name) as $k) {
$dir_op = $dir_op . $k;
if (is_file($dir_op)) unlink($dir_op);
if (!is_dir($dir_op)) mkdir($dir_op);
chmod($dir_op, 0777);
$dir_op = $dir_op . "/" ;
}
}
$fp=fopen($dir.$file."/".zip_entry_name($zip_entry),"w+");
chmod($dir.$file."/".zip_entry_name($zip_entry), 0777);
fwrite($fp,$buf);
fclose($fp);
zip_entry_close($zip_entry);
} else
return false;
}
zip_close($zip);
}
} else
return false;
return true;
}
$dir = $_SERVER['DOCUMENT_ROOT']."/"."destdirectory/";
$file = 'zipfilename_without_extension';
unpackZip($dir,$file);
$print = $_SERVER['DOCUMENT_ROOT'];
?>
19-Oct-2006 04:19
I've needed a simple php class to manipulate zip files without PCEL extension, after reading all comment and tested 2 nice classes, here's my conclusion:
"You can use PHPMyAdmin's zip.lib"
-- Lennart Poot -- 12-Jul-2006 07:33
true, getting it is a bit awkward but the class is straight foward and works. BUT it only create zip files, it wont extract them.
"Hey guys, maybe you should check this out:
http://www.phpconcept.net/pclzip/index.en.php "
-- master_auer at web dot de -- 16-Nov-2003 07:49
true, check it if you need more advanced feature, this class is awsome.
18-Oct-2006 04:14
This will simply unpack (including directories) $zip to $dir -- in this example the zip is being uploaded.
<?php
$dir = 'C:\\reports-temp\\';
$zip = zip_open($_FILES['report_zip']['tmp_name']);
while($zip_entry = zip_read($zip)) {
$entry = zip_entry_open($zip,$zip_entry);
$filename = zip_entry_name($zip_entry);
$target_dir = $dir.substr($filename,0,strrpos($filename,'/'));
$filesize = zip_entry_filesize($zip_entry);
if (is_dir($target_dir) || mkdir($target_dir)) {
if ($filesize > 0) {
$contents = zip_entry_read($zip_entry, $filesize);
file_put_contents($dir.$filename,$contents);
}
}
}
?>
07-Oct-2006 12:39
Notes from phpContrib [ a t ] eSurfers D o t COM are a ROCK!!!
1) In order to make it work U'll probably need:
** Allow Apache service to interact with desktop, but in that case U will notice momentarely console window opens and closes. That's may be annoying. The elegant solution instead of allowing interaction is redirect the command to some temporary file, and get rid of it later on:
define('UNZIP_CMD','unzip -o @_SRC_@ -x -d
@_DST_@ > tmpfile.txt');
2) I would recommend to use 7-zip, it's free and completely relyable. Also, it's comes with stand alone console executable which makes it quite convenient to place it in some exec library folder within your source, and refferering directly to it.
define("UNZIP_CMD","7za.exe x @_SRC_@ -o@_DST_@ > tmpfile.txt");
Previous poster's code is not using escape_shell_arg(), so it can be a huge security hole if attacker could manipulate filename/dir. I suggest replacing str_replace() with sprintf() and escape_shell_arg().
05-Sep-2006 06:35
1) A warning to all users of PHP Zipping/Unzipping Classes: most of them require the whole content of the ZIP file and/or the unzipped contents to stay in memory. They work very well, but might fail with very large files. Keep this in mind before starting development of a project involving large ZIP files.
2) If all you need is to unzip files to a folder, and your server has a zip command line tool, all you might need is a function like this:
if(!defined('UNZIP_CMD')) define('UNZIP_CMD','unzip -o @_SRC_@ -x -d
@_DST_@');
function UnzipAllFiles($zipFile,$zipDir) {
$unzipCmd=UNZIP_CMD;
$unzipCmd=str_replace('@_SRC_@',$zipFile,$unzipCmd);
$unzipCmd=str_replace('@_DST_@',$zipDir,$unzipCmd);
$res=-1; // any nonzero value
$UnusedArrayResult=array();
$UnusedStringResult=exec($unzipCmd,$UnusedArrayResult,$res);
return ($res==0);
}
// * * * You DO NOT NEED the ZIP lib or M-ZIP to use this, but you will need the tool M-ZIP uses (unzip.exe or unzip for linux). * * *
// Use it this way:
$zipFile='/path/to/uploaded_file.zip';
$whereToUnzip='/path/to/writable/folder/';
$result=UnzipAllFiles($zipFile,$whereToUnzip);
if($result===FALSE) echo('FAILED');
else echo('SUCCESS');
By changing UNZIP_CMD (the same constant as in the m-zip lib) you can
of course untar, unarc, unrar, etc... provided you have the correct
unwhatever command or unwhatever.exe executable and write its correct syntax in UNZIP_CMD.
To see details of what tools you need and where to put them (or to use a syntax compatible to the standard zip lib) see http://eSurfers.com/m-zip/
12-Jul-2006 08:33
You can use PHPMyAdmin's zip.lib library. It's available in the package PHPMyAdmin ( http://www.phpmyadmin.net/ ).
The zip class is located in /libraries/zip.lib.php
Next example generates a file named 'subfolder_in_zip_archive/_settings.php' using information of variable $settings. All files in folder 'folder_holding_files_to_add_to_zip' ($ad_dir) will be added to the archive as well.
In this example the raw zip archive data is responded to the browser. Uncomment the lines after 'write the file to disk' if you would like the archive to be stored on disk.
The function $zipfile -> addFile requires two parameters.
- The first parameter holds raw data and needs to be escaped using php function ‘stripcslashes’
- The second parameter holds the filename in the archive. You can enter complete paths to create complex folder structures
A timestamp can be added using the third additional parameter.
Regars,
Lennart Poot
<?php
require_once('zip.lib.php');
$zipfilename = 'zip_archive_to_generate.zip';
$zip_subfolder = 'subfolder_in_zip_archive';
$settings = '
<?php
$setting1 = "'.$setting1.'";
$setting2 = "'.$setting2.'";
?>
';
$ad_dir = 'folder_holding_files_to_add_to_zip';
// form is posted, handle it
$zipfile = new zipfile();
// generate _settings into zip file
$zipfile ->addFile( stripcslashes( $settings ), $zip_subfolder . '/_settings.php' );
if ($handle = opendir($ad_dir)) {
while (false !== ($file = readdir($handle))) {
if (!is_dir($file) && $file != "." && $file != ".." ) {
$f_tmp = @fopen( $ad_dir . '/' . $file, 'r');
if($f_tmp){
$dump_buffer=fread( $f_tmp, filesize($ad_dir . '/' . $file));
$zipfile -> addFile($dump_buffer, $zip_subfolder . '/' . $file);
fclose( $f_tmp );
}
}
}
$dump_buffer = $zipfile -> file();
// write the file to disk:
/*
$file_pointer = fopen('newzip.zip', 'w');
if($file_pointer){
fwrite( $file_pointer, $dump_buffer, strlen($dump_buffer) );
fclose( $file_pointer );
}
*/
// response zip archive to browser:
header('Pragma: public');
header('Content-type: application/zip');
header('Content-length: ' . strlen($dump_buffer));
header('Content-Disposition: attachment; filename="'.$zipfilename.'"');
exit;
?>
12-May-2006 02:27
I needed a quick way to make a zip file from an entire directory including subdirectories. This is downright rough but if someone would fancy it up a bit it works good with the zip class at http://smiledsoft.com/demos/phpzip/ (free version).
Basic use in script would be...
make_archive("the/directory", $zip_archive_object);
<?php
function make_archive( $dir , &$zip, $extdir="")
{
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false ) {
if( $file != "." && $file != ".." )
{
if( is_dir( $dir . $file ) )
{
$zip->add_file($dir.$file,$extdir.$file);
make_archive($dir.$file."/" , $zip, $extdir.$file."/");
}
else
{
$zip->add_file($dir.$file,$extdir.$file);
}
}
}
closedir($dh);
}
}
return true;
}
?>
I try to use the function unpackZip from schore at NOSPAM dot hotmail dot com.
it appears that the fclose instruction is missing. Without that instruction, i've got some trouble to unpack zip file with recursive folders.
Here is the new code with the missing instruction
<?php
function unpackZip($dir,$file) {
if ($zip = zip_open($dir.$file.".zip")) {
if ($zip) {
mkdir($dir.$file);
while ($zip_entry = zip_read($zip)) {
if (zip_entry_open($zip,$zip_entry,"r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
$dir_name = dirname(zip_entry_name($zip_entry));
if ($dir_name != ".") {
$dir_op = $dir.$file."/";
foreach ( explode("/",$dir_name) as $k) {
$dir_op = $dir_op . $k;
if (is_file($dir_op)) unlink($dir_op);
if (!is_dir($dir_op)) mkdir($dir_op);
$dir_op = $dir_op . "/" ;
}
}
$fp=fopen($dir.$file."/".zip_entry_name($zip_entry),"w");
fwrite($fp,$buf);
fclose($fp);
zip_entry_close($zip_entry);
} else
return false;
}
zip_close($zip);
}
} else
return false;
return true;
}
?>
27-Jan-2006 11:06
this function extract all files and subdirectories, you can choose verbose mode for get paths of files extracted. the function return a msg what indicate the error, if msg is OK, all is done.
---
code:
function unzip($dir, $file, $verbose = 0) {
$dir_path = "$dir$file";
$zip_path = "$dir$file.zip";
$ERROR_MSGS[0] = "OK";
$ERROR_MSGS[1] = "Zip path $zip_path doesn't exists.";
$ERROR_MSGS[2] = "Directory $dir_path for unzip the pack already exists, impossible continue.";
$ERROR_MSGS[3] = "Error while opening the $zip_path file.";
$ERROR = 0;
if (file_exists($zip_path)) {
if (!file_exists($dir_path)) {
mkdir($dir_path);
if (($link = zip_open($zip_path))) {
while (($zip_entry = zip_read($link)) && (!$ERROR)) {
if (zip_entry_open($link, $zip_entry, "r")) {
$data = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
$dir_name = dirname(zip_entry_name($zip_entry));
$name = zip_entry_name($zip_entry);
if ($name[strlen($name)-1] == '/') {
$base = "$dir_path/";
foreach ( explode("/", $name) as $k) {
$base .= "$k/";
if (!file_exists($base))
mkdir($base);
}
}
else {
$name = "$dir_path/$name";
if ($verbose)
echo "extracting: $name<br>";
$stream = fopen($name, "w");
fwrite($stream, $data);
}
zip_entry_close($zip_entry);
}
else
$ERROR = 4;
}
zip_close($link);
}
else
$ERROR = "3";
}
else
$ERROR = 2;
}
else
$ERROR = 1;
return $ERROR_MSGS[$ERROR];
}
---
example:
$error = unzip("d:/www/dir/", "zipname", 1);
echo $error;
---
i hope this help you,
good bye.
20-Aug-2005 10:12
i try to find function that will show exists file in zip archive or not. of course i not found it. and so write mine:
first will just check archive for list of files, if not found all files function return FALSE:
function zipx_entries_exists()
{
$names=array();
$args=func_get_args();
$far_size=count($args);
if($args[0])
{
for(; $zip_entry=zip_read($args[0]); $names[]= zip_entry_name($zip_entry));
for($x=1; $x<=$far_size; $t+=in_array($args[$x], $names), $x++);
return $t==--$far_size;
}else{
return 'No zip file in descriptor!';
}
}
example:
$zip=zip_open('any_zip_file_zip');
var_dump(zip_entries_exists($zip, 'photo_1.jpg', 'photo_2.jpg'));
second function will try to find files in zip, if not found it return string with names of not found files with specified delimeter:
function zipx_entries_nonexists_list()
{
$names=array();
$args=func_get_args();
$m=NULL;
$far_size=count($args);
if($args[0])
{
for(; $zip_entry=zip_read($args[0]); $names[]= zip_entry_name($zip_entry));
for($x=2; $x<=$far_size; $m.=(in_array($args[$x], $names) ? NULL : $args[$x].$args[1]), $x++);
return trim($m, $args[1]);
}else{
return 'No zip file in descriptor!';
}
}
example:
$zip=zip_open('any_zip_file_zip');
var_dump(zip_entries_nonexists_list($zip, '<br />', 'photo_1.jpg', 'photo_2.jpg'));
it will return if not found files:
photo_1.jpg<br />photo_2.jpg
28-Jun-2005 01:33
If you just want to unzip a zip folder an alternative to some of the lengthy functions below is:
<?
function unzip($zip_file, $src_dir, $extract_dir)
{
copy($src_dir . "/" . $zip_file, $extract_dir . "/" . $zip_file);
chdir($extract_dir);
shell_exec("unzip $zip_file");
}
?>
You don't need the ZIP extension for this.
28-Apr-2005 12:52
New Unzip function, recursive extract
Require mkdirr() (recursive create dir)
$file = "2537c61ef7f47fc3ae919da08bcc1911.zip";
$dir = getcwd();
function Unzip($dir, $file, $destiny="")
{
$dir .= DIRECTORY_SEPARATOR;
$path_file = $dir . $file;
$zip = zip_open($path_file);
$_tmp = array();
$count=0;
if ($zip)
{
while ($zip_entry = zip_read($zip))
{
$_tmp[$count]["filename"] = zip_entry_name($zip_entry);
$_tmp[$count]["stored_filename"] = zip_entry_name($zip_entry);
$_tmp[$count]["size"] = zip_entry_filesize($zip_entry);
$_tmp[$count]["compressed_size"] = zip_entry_compressedsize($zip_entry);
$_tmp[$count]["mtime"] = "";
$_tmp[$count]["comment"] = "";
$_tmp[$count]["folder"] = dirname(zip_entry_name($zip_entry));
$_tmp[$count]["index"] = $count;
$_tmp[$count]["status"] = "ok";
$_tmp[$count]["method"] = zip_entry_compressionmethod($zip_entry);
if (zip_entry_open($zip, $zip_entry, "r"))
{
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
if($destiny)
{
$path_file = str_replace("/",DIRECTORY_SEPARATOR, $destiny . zip_entry_name($zip_entry));
}
else
{
$path_file = str_replace("/",DIRECTORY_SEPARATOR, $dir . zip_entry_name($zip_entry));
}
$new_dir = dirname($path_file);
// Create Recursive Directory
mkdirr($new_dir);
$fp = fopen($dir . zip_entry_name($zip_entry), "w");
fwrite($fp, $buf);
fclose($fp);
zip_entry_close($zip_entry);
}
echo "\n</pre>";
$count++;
}
zip_close($zip);
}
}
Unzip($dir,$file);
25-Feb-2005 11:52
Zip Class from Devin Doucette at phpclasses.org
Allows the creation of tar, gzip, bzip2, and zip archives, and the extraction of tar, gzip, and bzip2.
Supports relative paths/no paths, comments, and recursing through subdirectories.
Can write file to disk, allow user to download directly, or return file contents in a string or an array.
Does not require any external programs to run.
Here is a link: http://www.phpclasses.org/browse/package/945.html
If (as me) all you wanted to do is store a big string (for example, a serialized array or the like) in a mysql BLOB field, remember that mysql has a COMPRESS() and UNCOMPRESS() pair of functions that do exactly that. Compression/decompression is therefore available also when accessing the DB from other languages like java, etc.
07-Jan-2005 01:19
I slightly changed the extension of Schore that makes function operate more effiecently - less number of itterations. This is most important in dealing with HUGE archives. I also add comment to brief the code. Fits for WIN users.
function unzip($dir,$file) {
$zip = zip_open($dir.$file.".zip"))
if ($zip) {
mkdir($dir.$file);
while ($zip_entry = zip_read($zip)) {
if (zip_entry_open($zip,$zip_entry,"r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
$hostDir = dirname(zip_entry_name($zip_entry));
/* for root "." directory nothing to be done coase root directory was
created before the while-loop statement */
if ($hostDir != ".") {
$absPathToHostDir = $dir.$file."\\";
/* Convrerts path string upon to FMS & OS-WIN configuration */
foreach ( explode("/",$hostDir) as $k)
$absPathToHostDir = $absPathToHostDir . $k . "\\";
$absPathToHostDir = substr($absPathToHostDir,0,-1);
if (is_file($absPathToHostDir))
unlink($absPathToHostDir);
if (!is_dir($absPathToHostDir))
mkdir($absPathToHostDir);
/* Stores Archive entries BOTH as file: Directories & Files;
for this porpose we need these 2 hereabove [IF] checks */
$fp=fopen($dir.$file."\\".zip_entry_name($zip_entry),"w");
fwrite($fp,$buf);
fclose($fp);
zip_entry_close($zip_entry);
}
}
else {
echo "unable open - ";
return false;
}
} // while-loop end
zip_close($zip);
}
else
return false;
return true;
}
14-Dec-2004 03:55
I have had problems with the function of kristiankjaer to unpack zipfiles within any directories.
So I wrote a little extension.
<?php
function unpackZip($dir,$file) {
if ($zip = zip_open($dir.$file.".zip")) {
if ($zip) {
mkdir($dir.$file);
while ($zip_entry = zip_read($zip)) {
if (zip_entry_open($zip,$zip_entry,"r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
$dir_name = dirname(zip_entry_name($zip_entry));
if ($dir_name != ".") {
$dir_op = $dir.$file."/";
foreach ( explode("/",$dir_name) as $k) {
$dir_op = $dir_op . $k;
if (is_file($dir_op)) unlink($dir_op);
if (!is_dir($dir_op)) mkdir($dir_op);
$dir_op = $dir_op . "/" ;
}
}
$fp=fopen($dir.$file."/".zip_entry_name($zip_entry),"w");
fwrite($fp,$buf);
zip_entry_close($zip_entry);
} else
return false;
}
zip_close($zip);
}
} else
return false;
return true;
}
?>
06-Dec-2004 09:06
Here is a function if all you need is to unpack an archive:
function unpackZip($dir,$file) {
$root=$_SERVER['DOCUMENT_ROOT'];
if ($zip = zip_open($root.$dir.$file.".zip")) {
if ($zip) {
mkdir(".".$dir.$file);
while ($zip_entry = zip_read($zip)) {
if (zip_entry_open($zip,$zip_entry,"r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
$fp=fopen(".".$dir.$file."/".zip_entry_name($zip_entry),"w");
fwrite($fp,$buf);
zip_entry_close($zip_entry);
} else
return false;
}
zip_close($zip);
}
} else
return false;
return true;
}
PKZipfiles (or Winzip files) can be easily created by using my Ziplib class, can be found on http://sourceforge.net/projects/phpziplib . All it requires is PHP to be compiled with zlib, and support for classes(don't know when they've been added, but I think with PHP4 you're safe).
24-Jun-2004 08:15
If you don't have the --with-zip=DIR compile option and can't change it, but you do have --with-pear, then you can use the pear Archive_Zip class available at http://cvs.php.net/pear/Archive_Zip/
As of 2004-06-23, the class isn't packaged and auto-documented yet, but like all pear classes, the comments are extremely verbose and helpful, and you can download the php file as a standalone. Just include the Zip.php in your project, and you can use the class.
31-May-2004 05:28
If you want to unzip an password protected file with php..try the following command....it works in Unix/Apache environment...I haven't tested in any other environment...
system("`which unzip` -P Password $zipfile -d $des",$ret_val)
Where $zipfile is the path to the .zip to be unzipped and $des is path to the destination directory.....here both absolute and relative path to the script (which contains this system command) will work...
if everything runs well...file should be unzipped at the $des directory..and you will get 0 value for $ret_val , which means success(info-zip.org)
Regards
Krishnendu
24-Jan-2004 08:28
I have written an OpenSource ZIP2FTP interface, which actually takes a given ZIP file and decompresses it in the folder on an FTP server you specify...
Therefore it may be quite interesting for you people interested in ZIP, its adress is http://zip2ftp.alishomepage.com ; those who directly want the source may visit http://zip2ftp.alishomepage.com/?do=getSource
02-Dec-2003 02:53
As of lately this extension has moved to PECL. You find it at it's new home at http://pecl.php.net/package/zip.
`pear install http://pecl.php.net/get/zip-1.0.tgz ` installs it, use dl('zip.so'); to load it after successful installation.
17-Nov-2003 09:49
Hey guys, maybe you should check this out:
http://www.phpconcept.net/pclzip/index.en.php
A complete library to write and read ZIP files. The only requirement is zLib.
Hope I could help
Regards
Manuel
13-Nov-2003 02:05
For more informations about the ZIP-format visit this document :
http://www.pkware.com/products/enterprise/white_papers/appnote.html
So you be able to write your own ZIP and UNZIP code ... ;o)
Regards
Holger
05-Sep-2003 01: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...
19-Jul-2003 06: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_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/
18-Jun-2003 02: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)
09-Apr-2003 02: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
23-Mar-2003 03: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.
30-Oct-2002 05: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
11-Oct-2002 09: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.
04-Oct-2002 10:27
Here is a link to a class for creating and reading zip files
http://www.phpconcept.net/pclzip/
17-Apr-2002 03: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
04-Jan-2002 06: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
28-Dec-2001 04: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/