Professional Documents
Culture Documents
net sites |
links | my php.net
function list
search for in the
PHP Manual
yp_order zip_close
Function
Reference
Zlib ---
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.
ringu at mail dot ru
20-Aug-2005 01: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
tom
28-Jun-2005 04: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");
}
?>
$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);
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:
http://www.phpclasses.org/browse/package/945.html
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
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).
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
http://cvs.php.net/pear/Archive_Zip/
}
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/unzippe
d/');
The command to invoke to unzip the files:
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 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:
http://www.phpmyadmin.net/
add a note
yp_order zip_close
Last updated: Sat, 27 May 2006
Copyright © 2001-2006 The PHP Group This mirror generously provided by: Directi Web Hosting
All rights reserved. Last updated: Thu Jun 1 07:17:21 2006 GMT