Пример #1
0
$fileDir = './';

// To use the new namespaces, you need a bootstrapper/autoloader, examples are provided here.
// The changes to your Zip use are limited to two lines after that is in place.
// Require your bootstrap.php, or the autoload.php, and change the class instantiation from nwe Zip( to
// new \PHPZip\Zip\File\Zip(
// The parameters are unchanged.

require_once('bootstrap.php'); // include_once("Zip.php");
$fileTime = date("D, d M Y H:i:s T");

$zip = new \PHPZip\Zip\File\Zip(); // $zip = new Zip();
$zip->setZipFile("ZipExample.zip");

$zip->setComment("Example Zip file.\nCreated on " . date('l jS \of F Y h:i:s A'));
$zip->addFile("Hello World!", "hello.txt");

@$handle = opendir($fileDir);
if ($handle) {
    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
        if (strpos($file, ".html") !== false) {
            $pathData = pathinfo($fileDir . $file);
            $fileName = $pathData['filename'];

            $zip->addFile(file_get_contents($fileDir . $file), $file, filectime($fileDir . $file));
        }
    }
}

$zip->finalize(); // as we are not using getZipData or getZipFile, we need to call finalize ourselves.
Пример #2
0
require_once('bootstrap.php'); // include_once("Zip.php");
$fileTime = date("D, d M Y H:i:s T");

// Set a temp file to use, instead of the default system temp file directory.
// The temp file is used if the generated Zip file is becoming too large to hold in memory.
//Zip::$temp = "./tempFile";

// Setting this to a function to create the temp files requires PHP 5.3 or newer:
//Zip::$temp = function() { return tempnam(sys_get_temp_dir(), 'Zip');};
\PHPZip\Zip\File\Zip::$temp = function() { return "./tempFile_" . rand(100000, 999999);};

$zip = new \PHPZip\Zip\File\Zip(); // $zip = new Zip();
// Archive comments don't really support utf-8. Some tools detect and read it though.
$zip->setComment("Example Zip file.\nCreated on " . date('l jS \of F Y h:i:s A'));
$zip->addFile("Hello World!", "hello.txt");

@$handle = opendir($fileDir);
if ($handle) {
	/* This is the correct way to loop over the directory. */
	while (false !== ($file = readdir($handle))) {
		if (strpos($file, ".php") !== false) {
			$pathData = pathinfo($fileDir . $file);
			$fileName = $pathData['filename'];

			$zip->addFile(file_get_contents($fileDir . $file), $file, filectime($fileDir . $file), NULL, TRUE, Zip::getFileExtAttr($file));
		}
	}
}

// Uses my Lipsum generator from https://github.com/Grandt/PHPLipsumGenerator
Пример #3
0
$fileDir = './';
ob_start(); // This is only to show that ob_start can be called, however the buffer must be empty when sending.

// To use the new namespaces, you need a bootstrapper/autoloader, examples are provided here.
// The changes to your Zip use are limited to two lines after that is in place.
// Require your bootstrap.php, or the autoload.php, and change the class instantiation from nwe Zip( to
// new \PHPZip\Zip\File\Zip(
// The parameters are unchanged.
require_once('bootstrap.php'); // include_once("Zip.php");
$fileTime = date("D, d M Y H:i:s T");

$zip = new \PHPZip\Zip\File\Zip(); // $zip = new Zip();
// Archive comments don't really support utf-8. Some tools detect and read it though.
$zip->setComment("Example Zip file.\nАрхив Комментарий\nCreated on " . date('l jS \of F Y h:i:s A'));
// A bit of Russian (I hope), to test UTF-8 file names. (Note. I'm not Russian, and don't speak the language, this is just for testing)
$zip->addFile("Hello World!", "hello.txt");
$zip->addFile("Привет мир!", "Привет мир. С комментарий к файлу.txt", 0, "Кириллица файл комментарий");
$zip->addFile("Hello World!", "hello.txt");

@$handle = opendir($fileDir);
if ($handle) {
	/* This is the correct way to loop over the directory. */
	while (false !== ($file = readdir($handle))) {
		if (strpos($file, ".php") !== false) {
			$pathData = pathinfo($fileDir . $file);
			$fileName = $pathData['filename'];

			$zip->addFile(file_get_contents($fileDir . $file), $file, filectime($fileDir . $file), NULL, TRUE, Zip::getFileExtAttr($file));
		}
	}
}