PHP Classes

File: Zip.Example2.php

Recommend this page to a friend!
  Classes of Asbjorn Grandt   Zip   Zip.Example2.php   Download  
File: Zip.Example2.php
Role: Example script
Content type: text/plain
Description: Example file for generating a zip file and save it on the server.
Class: Zip
Create archives of compressed files in ZIP format
Author: By
Last change: New parameter on addFile, to force 'STORE' of file.

* Added: Parameter $compress to addFile. A boolean which when set to
FALSE will store the added data, rather than attempt to compress it.
Good for already compressed files such as images and other multimedia
types.
Mostly files changes are just cleanup (trailing spaces, and tabs to
spaces)
This is the initial release of PHPZip on GitHub
The version if the files are 1.28

Signed-off-by: Asbjorn Grandt <asbjorn@grandt.com>
Date: 10 years ago
Size: 1,155 bytes
 

Contents

Class file image Download
<?php
// Example. Zip all .html files in the current directory and save to current directory.
// Make a copy, also to the current dir, for good measure.
$fileDir = './';

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

$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.
$zip->setZipFile("ZipExample2.zip");
?>
<html>
<head>
<title>Zip Test</title>
</head>
<body>
<h1>Zip Test</h1>
<p>Zip files saved, length is <?php echo strlen($zip->getZipData()); ?> bytes.</p>
</body>
</html>