PHP Doku:: The ZipArchive class - class.ziparchive.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzErweiterungen zur Datenkompression und ArchivierungZipThe ZipArchive class

Ein Service von Reinhard Neidl - Webprogrammierung.

Zip

<<Beispiele

ZipArchive::addEmptyDir>>


UnterSeiten:

The ZipArchive class

Einführung

A file archive, compressed with Zip.

Klassenbeschreibung

ZipArchive {
/* Eigenschaften */
/* Methoden */
bool addEmptyDir ( string $dirname )
bool addFile ( string $filename [, string $localname ] )
bool addFromString ( string $localname , string $contents )
bool close ( void )
bool deleteIndex ( int $index )
bool deleteName ( string $name )
bool extractTo ( string $destination [, mixed $entries ] )
string getArchiveComment ( void )
string getCommentIndex ( int $index [, int $flags ] )
string getCommentName ( string $name [, int $flags ] )
mixed getFromIndex ( int $index [, int $flags ] )
mixed getFromName ( string $name [, int $flags ] )
string getNameIndex ( int $index )
string getStatusString ( void )
resource getStream ( string $name )
mixed locateName ( string $name [, int $flags ] )
mixed open ( string $filename [, int $flags ] )
bool renameIndex ( int $index , string $newname )
bool renameName ( string $name , string $newname )
mixed setArchiveComment ( string $comment )
mixed setCommentIndex ( int $index , string $comment )
mixed setCommentName ( string $name , string $comment )
mixed statIndex ( int $index [, int $flags ] )
mixed statName ( name $name [, int $flags ] )
mixed unchangeAll ( void )
mixed unchangeArchive ( void )
mixed unchangeIndex ( int $index )
mixed unchangeName ( string $name )
}

Eigenschaften

status

Status of the Zip Archive

statusSys

System status of the Zip Archive

numFiles

Number of files in archive

filename

File name in the file system

comment

Comment for the archive

Inhaltsverzeichnis


2 BenutzerBeiträge:
- Beiträge aktualisieren...
h-fate at gmx dot net
5.10.2010 13: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
16.06.2010 0: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.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)