note 102340 added to function.ziparchive-addfile
| From: | DavidHunt at osu1 dot php dot net | Date: | Wed, 09 Feb 2011 17:12:02 +0000 |
| Subject: | note 102340 added to function.ziparchive-addfile | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-176815@lists.php.net to get a copy of this message | ||
RE: aartdebruijn Note on 1024 file limit when adding files to archive.
Simple solution for creating large archives - break the file into parts during the loop.
/*
(scalar) $zipFile - full path to zip file
(scalar) $filesPath - base path of files list
(array) $filesList - list of files relative to base path
*/
function create_zip( $zipFile, $filesPath, $filesList, $ignoreError=false ){
// Max Timeout
ini_set('max_execution_time', 300);
// Handler
$zip = new ZipArchive();
// Create Archive
if ($zip->open($zipFile, ZIPARCHIVE::CREATE) !== true) {
return 'Failed to create archive';
}
// Append Files to Archive
// Close / Open after Max to overcome server Open File Limits
$loop_inc = 0; $loop_max = 200;
foreach($filesList AS $fn) {
$fp = $filesPath.$fn;
if( file_exists($fp) && is_readable($fp) ){
// Add file to archive
if( $zip->addFile($fp, $fn) !== true )
return 'Failed to add file to archive: '.$fn;
// If Max Reached - Close / Open
if( $loop_inc++ > $loop_max ){
$zip->close();
if( $zip->open( $zipFile ) !== true ){
return 'Failed to open archive for append';
}
$loop_inc = 0;
}
} elseif( !$ignoreError )
return 'Failed to add file to archive: '.$fn;
}
// Close
$zip->close();
return true;
}
/* Example */
$res = create_zip(
'/home/username/www/myfile.zip',
'/home/username/www/images/',
array( 'image1.jpg', 'folder/image2.jpg' )
);
if( $res !== true ) echo 'Whoops! '.$res;
----
Server IP: 69.147.83.197
Probable Submitter: 71.204.87.158
----
Manual Page -- http://www.php.net/manual/en/function.ziparchive-addfile.php
Edit -- https://master.php.net/note/edit/102340
Del: integrated -- https://master.php.net/note/delete/102340/integrated
Del: useless -- https://master.php.net/note/delete/102340/useless
Del: bad code -- https://master.php.net/note/delete/102340/bad+code
Del: spam -- https://master.php.net/note/delete/102340/spam
Del: non-english -- https://master.php.net/note/delete/102340/non-english
Del: in docs -- https://master.php.net/note/delete/102340/in+docs
Del: other reasons-- https://master.php.net/note/delete/102340
Reject -- https://master.php.net/note/reject/102340
Search -- https://master.php.net/manage/user-notes.php