exit; } $zipName = time() . "-" . rand(0, 100000); $zipFilePureName = $zipName . ".zip"; $zipFileName = realpath(TEMP_FOLDER) . "/" . $zipFilePureName; if (ZIP_METHOD == "BUILD_IN") { $zip = new ZipArchive(); $zip->open($zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE); $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY); foreach ($files as $name => $file) { if (!$file->isDir()) { $filePath = $file->getRealPath(); $relativePath = substr($filePath, strlen($rootPath) + 1); if (substr($relativePath, 0, 1) == "." || strpos($relativePath, "/.") !== false) { continue; } $zip->addFile($filePath, $zipDir . $relativePath); if (version_compare(PHP_VERSION, '7.0.0', '>=')) { $zip->setCompressionName($filePath, ZipArchive::CM_STORE); } } } $zip->close(); } elseif (ZIP_METHOD == "LINUX_EXT") { exec("cd \"" . UPLOAD_FOLDER . "/" . PROJECT_NAME . "/" . $dir . "\" && zip -r0 \"{$zipFileName}\" . -x \".*\""); } else { echo "WRONG CONFIG-SETTING"; exit; } $downloadName = WEBSITE_DEFAULT_URI . "/temp/" . $zipFilePureName; header("Location: " . $downloadName);
<?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();
public function action_download() { $id = preg_replace('/[^a-z0-9]/i', '', $this->request->param('id')); DB::delete('downloads')->where('expires', '<=', time())->execute(); $expired = DB::select('filename')->from('download_packages')->where('expires', '<=', time())->execute()->as_array(NULL, 'filename'); if ($expired) { foreach ($expired as $file) { if (file_exists($file)) { unlink($file); } } DB::delete('download_packages')->where('filename', 'IN', $expired)->execute(); } $filename = DOCROOT . 'storage/zip' . $id; DB::query(Database::UPDATE, DB::expr('REPLACE INTO `download_packages` (`filename`, `expires`) VALUES (:filename, :expires)', array(':filename' => $filename, ':expires' => time() + 3600))->compile())->execute(); $files = DB::select()->from('downloads')->where('download_id', '=', $id)->execute()->as_array(); if (!$files) { if (!file_exists($filename)) { throw new HTTP_Exception_404('Not found'); } header('X-SendFile: ' . realpath($filename)); header('Content-Type: application/zip'); header('Content-Disposition: attachment; filename="Attachments.zip"'); header('X-Accel-Redirect: ' . URL::base() . 'storage/zip' . $id); die; /*header('Content-length: ' . filesize($filename)); $file = fopen($filename, 'r'); $bufsize = 16 * 1024 * 1024; while ($buf = fread($file, $bufsize)) { echo $buf; $buf = ''; } fclose($file);*/ } $count = 0; $total = 0; foreach ($files as $file) { $total += $file['size']; $count++; if (!isset($zip)) { $zip = new ZipArchive(); $zip->open($filename, ZipArchive::CREATE); } $zip->addFile(DOCROOT . 'storage/' . $file['attachment_id'], $file['path']); $zip->setCompressionName($file['path'], ZipArchive::CM_STORE); DB::delete('downloads')->where('id', '=', $file['id'])->execute(); if ($count >= 100 || $total >= 100 * 1024 * 1024) { $zip->close(); unset($zip); break; } } if (isset($zip)) { $zip->close(); } $info = DB::select(DB::expr('SUM(`size`) as size'), DB::expr('COUNT(*) as cnt'))->from('downloads')->where('download_id', '=', $id)->execute()->current(); header('Content-type: application/json'); die(json_encode(array('success' => true, 'id' => $id, 'count' => intval($info['cnt']), 'total' => intval($info['size'])))); }
unlink($tmpfile); } // 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";
<?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);