zipFile = tmpfile(); } else { $this->zipData = ""; } } function __destruct() { if (!is_null($this->zipFile)) { fclose($this->zipFile); } $this->zipData= null; } /** * Set Zip archive comment. * * @param string $newComment New comment. null to clear. */ public function setComment($newComment = null) { $this->zipComment = $newComment; } /** * Set zip file to write zip data to. * 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. * * @param string $fileName */ public function setZipFile($fileName) { if (file_exists($fileName)) { unlink ($fileName); } $fd=fopen($fileName, "x+b"); if (!is_null($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; } /** * Add an empty directory entry to the zip archive. * Basically this is only used if an empty directory is added. * * @param string $directoryPath Directory Path and name to be added to the archive. * @param int $timestamp (Optional) Timestamp for the added directory, if omitted or set to 0, the current time will be used. * @param string $fileComment (Optional) Comment to be added to the archive for this directory. To use fileComment, timestamp must be given. */ public function addDirectory($directoryPath, $timestamp = 0, $fileComment = null) { if ($this->isFinalized) { return; } $this->buildZipEntry($directoryPath, $fileComment, "\x00\x00", "\x00\x00", $timestamp, "\x00\x00\x00\x00", 0, 0, 16); } /** * Add a file to the archive at the specified location and file name. * * @param string $data File data. * @param string $filePath Filepath and name to be used in the archive. * @param int $timestamp (Optional) Timestamp for the added file, if omitted or set to 0, the current time will be used. * @param string $fileComment (Optional) Comment to be added to the archive for this file. To use fileComment, timestamp must be given. */ public function addFile($data, $filePath, $timestamp = 0, $fileComment = null) { if ($this->isFinalized) { return; } $gzType = "\x08\x00"; // Compression type 8 = deflate $gpFlags = "\x02\x00"; // General Purpose bit flags for compression type 8 it is: 0=Normal, 1=Maximum, 2=Fast, 3=super fast compression. $dataLength = strlen($data); $fileCRC32 = pack("V", crc32($data)); $gzData = gzcompress($data); $gzData = substr( substr($gzData, 0, strlen($gzData) - 4), 2); // gzcompress adds a 2 byte header and 4 byte CRC we can't use. // The 2 byte header does contain useful data, though in this case the 2 parameters we'd be interrested in will always be 8 for compression type, and 2 for General purpose flag. $gzLength = strlen($gzData); if ($gzLength >= $dataLength) { $gzLength = $dataLength; $gzData = $data; $gzType = "\x00\x00"; // Compression type 0 = stored $gpFlags = "\x00\x00"; // Compression type 0 = stored } if (is_null($this->zipFile) && ($this->offset + $gzLength) > $this->zipMemoryThreshold) { $this->zipFile = tmpfile(); fwrite($this->zipFile, $this->zipData); $this->zipData = null; } $this->buildZipEntry($filePath, $fileComment, $gpFlags, $gzType, $timestamp, $fileCRC32, $gzLength, $dataLength, 32); if (is_null($this->zipFile)) { $this->zipData .= $gzData; } else { fwrite($this->zipFile, $gzData); } } /** * Add a file to the archive at the specified location and file name. * * @param string $dataFile File name/path. * @param string $filePath Filepath and name to be used in the archive. * @param int $timestamp (Optional) Timestamp for the added file, if omitted or set to 0, the current time will be used. * @param string $fileComment (Optional) Comment to be added to the archive for this file. To use fileComment, timestamp must be given. */ public function addLargeFile($dataFile, $filePath, $timestamp = 0, $fileComment = null) { if ($this->isFinalized) { return; } $this->openStream($filePath, $timestamp, $fileComment); $fh = fopen($dataFile, "rb"); while(!feof($fh)) { $this->addStreamData(fread($fh, $this->streamChunkSize)); } fclose($fh); $this->closeStream(); } /** * Create a stream to be used for large entries. * * @param string $filePath Filepath and name to be used in the archive. * @param int $timestamp (Optional) Timestamp for the added file, if omitted or set to 0, the current time will be used. * @param string $fileComment (Optional) Comment to be added to the archive for this file. To use fileComment, timestamp must be given. */ public function openStream($filePath, $timestamp = 0, $fileComment = null) { if ($this->isFinalized) { return; } if (is_null($this->zipFile)) { $this->zipFile = tmpfile(); fwrite($this->zipFile, $this->zipData); $this->zipData = null; } if (strlen($this->streamFilePath) > 0) { closeStream(); } $this->streamFile = tempnam(sys_get_temp_dir(), 'Zip'); $this->streamData = gzopen($this->streamFile, "w9"); $this->streamFilePath = $filePath; $this->streamTimestamp = $timestamp; $this->streamFileComment = $fileComment; $this->streamFileLength = 0; } public function addStreamData($data) { $length = gzwrite($this->streamData, $data, strlen($data)); if ($length != strlen($data)) { print "
Length mismatch
\n"; } $this->streamFileLength += $length; return $length; } /** * Close the current stream. */ public function closeStream() { if ($this->isFinalized || strlen($this->streamFilePath) == 0) { return; } fflush($this->streamData); gzclose($this->streamData); $gzType = "\x08\x00"; // Compression type 8 = deflate $gpFlags = "\x02\x00"; // General Purpose bit flags for compression type 8 it is: 0=Normal, 1=Maximum, 2=Fast, 3=super fast compression. $file_handle = fopen($this->streamFile, "rb"); $stats = fstat($file_handle); $eof = $stats['size']; fseek($file_handle, $eof-8); $fileCRC32 = fread($file_handle, 4); $dataLength = $this->streamFileLength;//$gzl[1]; $gzLength = $eof-10; $eof -= 9; fseek($file_handle, 10); $this->buildZipEntry($this->streamFilePath, $this->streamFileComment, $gpFlags, $gzType, $this->streamTimestamp, $fileCRC32, $gzLength, $dataLength, 32); while(!feof($file_handle)) { fwrite($this->zipFile, fread($file_handle, $this->streamChunkSize)); } unlink($this->streamFile); $this->streamFile = null; $this->streamData = null; $this->streamFilePath = null; $this->streamTimestamp = null; $this->streamFileComment = null; $this->streamFileLength = 0; } /** * Close the archive. * A closed archive can no longer have new files added to it. */ public function finalize() { if(!$this->isFinalized) { if (strlen($this->streamFilePath) > 0) { $this->closeStream(); } $cd = implode("", $this->cdRec); $cdRec = $cd . $this->endOfCentralDirectory . pack("v", sizeof($this->cdRec)) . pack("v", sizeof($this->cdRec)) . pack("V", strlen($cd)) . pack("V", $this->offset); if (!is_null($this->zipComment)) { $cdRec .= pack("v", strlen($this->zipComment)) . $this->zipComment; } else { $cdRec .= "\x00\x00"; } if (is_null($this->zipFile)) { $this->zipData .= $cdRec; } else { fwrite($this->zipFile, $cdRec); fflush($this->zipFile); } $this->isFinalized = true; $cd = null; $this->cdRec = null; } } /** * Get the handle ressource for the archive zip file. * If the zip haven't been finalized yet, this will cause it to become finalized * * @return zip file handle */ public function getZipFile() { if(!$this->isFinalized) { $this->finalize(); } if (is_null($this->zipFile)) { $this->zipFile = tmpfile(); fwrite($this->zipFile, $this->zipData); $this->zipData = null; } rewind($this->zipFile); return $this->zipFile; } /** * Get the zip file contents * If the zip haven't been finalized yet, this will cause it to become finalized * * @return zip data */ public function getZipData() { if(!$this->isFinalized) { $this->finalize(); } if (is_null($this->zipFile)) { return $this->zipData; } else { rewind($this->zipFile); $filestat = fstat($this->zipFile); return fread($this->zipFile, $filestat['size']); } } /** * Send the archive as a zip download * * @param String $fileName The name of the Zip archive, ie. "archive.zip". * @return void */ function sendZip($fileName) { if(!$this->isFinalized) { $this->finalize(); } if (!headers_sent($headerFile, $headerLine) or die("Error: Unable to send file $fileName. HTML Headers have already been sent from $headerFile in line $headerLine
")) { if (ob_get_contents() === false or die("\nError: Unable to send file $fileName.epub. Output buffer contains the following text (typically warnings or errors):
" . ob_get_contents() . "