note 97409 added to function.ziparchive-extractto
| From: | cory dot mawhorter at ephective dot com | Date: | Sun, 18 Apr 2010 20:28:26 +0000 |
| Subject: | note 97409 added to function.ziparchive-extractto | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168375@lists.php.net to get a copy of this message | ||
This function will flatten a zip file using the ZipArchive class.
It will extract all the files in the zip and store them in a single destination directory. That is,
no sub-directories will be created.
If anyone knows a better way to determine if an entry is a directory, please chime in. I feel dirty
checking for a trailing slash.
<?php
// dest shouldn't have a trailing slash
function zip_flatten ( $zipfile, $dest='.' )
{
$zip = new ZipArchive;
if ( $zip->open( $zipfile ) )
{
for ( $i=0; $i < $zip->numFiles; $i++ )
{
$entry = $zip->getNameIndex($i);
if ( substr( $entry, -1 ) == '/' ) continue; // skip directories
$fp = $zip->getStream( $entry );
$ofp = fopen( $dest.'/'.basename($entry), 'w' );
if ( ! $fp )
throw new Exception('Unable to extract the file.');
while ( ! feof( $fp ) )
fwrite( $ofp, fread($fp, 8192) );
fclose($fp);
fclose($ofp);
}
}
else
return false;
return $zip;
}
/*
Example Usage:
zip_flatten( 'test.zip', 'my/path' );
*/
?>
----
Server IP: 69.147.83.197
Probable Submitter: 71.238.254.92
----
Manual Page -- http://www.php.net/manual/en/function.ziparchive-extractto.php
Edit -- https://master.php.net/note/edit/97409
Del: integrated -- https://master.php.net/note/delete/97409/integrated
Del: useless -- https://master.php.net/note/delete/97409/useless
Del: bad code -- https://master.php.net/note/delete/97409/bad+code
Del: spam -- https://master.php.net/note/delete/97409/spam
Del: non-english -- https://master.php.net/note/delete/97409/non-english
Del: in docs -- https://master.php.net/note/delete/97409/in+docs
Del: other reasons-- https://master.php.net/note/delete/97409
Reject -- https://master.php.net/note/reject/97409
Search -- https://master.php.net/manage/user-notes.php