PHPZip\Zip\File\Zip::setZipFile PHP Method

setZipFile() public method

This will cause all present and future data written to this class to be written to this file. This can be used at any time, even after the Zip Archive have been finalized. Any previous file will be closed. Warning: If the given file already exists, it will be overwritten.
Author: A. Grandt ([email protected])
public setZipFile ( string $fileName ) : boolean
$fileName string
return boolean Success
    public function setZipFile($fileName) {
        if (is_file($fileName)) {
            unlink($fileName);
        }

        $fd = fopen($fileName, "x+b");

        if (is_resource($this->_zipFile)) {
            rewind($this->_zipFile);

            while (!feof($this->_zipFile)) {
                fwrite($fd, fread($this->_zipFile, $this->streamChunkSize));
            }

            fclose($this->_zipFile);
        } else {
            fwrite($fd, $this->_zipData);
            $this->_zipData = null;
        }

        $this->_zipFile = $fd;
        return true;
    }

Usage Example

Example #1
0
	public function test2(){

		// 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 = new ZipArchiveFile();

		$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");

	}