// generate the ZIP file $zip = new ZipArchive(); if ($zip->open($tmpfile, ZipArchive::CREATE) !== TRUE) { exit('failed'); } $txt = file_get_contents(__FILE__); $zip->addFromString('entry1.txt', $txt); $zip->addFromString('entry2.txt', $txt); $zip->addFromString('dir/entry3.txt', $txt); $zip->addFromString('entry4.txt', $txt); $zip->addFromString('entry5.txt', $txt); $zip->addFromString('entry6.txt', $txt); $zip->addFromString('entry7.txt', $txt); var_dump($zip->setCompressionName('entry2.txt', ZipArchive::CM_DEFAULT)); var_dump($zip->setCompressionName('dir/entry3.txt', ZipArchive::CM_STORE)); var_dump($zip->setCompressionName('entry4.txt', ZipArchive::CM_DEFLATE)); var_dump($zip->setCompressionIndex(4, ZipArchive::CM_STORE)); var_dump($zip->setCompressionIndex(5, ZipArchive::CM_DEFLATE)); var_dump($zip->setCompressionIndex(6, ZipArchive::CM_DEFAULT)); if (!$zip->close()) { exit('failed'); } // check the ZIP file $zip = zip_open($tmpfile); if (!is_resource($zip)) { exit('failed'); } while ($e = zip_read($zip)) { echo zip_entry_name($e) . ': ' . zip_entry_compressionmethod($e) . "\n"; } zip_close($zip);
<?php error_reporting(E_ALL); if (!extension_loaded('zip')) { dl('zip.so'); } $zip = new ZipArchive(); $filename = "a.zip"; if (!$zip->open($filename, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE)) { exit("cannot open <{$filename}>\n"); } $zip->addFromString("testfilephp.txt", "#1 This is a test string added as testfilephp.txt.\n"); $zip->addFromString("testfilephp2.txt", "#2 This is a test string added as testfilephp2.txt.\n"); $zip->addFile("too.php", "testfromfile.php"); $zip->setCompressionName("testfilephp2.txt", ZipArchive::CM_STORE); $zip->setCompressionIndex(2, ZipArchive::CM_STORE); $zip->close();
<?php $str = 'temp'; $dir = tempnam(sys_get_temp_dir(), __FILE__); unlink($dir); mkdir($dir); $archive = new ZipArchive(); $archive->open("{$dir}/comptest.zip", ZipArchive::CREATE); $archive->addFromString("A.txt", $str); $archive->addFromString("B.txt", $str); var_dump($archive->setCompressionIndex(0, ZIPArchive::CM_STORE)); var_dump($archive->setCompressionName("B.txt", ZIPArchive::CM_STORE)); $archive->close(); unlink("{$dir}/comptest.zip"); rmdir($dir);