1<?php
2class ArchiveHelperZip {
3
4	/** The temporary file name of the Zip archive. */
5	private $temporaryZipFilename;
6
7	/** Holds the Zip archive builder. */
8	private $zip;
9
10	/** Name of the file currently being appended into the ZIP archive. */
11	private $currentFilename;
12
13	/** Content of the file currently being appended into the ZIP archive. */
14	private $currentContent;
15
16	/**
17	 * Returns the MIME type of this kind of archive.
18	 */
19	function getContentType() {
20		return "application/zip";
21	}
22
23	/**
24	 * Initializes the temporary ZIP archive.
25	 */
26	function startArchive() {
27		$this->temporaryZipFilename = tempnam(sys_get_temp_dir(), "zip");
28		$this->zip = new ZipArchive();
29		$this->zip->open($this->temporaryZipFilename, ZipArchive::OVERWRITE);
30	}
31
32	/**
33	 * Starts a new file in the ZIP archive.
34	 */
35	function startFile($filename) {
36		$this->currentFilename = $filename;
37		$this->currentContent = "";
38	}
39
40	/**
41	 * Appends content to current file.
42	 */
43	function appendContent($content) {
44		$this->currentContent .= $content;
45	}
46
47	/**
48	 * Close current file.
49	 */
50	function closeFile() {
51		$this->zip->addFromString($this->currentFilename, $this->currentContent);
52		$this->currentFilename = "";
53		$this->currentContent = "";
54	}
55	/**
56	 * Inserts a complete entry.
57 	 */
58	function insertContent($filename, $content) {
59		$this->zip->addFromString($filename, $content);
60	}
61
62	/**
63	 * Closes the ZIP archive and returns its whole content as a string.
64	 * @return Content of the ZIP archive.
65	 */
66	function closeArchive() {
67		$this->zip->close();
68		return file_get_contents($this->temporaryZipFilename);
69		unlink($this->temporaryZipFilename);
70	}
71
72}
73