©
本文档使用
php.cn手册 发布
(PHP 5 >= 5.3.0, PECL zip >= 1.9.0)
ZipArchive::addGlob — Add files from a directory by glob pattern
$pattern
[, int $flags
= 0
[, array $options
= array()
]] )
Add files from a directory which match the glob pattern
.
pattern
A glob() pattern against which files will be matched.
flags
A bit mask of glob() flags.
options
An associative array of options. Available options are:
"add_path"
Prefix to prepend when translating to the local path of the file within the archive. This is applied after any remove operations defined by the "remove_path" or "remove_all_path" options.
"remove_path"
Prefix to remove from matching file paths before adding to the archive.
"remove_all_path"
TRUE
to use the file name only and add to the root of the archive.
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
Example #1 ZipArchive::addGlob() example
Add all php scripts and text files from current working directory
<?php
$zip = new ZipArchive ();
$ret = $zip -> open ( 'application.zip' , ZipArchive :: OVERWRITE );
if ( $ret !== TRUE ) {
printf ( 'Failed with code %d' , $ret );
} else {
$options = array( 'add_path' => 'sources/' , 'remove_all_path' => TRUE );
$zip -> addGlob ( '*.{php,txt}' , GLOB_BRACE , $options );
$zip -> close ();
}
?>
[#1] sumariva at gmail dot com [2015-04-07 13:40:21]
As others suggested, to remove all paths, use the space as basepath.
Zip opened with builtin utility on a Windows XP and WinRAR.
$options = array('add_path' => ' ','remove_all_path' => TRUE);
Thanks for all contributers.
[#2] johnsmith at na dot com [2015-02-19 03:36:12]
Neither 'remove_all_path' or 'remove_path' options seem to be workng
[#3] zckernel at gmail dot com [2015-02-13 12:52:31]
Doesn't work with the following options:
$options = array('remove_all_path' => TRUE);
$zipArchive->addGlob($path."
?>
[EDIT BY danbrown AT php DOT net: Added $zip-close() per indication by original poster in follow-up note on 18-APR-2010.]
[#7] Anonymous [2009-12-02 16:38:37]
I am using this function to extract a specific folder and it's contents from a zip file:
<?php
function extractDir($zipfile, $path) {
if (file_exists($zipfile)) {
$files = array();
$zip = new ZipArchive;
if ($zip->open($zipfile) === TRUE) {
for($i = 0; $i < $zip->numFiles; $i++) {
$entry = $zip->getNameIndex($i);
//Use strpos() to check if the entry name contains the directory we want to extract
if (strpos($entry, "/MyFolder/")) {
//Add the entry to our array if it in in our desired directory
$files[] = $entry;
}
}
//Feed $files array to extractTo() to get only the files we want
if ($zip->extractTo($path, $files) === TRUE) {
return TRUE;
} else {
return FALSE;
}
$zip->close();
} else {
return FALSE;
}
} else {
return FALSE;
}
}
//Run the function
if (extractDir($zipfile, $path)) {
$extracted = "YES! :-D";
} else {
$extracted = "NO! :*(";
}
echo $extracted;
?>
[#8] kawzaki at yahoo dot com [2009-05-31 21:28:05]
Please be aware of the fact that using this function has OVERWRITE true.
an old file will be overwritten if the achieve (zipped file) contains file matching the same old file name.
old files that has no match in the zip, will be kept as is.
hopefully the someone will explain how to avoid overwriting old files.
[#9] Anonymous [2009-04-02 13:50:52]
I found it useful to add this to a function.
<?php
function zip_extract($file, $extractPath) {
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
$zip->extractTo($extractPath);
$zip->close();
return TRUE;
} else {
return FALSE;
}
} // end function
?>
[#10] Kaya [2008-10-03 01:36:01]
Make attention when using this function with apache & windows system. Windows file system use \ (backslash) instead of unix / (slash)
Use str_replace like this.
<?php
$zip = new ZipArchive;
if ($zip->open("file.zip")){
$path = getcwd() . "/dirToextract/";
$path = str_replace("\\","/",$path);
echo $path;
echo $zip->extractTo($path);
$zip->close();
echo 'Done.';
} else {
echo "Error";
}
?>
[#11] tBone [2008-06-02 17:03:18]
This function, at least from my experience, maintains/forces the directory structure within the ZIP file.
ie. if you have FOLDER1/File1.txt in the zip file and you use
$zip->extractTo('/extract', 'FOLDER1/File1.txt');
the location of the extracted file will be:
/extract/FOLDER1/File1.txt
[#12] DerkaDerka [2007-03-05 22:48:12]
This function will overwrite destination files with the same name.