|
|
CXXXIV. Zipファイル関数(読込のみ)
このモジュールにより、ZIP圧縮されたアーカイブととの内部のファイル
を透過的に読み込むことが可能となります。
このモジュールは、Guido Draheimにより作成されたライブラリ
の関数を使用します。
ZZIPlibバージョン >= 0.10.6が必要です。
ZZIPlib は、ZIPの圧縮アルゴリズムの完全な実装で提供される関数のサ
ブセットであり、ZIPファイルアーカイブの読込みのみができることに注
意して下さい。このライブラリで読み込まれるZIPファイルアーカイブを
作成するには、通常のZIPユーティリティが必要です。
PHPにおけるZipサポートは、デフォルトでは使用できません。Zipサポート
を有効にするには、PHPのコンパイル時にconfigureのオプションに
--with-zip[=DIR]
を指定してコンパイルする必要があります。
注意:
Zipサポートは、PHP 4.1.0以前は実験的なものでした。この拡張モジュー
ルは、PHP 4.1.0以降に存在するZIP拡張モジュールを反映しています。
この拡張モジュールは設定ディレクティブを全く定義しません。 この拡張モジュールはリソース型を全く定義しません。
以下の例は、ZIPファイルアーカイブをオープンし、そのアーカイブの中の各
ファイルを読み込んで、その内容を出力するものです。この例で使用されて
いる test2.php アーカイブは、ZZIPlibソース配布に
含まれるテスト用のアーカイブの一つです。
例 1. 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);
}
?>
|
|
Nahaylo Vitalik
25-Feb-2005 02: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:
23-Feb-2005 04:54
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.
Mishania AT ketsujin DOT COM
06-Jan-2005 04: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;
}
schore at NOSPAM dot hotmail dot com
13-Dec-2004 06: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;
}
?>
kristiankjaer AT gmail DOT com
06-Dec-2004 12: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;
}
01-Jul-2004 12:40
PKZipfiles (or Winzip files) can be easily created by using my Ziplib class, can be found on . 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).
php at isaacschlueter dot com
23-Jun-2004 11: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
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.
krishnendu at spymac dot com
31-May-2004 08: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
postmaster at alishomepage dot com
24-Jan-2004 11: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 ; those who directly want the source may visit
Hannes Gassert
02-Dec-2003 05:53
As of lately this extension has moved to PECL. You find it at it's new home at
`pear install ` installs it, use dl('zip.so'); to load it after successful installation.
master_auer at web dot de
17-Nov-2003 12:49
Hey guys, maybe you should check this out:
A complete library to write and read ZIP files. The only requirement is zLib.
Hope I could help
Regards
Manuel
vbwebprofi at gmx dot de
13-Nov-2003 05:05
For more informations about the ZIP-format visit this document :
So you be able to write your own ZIP and UNZIP code ... ;o)
Regards
Holger
Joris P.
04-Sep-2003 04: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
18-Jul-2003 09: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
travis
17-Jun-2003 05: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
08-Apr-2003 05: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 06: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
29-Oct-2002 08: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.
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:
Newbie Von Greenhorn
a1cypher at shaw dot ca
11-Oct-2002 12: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 01:27
Here is a link to a class for creating and reading zip files
guidod at gmx dot de
16-Apr-2002 06:22
- look at - -
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
03-Jan-2002 09:29
Check out these pages on the Zend Web Site. The author of the zipfile class explains how his class runs!
Have fun ;)
vangoethem at hotmail dot com
28-Dec-2001 07: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:
| |