There is a usefull function to get the ZipArchive status as a human readable string :
<?php
function ZipStatusString( $status )
{
switch( (int) $status )
{
case ZipArchive::ER_OK : return 'N No error';
case ZipArchive::ER_MULTIDISK : return 'N Multi-disk zip archives not supported';
case ZipArchive::ER_RENAME : return 'S Renaming temporary file failed';
case ZipArchive::ER_CLOSE : return 'S Closing zip archive failed';
case ZipArchive::ER_SEEK : return 'S Seek error';
case ZipArchive::ER_READ : return 'S Read error';
case ZipArchive::ER_WRITE : return 'S Write error';
case ZipArchive::ER_CRC : return 'N CRC error';
case ZipArchive::ER_ZIPCLOSED : return 'N Containing zip archive was closed';
case ZipArchive::ER_NOENT : return 'N No such file';
case ZipArchive::ER_EXISTS : return 'N File already exists';
case ZipArchive::ER_OPEN : return 'S Can\'t open file';
case ZipArchive::ER_TMPOPEN : return 'S Failure to create temporary file';
case ZipArchive::ER_ZLIB : return 'Z Zlib error';
case ZipArchive::ER_MEMORY : return 'N Malloc failure';
case ZipArchive::ER_CHANGED : return 'N Entry has been changed';
case ZipArchive::ER_COMPNOTSUPP : return 'N Compression method not supported';
case ZipArchive::ER_EOF : return 'N Premature EOF';
case ZipArchive::ER_INVAL : return 'N Invalid argument';
case ZipArchive::ER_NOZIP : return 'N Not a zip archive';
case ZipArchive::ER_INTERNAL : return 'N Internal error';
case ZipArchive::ER_INCONS : return 'N Zip archive inconsistent';
case ZipArchive::ER_REMOVE : return 'S Can\'t remove file';
case ZipArchive::ER_DELETED : return 'N Entry has been deleted';
default: return sprintf('Unknown status %s', $status );
}
}
?>
Die ZipArchive Klasse
(No version information available, might only be in SVN)
Einführung
Ein Dateiarchiv, komprimiert mit Zip.
Klassenbeschreibung
ZipArchive
{
/* Eigenschaften */
/* Methoden */
bool addFile
( string
}$filename
[, string $localname = NULL
[, int $start = 0
[, int $length = 0
]]] )Eigenschaften
- status
-
Status des Zip-Archives
- statusSys
-
System Status des Zip-Archives
- numFiles
-
Anzahl Dateien im Archiv
- filename
-
Dateiname im Dateisystem
- comment
-
Kommentar für das Archiv
Inhaltsverzeichnis
- ZipArchive::addEmptyDir — Fügt ein neues Verzeichnis hinzu
- ZipArchive::addFile — Fügt eine Datei von einem gegebenen Pfad zu einem ZIP-Archiv hinzu
- ZipArchive::addFromString — Fügt eine Datei unter Verwendung ihres Inhalts zu einem ZIP-Archiv hinzu
- ZipArchive::close — Schließt das aktive Archiv (geöffnet oder neu erstellt)
- ZipArchive::deleteIndex — Löscht einen Archiveintrag unter Verwendung seines Index
- ZipArchive::deleteName — Löscht einen Archiveintrag unter Verwendung seines Namens
- ZipArchive::extractTo — Extrahiert den Archivinhalt
- ZipArchive::getArchiveComment — Gibt den ZIP-Archiv-Kommentar zurück
- ZipArchive::getCommentIndex — Gibt den Kommentar zu einem Eintrag unter Verwendung des Eintragsindex zurück
- ZipArchive::getCommentName — Gibt den Kommentar zu einem Eintrag unter Verwendung des Eintragsnamens zurück
- ZipArchive::getFromIndex — Gibt den Inhalt eines Eintrags unter Verwendung seines Index zurück
- ZipArchive::getFromName — Gibt den Inhalt eines Eintrags unter Verwendung seines Namens zurück
- ZipArchive::getNameIndex — Gibt den Namen eines Eintrags unter Verwendung seines Index zurück
- ZipArchive::getStatusString — Gibt die Statusfehlermeldung sowie die System- oder ZIP-Meldung zurück
- ZipArchive::getStream — Erzeugt einen Dateizeiger zu dem per Name bestimmten Eintrag (read only)
- ZipArchive::locateName — Gibt den Index eines Archiveintrags zurück
- ZipArchive::open — Öffnet ein ZIP-Dateiarchiv
- ZipArchive::renameIndex — Benennt einen durch seinen Index bestimmten Eintrag um
- ZipArchive::renameName — Benennt einen durch seinen Namen bestimmten Eintrag um
- ZipArchive::setArchiveComment — Setzt einen Kommentar zu einem ZIP-Archiv
- ZipArchive::setCommentIndex — Setzt einen Eintragskommentar, der via Eintragsindex bestimmt wird
- ZipArchive::setCommentName — Setzt einen Eintragskommentar, der via Eintragsnamen bestimmt wird
- ZipArchive::statIndex — Gibt die Details eines via Index bestimmten Eintrags zurück
- ZipArchive::statName — Gibt die Details eines via Namen bestimmten Eintrags zurück
- ZipArchive::unchangeAll — Setzt alle im Archiv durchgeführten Änderungen zurück
- ZipArchive::unchangeArchive — Nimmt alle globalen Änderungen zurück, die im Archiv durchgeführt wurden
- ZipArchive::unchangeIndex — Nimmt alle Änderungen zurück, die auf dem Eintrag mit dem gegebenen Index gemacht wurden
- ZipArchive::unchangeName — Nimmt alle Änderungen zurück, die auf dem Eintrag mit dem gegebenen Namen gemacht wurden
bruno dot vibert at bonobox dot fr
09-May-2012 10:21
webmaster at sebastiangrinke dot info
06-Oct-2011 03:30
Here is a simple function which zips folders with all sub folders or only a simple file... the $data var can be a string or an array...
<?php
public function un_zip($data,$arcpf,$mode='zip',$obj=''){
$absoluterpfad = 'YOUR_BASE_PATH';
$arcpf = $absoluterpfad.DS.$arcpf;
if(is_object($obj)==false){
$archiv = new ZipArchive();
$archiv->open($arcpf,ZipArchive::CREATE);
}else{$archiv =& $obj;}
if($mode=='zip'){
if(is_array($data)==true){
foreach($data as $dtmp){
$archiv =& un_zip($dtmp,$arcpf,'zip',&$archiv);
}
}else{
if(is_dir($data)==true){
$archiv->addEmptyDir(str_replace($absoluterpfad.DS,'',$data));
$files = scandir($data);
$bad = array('.','..');
$files = array_diff($files,$bad);
foreach($files as $ftmp){
if(is_dir($data.DS.$ftmp)==true){
$archiv->addEmptyDir(str_replace($absoluterpfad.DS,'',$data.'/'.$ftmp));
$archiv =& un_zip($data.DS.$ftmp,$arcpf,'zip',&$archiv);
}elseif(is_file($data.DS.$ftmp)==true){
$archiv->addFile($data.DS.$ftmp,str_replace($absoluterpfad.DS,'',$data.'/'.$ftmp));
}
}
}elseif(is_file($data)==true){$archiv->addFile($data,str_replace($absoluterpfad.DS,'',$data));}
}
}
if(is_object($obj)==false){$archiv->close();}
else{return $archiv;}
if($mode=='unzip'){$archiv->extractTo($data);}
}
?>
Jerry dot Saravia at emc dot com
08-Aug-2011 03:57
The following code can be used to get a list of all the file names in a zip file.
<?php
$za = new ZipArchive();
$za->open('theZip.zip');
for( $i = 0; $i < $za->numFiles; $i++ ){
$stat = $za->statIndex( $i );
print_r( basename( $stat['name'] ) . PHP_EOL );
}
?>
anonymous at example dot net
29-Jul-2011 01:18
status - libzip error code (ER_*)
statusSys - copy of errno (E*) or zlib error code
ER_OK N No error
ER_MULTIDISK N Multi-disk zip archives not supported
ER_RENAME S Renaming temporary file failed
ER_CLOSE S Closing zip archive failed
ER_SEEK S Seek error
ER_READ S Read error
ER_WRITE S Write error
ER_CRC N CRC error
ER_ZIPCLOSED N Containing zip archive was closed
ER_NOENT N No such file
ER_EXISTS N File already exists
ER_OPEN S Can't open file
ER_TMPOPEN S Failure to create temporary file
ER_ZLIB Z Zlib error
ER_MEMORY N Malloc failure
ER_CHANGED N Entry has been changed
ER_COMPNOTSUPP N Compression method not supported
ER_EOF N Premature EOF
ER_INVAL N Invalid argument
ER_NOZIP N Not a zip archive
ER_INTERNAL N Internal error
ER_INCONS N Zip archive inconsistent
ER_REMOVE S Can't remove file
ER_DELETED N Entry has been deleted
h-fate at gmx dot net
05-Oct-2010 06:17
Be wary that there are several algorithms to generate a zip file. I found that Office OpenXML files created with ZipArchive are not recognized by Excel 2007, for example.
You have to use a different class to zip in this case, such as PclZip.
hardcorevenom at gmx dot com
15-Jun-2010 05:17
Read a file from an archive to a variable.
A warning is printed automatically in case of a CRC32 mismatch, which we capture, so we can print our own error message.
<?php
$zip = new ZipArchive();
if ($zip->open('archive.zip')) {
$fp = $zip->getStream('myfile.txt'); //file inside archive
if(!$fp)
die("Error: can't get stream to zipped file");
$stat = $zip->statName('myfile.txt');
$buf = ""; //file buffer
ob_start(); //to capture CRC error message
while (!feof($fp)) {
$buf .= fread($fp, 2048); //reading more than 2156 bytes seems to disable internal CRC32 verification (bug?)
}
$s = ob_get_contents();
ob_end_clean();
if(stripos($s, "CRC error") != FALSE){
echo 'CRC32 mismatch, current ';
printf("%08X", crc32($buf)); //current CRC
echo ', expected ';
printf("%08X", $stat['crc']); //expected CRC
}
fclose($fp);
$zip->close();
//Done, unpacked file is stored in $buf
}
?>
To create a corrupt file, change a byte in a zip file using a hex editor.
