1<?php
2// stolen from PEAR2_Pyrus_Developer_Creator_Zip by Greg Beaver, the original author, for use in unit tests
3class zipmaker
4{
5    /**
6     * Path to archive file
7     *
8     * @var string
9     */
10    protected $archive;
11    /**
12     * @var ZIPArchive
13     */
14    protected $zip;
15    protected $path;
16    function __construct($path)
17    {
18        if (!class_exists('ZIPArchive')) {
19            throw new Exception(
20                'Zip extension is not available');
21        }
22        $this->path = $path;
23    }
24
25    /**
26     * save a file inside this package
27     * @param string relative path within the package
28     * @param string|resource file contents or open file handle
29     */
30    function addFile($path, $fileOrStream)
31    {
32        if (is_resource($fileOrStream)) {
33            $this->zip->addFromString($path, stream_get_contents($fileOrStream));
34        } else {
35            $this->zip->addFromString($path, $fileOrStream);
36        }
37    }
38
39    /**
40     * Initialize the package creator
41     */
42    function init()
43    {
44        $this->zip = new ZipArchive;
45        if (true !== $this->zip->open($this->path, ZIPARCHIVE::CREATE)) {
46            throw new Exception(
47                'Cannot open ZIP archive ' . $this->path
48            );
49        }
50    }
51
52    /**
53     * Create an internal directory, creating parent directories as needed
54     *
55     * This is a no-op for the tar creator
56     * @param string $dir
57     */
58    function mkdir($dir)
59    {
60        $this->zip->addEmptyDir($dir);
61    }
62
63    /**
64     * Finish saving the package
65     */
66    function close()
67    {
68        $this->zip->close();
69    }
70}
71