xref: /dokuwiki/vendor/splitbrain/php-archive/src/Zip.php (revision 2b6c681901bfed9efc7d09dca27aee308d48341d)
1605f8e8dSAndreas Gohr<?php
2605f8e8dSAndreas Gohr
3605f8e8dSAndreas Gohrnamespace splitbrain\PHPArchive;
4605f8e8dSAndreas Gohr
5605f8e8dSAndreas Gohr/**
6605f8e8dSAndreas Gohr * Class Zip
7605f8e8dSAndreas Gohr *
8605f8e8dSAndreas Gohr * Creates or extracts Zip archives
9605f8e8dSAndreas Gohr *
10*2b6c6819SAndreas Gohr * for specs see http://www.pkware.com/appnote
11*2b6c6819SAndreas Gohr *
12605f8e8dSAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
13605f8e8dSAndreas Gohr * @package splitbrain\PHPArchive
14605f8e8dSAndreas Gohr * @license MIT
15605f8e8dSAndreas Gohr */
16605f8e8dSAndreas Gohrclass Zip extends Archive
17605f8e8dSAndreas Gohr{
18605f8e8dSAndreas Gohr
19605f8e8dSAndreas Gohr    protected $file = '';
20605f8e8dSAndreas Gohr    protected $fh;
21605f8e8dSAndreas Gohr    protected $memory = '';
22605f8e8dSAndreas Gohr    protected $closed = true;
23605f8e8dSAndreas Gohr    protected $writeaccess = false;
24605f8e8dSAndreas Gohr    protected $ctrl_dir;
25605f8e8dSAndreas Gohr    protected $complevel = 9;
26605f8e8dSAndreas Gohr
27605f8e8dSAndreas Gohr    /**
28605f8e8dSAndreas Gohr     * Set the compression level.
29605f8e8dSAndreas Gohr     *
30605f8e8dSAndreas Gohr     * Compression Type is ignored for ZIP
31605f8e8dSAndreas Gohr     *
32605f8e8dSAndreas Gohr     * You can call this function before adding each file to set differen compression levels
33605f8e8dSAndreas Gohr     * for each file.
34605f8e8dSAndreas Gohr     *
35605f8e8dSAndreas Gohr     * @param int $level Compression level (0 to 9)
36605f8e8dSAndreas Gohr     * @param int $type  Type of compression to use ignored for ZIP
37605f8e8dSAndreas Gohr     * @return mixed
38605f8e8dSAndreas Gohr     */
39605f8e8dSAndreas Gohr    public function setCompression($level = 9, $type = Archive::COMPRESS_AUTO)
40605f8e8dSAndreas Gohr    {
41605f8e8dSAndreas Gohr        $this->complevel = $level;
42605f8e8dSAndreas Gohr    }
43605f8e8dSAndreas Gohr
44605f8e8dSAndreas Gohr    /**
45605f8e8dSAndreas Gohr     * Open an existing ZIP file for reading
46605f8e8dSAndreas Gohr     *
47605f8e8dSAndreas Gohr     * @param string $file
48605f8e8dSAndreas Gohr     * @throws ArchiveIOException
49605f8e8dSAndreas Gohr     */
50605f8e8dSAndreas Gohr    public function open($file)
51605f8e8dSAndreas Gohr    {
52605f8e8dSAndreas Gohr        $this->file = $file;
53605f8e8dSAndreas Gohr        $this->fh   = @fopen($this->file, 'rb');
54605f8e8dSAndreas Gohr        if (!$this->fh) {
55605f8e8dSAndreas Gohr            throw new ArchiveIOException('Could not open file for reading: '.$this->file);
56605f8e8dSAndreas Gohr        }
57605f8e8dSAndreas Gohr        $this->closed = false;
58605f8e8dSAndreas Gohr    }
59605f8e8dSAndreas Gohr
60605f8e8dSAndreas Gohr    /**
61605f8e8dSAndreas Gohr     * Read the contents of a ZIP archive
62605f8e8dSAndreas Gohr     *
63605f8e8dSAndreas Gohr     * This function lists the files stored in the archive, and returns an indexed array of FileInfo objects
64605f8e8dSAndreas Gohr     *
65605f8e8dSAndreas Gohr     * The archive is closed afer reading the contents, for API compatibility with TAR files
66605f8e8dSAndreas Gohr     * Reopen the file with open() again if you want to do additional operations
67605f8e8dSAndreas Gohr     *
68605f8e8dSAndreas Gohr     * @throws ArchiveIOException
69605f8e8dSAndreas Gohr     * @return FileInfo[]
70605f8e8dSAndreas Gohr     */
71605f8e8dSAndreas Gohr    public function contents()
72605f8e8dSAndreas Gohr    {
73605f8e8dSAndreas Gohr        if ($this->closed || !$this->file) {
74605f8e8dSAndreas Gohr            throw new ArchiveIOException('Can not read from a closed archive');
75605f8e8dSAndreas Gohr        }
76605f8e8dSAndreas Gohr
77605f8e8dSAndreas Gohr        $result = array();
78605f8e8dSAndreas Gohr
79605f8e8dSAndreas Gohr        $centd = $this->readCentralDir();
80605f8e8dSAndreas Gohr
81605f8e8dSAndreas Gohr        @rewind($this->fh);
82605f8e8dSAndreas Gohr        @fseek($this->fh, $centd['offset']);
83605f8e8dSAndreas Gohr
84605f8e8dSAndreas Gohr        for ($i = 0; $i < $centd['entries']; $i++) {
85605f8e8dSAndreas Gohr            $result[] = $this->header2fileinfo($this->readCentralFileHeader());
86605f8e8dSAndreas Gohr        }
87605f8e8dSAndreas Gohr
88605f8e8dSAndreas Gohr        $this->close();
89605f8e8dSAndreas Gohr        return $result;
90605f8e8dSAndreas Gohr    }
91605f8e8dSAndreas Gohr
92605f8e8dSAndreas Gohr    /**
93605f8e8dSAndreas Gohr     * Extract an existing ZIP archive
94605f8e8dSAndreas Gohr     *
95605f8e8dSAndreas Gohr     * The $strip parameter allows you to strip a certain number of path components from the filenames
96605f8e8dSAndreas Gohr     * found in the tar file, similar to the --strip-components feature of GNU tar. This is triggered when
97605f8e8dSAndreas Gohr     * an integer is passed as $strip.
98605f8e8dSAndreas Gohr     * Alternatively a fixed string prefix may be passed in $strip. If the filename matches this prefix,
99605f8e8dSAndreas Gohr     * the prefix will be stripped. It is recommended to give prefixes with a trailing slash.
100605f8e8dSAndreas Gohr     *
101605f8e8dSAndreas Gohr     * By default this will extract all files found in the archive. You can restrict the output using the $include
102605f8e8dSAndreas Gohr     * and $exclude parameter. Both expect a full regular expression (including delimiters and modifiers). If
103605f8e8dSAndreas Gohr     * $include is set only files that match this expression will be extracted. Files that match the $exclude
104605f8e8dSAndreas Gohr     * expression will never be extracted. Both parameters can be used in combination. Expressions are matched against
105605f8e8dSAndreas Gohr     * stripped filenames as described above.
106605f8e8dSAndreas Gohr     *
107605f8e8dSAndreas Gohr     * @param string     $outdir  the target directory for extracting
108605f8e8dSAndreas Gohr     * @param int|string $strip   either the number of path components or a fixed prefix to strip
109605f8e8dSAndreas Gohr     * @param string     $exclude a regular expression of files to exclude
110605f8e8dSAndreas Gohr     * @param string     $include a regular expression of files to include
111605f8e8dSAndreas Gohr     * @throws ArchiveIOException
112605f8e8dSAndreas Gohr     * @return FileInfo[]
113605f8e8dSAndreas Gohr     */
114605f8e8dSAndreas Gohr    function extract($outdir, $strip = '', $exclude = '', $include = '')
115605f8e8dSAndreas Gohr    {
116605f8e8dSAndreas Gohr        if ($this->closed || !$this->file) {
117605f8e8dSAndreas Gohr            throw new ArchiveIOException('Can not read from a closed archive');
118605f8e8dSAndreas Gohr        }
119605f8e8dSAndreas Gohr
120605f8e8dSAndreas Gohr        $outdir = rtrim($outdir, '/');
121605f8e8dSAndreas Gohr        @mkdir($outdir, 0777, true);
122605f8e8dSAndreas Gohr
123605f8e8dSAndreas Gohr        $extracted = array();
124605f8e8dSAndreas Gohr
125605f8e8dSAndreas Gohr        $cdir      = $this->readCentralDir();
126605f8e8dSAndreas Gohr        $pos_entry = $cdir['offset']; // begin of the central file directory
127605f8e8dSAndreas Gohr
128605f8e8dSAndreas Gohr        for ($i = 0; $i < $cdir['entries']; $i++) {
129605f8e8dSAndreas Gohr            // read file header
130605f8e8dSAndreas Gohr            @fseek($this->fh, $pos_entry);
131605f8e8dSAndreas Gohr            $header          = $this->readCentralFileHeader();
132605f8e8dSAndreas Gohr            $header['index'] = $i;
133605f8e8dSAndreas Gohr            $pos_entry       = ftell($this->fh); // position of the next file in central file directory
134605f8e8dSAndreas Gohr            fseek($this->fh, $header['offset']); // seek to beginning of file header
135605f8e8dSAndreas Gohr            $header   = $this->readFileHeader($header);
136605f8e8dSAndreas Gohr            $fileinfo = $this->header2fileinfo($header);
137605f8e8dSAndreas Gohr
138605f8e8dSAndreas Gohr            // apply strip rules
139605f8e8dSAndreas Gohr            $fileinfo->strip($strip);
140605f8e8dSAndreas Gohr
141605f8e8dSAndreas Gohr            // skip unwanted files
142605f8e8dSAndreas Gohr            if (!strlen($fileinfo->getPath()) || !$fileinfo->match($include, $exclude)) {
143605f8e8dSAndreas Gohr                continue;
144605f8e8dSAndreas Gohr            }
145605f8e8dSAndreas Gohr
146605f8e8dSAndreas Gohr            $extracted[] = $fileinfo;
147605f8e8dSAndreas Gohr
148605f8e8dSAndreas Gohr            // create output directory
149605f8e8dSAndreas Gohr            $output    = $outdir.'/'.$fileinfo->getPath();
150605f8e8dSAndreas Gohr            $directory = ($header['folder']) ? $output : dirname($output);
151605f8e8dSAndreas Gohr            @mkdir($directory, 0777, true);
152605f8e8dSAndreas Gohr
153605f8e8dSAndreas Gohr            // nothing more to do for directories
154605f8e8dSAndreas Gohr            if ($fileinfo->getIsdir()) {
155605f8e8dSAndreas Gohr                continue;
156605f8e8dSAndreas Gohr            }
157605f8e8dSAndreas Gohr
158605f8e8dSAndreas Gohr            // compressed files are written to temporary .gz file first
159605f8e8dSAndreas Gohr            if ($header['compression'] == 0) {
160605f8e8dSAndreas Gohr                $extractto = $output;
161605f8e8dSAndreas Gohr            } else {
162605f8e8dSAndreas Gohr                $extractto = $output.'.gz';
163605f8e8dSAndreas Gohr            }
164605f8e8dSAndreas Gohr
165605f8e8dSAndreas Gohr            // open file for writing
166605f8e8dSAndreas Gohr            $fp = fopen($extractto, "wb");
167605f8e8dSAndreas Gohr            if (!$fp) {
168605f8e8dSAndreas Gohr                throw new ArchiveIOException('Could not open file for writing: '.$extractto);
169605f8e8dSAndreas Gohr            }
170605f8e8dSAndreas Gohr
171605f8e8dSAndreas Gohr            // prepend compression header
172605f8e8dSAndreas Gohr            if ($header['compression'] != 0) {
173605f8e8dSAndreas Gohr                $binary_data = pack(
174605f8e8dSAndreas Gohr                    'va1a1Va1a1',
175605f8e8dSAndreas Gohr                    0x8b1f,
176605f8e8dSAndreas Gohr                    chr($header['compression']),
177605f8e8dSAndreas Gohr                    chr(0x00),
178605f8e8dSAndreas Gohr                    time(),
179605f8e8dSAndreas Gohr                    chr(0x00),
180605f8e8dSAndreas Gohr                    chr(3)
181605f8e8dSAndreas Gohr                );
182605f8e8dSAndreas Gohr                fwrite($fp, $binary_data, 10);
183605f8e8dSAndreas Gohr            }
184605f8e8dSAndreas Gohr
185605f8e8dSAndreas Gohr            // read the file and store it on disk
186605f8e8dSAndreas Gohr            $size = $header['compressed_size'];
187605f8e8dSAndreas Gohr            while ($size != 0) {
188605f8e8dSAndreas Gohr                $read_size   = ($size < 2048 ? $size : 2048);
189605f8e8dSAndreas Gohr                $buffer      = fread($this->fh, $read_size);
190605f8e8dSAndreas Gohr                $binary_data = pack('a'.$read_size, $buffer);
191605f8e8dSAndreas Gohr                fwrite($fp, $binary_data, $read_size);
192605f8e8dSAndreas Gohr                $size -= $read_size;
193605f8e8dSAndreas Gohr            }
194605f8e8dSAndreas Gohr
195605f8e8dSAndreas Gohr            // finalize compressed file
196605f8e8dSAndreas Gohr            if ($header['compression'] != 0) {
197605f8e8dSAndreas Gohr                $binary_data = pack('VV', $header['crc'], $header['size']);
198605f8e8dSAndreas Gohr                fwrite($fp, $binary_data, 8);
199605f8e8dSAndreas Gohr            }
200605f8e8dSAndreas Gohr
201605f8e8dSAndreas Gohr            // close file
202605f8e8dSAndreas Gohr            fclose($fp);
203605f8e8dSAndreas Gohr
204605f8e8dSAndreas Gohr            // unpack compressed file
205605f8e8dSAndreas Gohr            if ($header['compression'] != 0) {
206605f8e8dSAndreas Gohr                $gzp = @gzopen($extractto, 'rb');
207605f8e8dSAndreas Gohr                if (!$gzp) {
208605f8e8dSAndreas Gohr                    @unlink($extractto);
209605f8e8dSAndreas Gohr                    throw new ArchiveIOException('Failed file extracting. gzip support missing?');
210605f8e8dSAndreas Gohr                }
211605f8e8dSAndreas Gohr                $fp = @fopen($output, 'wb');
212605f8e8dSAndreas Gohr                if (!$fp) {
213605f8e8dSAndreas Gohr                    throw new ArchiveIOException('Could not open file for writing: '.$extractto);
214605f8e8dSAndreas Gohr                }
215605f8e8dSAndreas Gohr
216605f8e8dSAndreas Gohr                $size = $header['size'];
217605f8e8dSAndreas Gohr                while ($size != 0) {
218605f8e8dSAndreas Gohr                    $read_size   = ($size < 2048 ? $size : 2048);
219605f8e8dSAndreas Gohr                    $buffer      = gzread($gzp, $read_size);
220605f8e8dSAndreas Gohr                    $binary_data = pack('a'.$read_size, $buffer);
221605f8e8dSAndreas Gohr                    @fwrite($fp, $binary_data, $read_size);
222605f8e8dSAndreas Gohr                    $size -= $read_size;
223605f8e8dSAndreas Gohr                }
224605f8e8dSAndreas Gohr                fclose($fp);
225605f8e8dSAndreas Gohr                gzclose($gzp);
226605f8e8dSAndreas Gohr            }
227605f8e8dSAndreas Gohr
228605f8e8dSAndreas Gohr            touch($output, $fileinfo->getMtime());
229605f8e8dSAndreas Gohr            //FIXME what about permissions?
230605f8e8dSAndreas Gohr        }
231605f8e8dSAndreas Gohr
232605f8e8dSAndreas Gohr        $this->close();
233605f8e8dSAndreas Gohr        return $extracted;
234605f8e8dSAndreas Gohr    }
235605f8e8dSAndreas Gohr
236605f8e8dSAndreas Gohr    /**
237605f8e8dSAndreas Gohr     * Create a new ZIP file
238605f8e8dSAndreas Gohr     *
239605f8e8dSAndreas Gohr     * If $file is empty, the zip file will be created in memory
240605f8e8dSAndreas Gohr     *
241605f8e8dSAndreas Gohr     * @param string $file
242605f8e8dSAndreas Gohr     * @throws ArchiveIOException
243605f8e8dSAndreas Gohr     */
244605f8e8dSAndreas Gohr    public function create($file = '')
245605f8e8dSAndreas Gohr    {
246605f8e8dSAndreas Gohr        $this->file   = $file;
247605f8e8dSAndreas Gohr        $this->memory = '';
248605f8e8dSAndreas Gohr        $this->fh     = 0;
249605f8e8dSAndreas Gohr
250605f8e8dSAndreas Gohr        if ($this->file) {
251605f8e8dSAndreas Gohr            $this->fh = @fopen($this->file, 'wb');
252605f8e8dSAndreas Gohr
253605f8e8dSAndreas Gohr            if (!$this->fh) {
254605f8e8dSAndreas Gohr                throw new ArchiveIOException('Could not open file for writing: '.$this->file);
255605f8e8dSAndreas Gohr            }
256605f8e8dSAndreas Gohr        }
257605f8e8dSAndreas Gohr        $this->writeaccess = true;
258605f8e8dSAndreas Gohr        $this->closed      = false;
259605f8e8dSAndreas Gohr        $this->ctrl_dir    = array();
260605f8e8dSAndreas Gohr    }
261605f8e8dSAndreas Gohr
262605f8e8dSAndreas Gohr    /**
263605f8e8dSAndreas Gohr     * Add a file to the current ZIP archive using an existing file in the filesystem
264605f8e8dSAndreas Gohr     *
265605f8e8dSAndreas Gohr     * @param string          $file     path to the original file
266605f8e8dSAndreas Gohr     * @param string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data, empty to take from original
267605f8e8dSAndreas Gohr     * @throws ArchiveIOException
268605f8e8dSAndreas Gohr     */
269605f8e8dSAndreas Gohr
270605f8e8dSAndreas Gohr    /**
271605f8e8dSAndreas Gohr     * Add a file to the current archive using an existing file in the filesystem
272605f8e8dSAndreas Gohr     *
273605f8e8dSAndreas Gohr     * @param string          $file     path to the original file
274605f8e8dSAndreas Gohr     * @param string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data, empty to take from original
275605f8e8dSAndreas Gohr     * @throws ArchiveIOException
276605f8e8dSAndreas Gohr     */
277605f8e8dSAndreas Gohr    public function addFile($file, $fileinfo = '')
278605f8e8dSAndreas Gohr    {
279605f8e8dSAndreas Gohr        if (is_string($fileinfo)) {
280605f8e8dSAndreas Gohr            $fileinfo = FileInfo::fromPath($file, $fileinfo);
281605f8e8dSAndreas Gohr        }
282605f8e8dSAndreas Gohr
283605f8e8dSAndreas Gohr        if ($this->closed) {
284605f8e8dSAndreas Gohr            throw new ArchiveIOException('Archive has been closed, files can no longer be added');
285605f8e8dSAndreas Gohr        }
286605f8e8dSAndreas Gohr
287605f8e8dSAndreas Gohr        $data = @file_get_contents($file);
288605f8e8dSAndreas Gohr        if ($data === false) {
289605f8e8dSAndreas Gohr            throw new ArchiveIOException('Could not open file for reading: '.$file);
290605f8e8dSAndreas Gohr        }
291605f8e8dSAndreas Gohr
292605f8e8dSAndreas Gohr        // FIXME could we stream writing compressed data? gzwrite on a fopen handle?
293605f8e8dSAndreas Gohr        $this->addData($fileinfo, $data);
294605f8e8dSAndreas Gohr    }
295605f8e8dSAndreas Gohr
296605f8e8dSAndreas Gohr    /**
297605f8e8dSAndreas Gohr     * Add a file to the current TAR archive using the given $data as content
298605f8e8dSAndreas Gohr     *
299605f8e8dSAndreas Gohr     * @param string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data
300605f8e8dSAndreas Gohr     * @param string          $data     binary content of the file to add
301605f8e8dSAndreas Gohr     * @throws ArchiveIOException
302605f8e8dSAndreas Gohr     */
303605f8e8dSAndreas Gohr    public function addData($fileinfo, $data)
304605f8e8dSAndreas Gohr    {
305605f8e8dSAndreas Gohr        if (is_string($fileinfo)) {
306605f8e8dSAndreas Gohr            $fileinfo = new FileInfo($fileinfo);
307605f8e8dSAndreas Gohr        }
308605f8e8dSAndreas Gohr
309605f8e8dSAndreas Gohr        if ($this->closed) {
310605f8e8dSAndreas Gohr            throw new ArchiveIOException('Archive has been closed, files can no longer be added');
311605f8e8dSAndreas Gohr        }
312605f8e8dSAndreas Gohr
313*2b6c6819SAndreas Gohr        // prepare info and compress data
314605f8e8dSAndreas Gohr        $size     = strlen($data);
315605f8e8dSAndreas Gohr        $crc      = crc32($data);
316605f8e8dSAndreas Gohr        if ($this->complevel) {
317605f8e8dSAndreas Gohr            $data = gzcompress($data, $this->complevel);
318605f8e8dSAndreas Gohr            $data = substr($data, 2, -4); // strip compression headers
319605f8e8dSAndreas Gohr        }
320605f8e8dSAndreas Gohr        $csize  = strlen($data);
321605f8e8dSAndreas Gohr        $offset = $this->dataOffset();
322605f8e8dSAndreas Gohr        $name   = $fileinfo->getPath();
323*2b6c6819SAndreas Gohr        $time   = $fileinfo->getMtime();
324*2b6c6819SAndreas Gohr
325*2b6c6819SAndreas Gohr        // write local file header
326*2b6c6819SAndreas Gohr        $this->writebytes($this->makeLocalFileHeader(
327*2b6c6819SAndreas Gohr            $time,
328*2b6c6819SAndreas Gohr            $crc,
329*2b6c6819SAndreas Gohr            $size,
330*2b6c6819SAndreas Gohr            $csize,
331*2b6c6819SAndreas Gohr            $name,
332*2b6c6819SAndreas Gohr            (bool) $this->complevel
333*2b6c6819SAndreas Gohr        ));
334*2b6c6819SAndreas Gohr
335*2b6c6819SAndreas Gohr        // we store no encryption header
336605f8e8dSAndreas Gohr
337605f8e8dSAndreas Gohr        // write data
338*2b6c6819SAndreas Gohr        $this->writebytes($data);
339*2b6c6819SAndreas Gohr
340*2b6c6819SAndreas Gohr        // we store no data descriptor
341605f8e8dSAndreas Gohr
342605f8e8dSAndreas Gohr        // add info to central file directory
343*2b6c6819SAndreas Gohr        $this->ctrl_dir[] = $this->makeCentralFileRecord(
344*2b6c6819SAndreas Gohr            $offset,
345*2b6c6819SAndreas Gohr            $time,
346*2b6c6819SAndreas Gohr            $crc,
347*2b6c6819SAndreas Gohr            $size,
348*2b6c6819SAndreas Gohr            $csize,
349*2b6c6819SAndreas Gohr            $name,
350*2b6c6819SAndreas Gohr            (bool) $this->complevel
351*2b6c6819SAndreas Gohr        );
352605f8e8dSAndreas Gohr    }
353605f8e8dSAndreas Gohr
354605f8e8dSAndreas Gohr    /**
355605f8e8dSAndreas Gohr     * Add the closing footer to the archive if in write mode, close all file handles
356605f8e8dSAndreas Gohr     *
357605f8e8dSAndreas Gohr     * After a call to this function no more data can be added to the archive, for
358605f8e8dSAndreas Gohr     * read access no reading is allowed anymore
359605f8e8dSAndreas Gohr     */
360605f8e8dSAndreas Gohr    public function close()
361605f8e8dSAndreas Gohr    {
362605f8e8dSAndreas Gohr        if ($this->closed) {
363605f8e8dSAndreas Gohr            return;
364605f8e8dSAndreas Gohr        } // we did this already
365605f8e8dSAndreas Gohr
366605f8e8dSAndreas Gohr        if ($this->writeaccess) {
367*2b6c6819SAndreas Gohr            // write central directory
368605f8e8dSAndreas Gohr            $offset = $this->dataOffset();
369605f8e8dSAndreas Gohr            $ctrldir = join('', $this->ctrl_dir);
370605f8e8dSAndreas Gohr            $this->writebytes($ctrldir);
371*2b6c6819SAndreas Gohr
372*2b6c6819SAndreas Gohr            // write end of central directory record
373*2b6c6819SAndreas Gohr            $this->writebytes("\x50\x4b\x05\x06"); // end of central dir signature
374*2b6c6819SAndreas Gohr            $this->writebytes(pack('v', 0)); // number of this disk
375*2b6c6819SAndreas Gohr            $this->writebytes(pack('v', 0)); // number of the disk with the start of the central directory
376*2b6c6819SAndreas Gohr            $this->writebytes(pack('v',
377*2b6c6819SAndreas Gohr                count($this->ctrl_dir))); // total number of entries in the central directory on this disk
378*2b6c6819SAndreas Gohr            $this->writebytes(pack('v', count($this->ctrl_dir))); // total number of entries in the central directory
379*2b6c6819SAndreas Gohr            $this->writebytes(pack('V', strlen($ctrldir))); // size of the central directory
380*2b6c6819SAndreas Gohr            $this->writebytes(pack('V',
381*2b6c6819SAndreas Gohr                $offset)); // offset of start of central directory with respect to the starting disk number
382*2b6c6819SAndreas Gohr            $this->writebytes(pack('v', 0)); // .ZIP file comment length
383*2b6c6819SAndreas Gohr
384605f8e8dSAndreas Gohr            $this->ctrl_dir = array();
385605f8e8dSAndreas Gohr        }
386605f8e8dSAndreas Gohr
387605f8e8dSAndreas Gohr        // close file handles
388605f8e8dSAndreas Gohr        if ($this->file) {
389605f8e8dSAndreas Gohr            fclose($this->fh);
390605f8e8dSAndreas Gohr            $this->file = '';
391605f8e8dSAndreas Gohr            $this->fh   = 0;
392605f8e8dSAndreas Gohr        }
393605f8e8dSAndreas Gohr
394605f8e8dSAndreas Gohr        $this->writeaccess = false;
395605f8e8dSAndreas Gohr        $this->closed      = true;
396605f8e8dSAndreas Gohr    }
397605f8e8dSAndreas Gohr
398605f8e8dSAndreas Gohr    /**
399605f8e8dSAndreas Gohr     * Returns the created in-memory archive data
400605f8e8dSAndreas Gohr     *
401605f8e8dSAndreas Gohr     * This implicitly calls close() on the Archive
402605f8e8dSAndreas Gohr     */
403605f8e8dSAndreas Gohr    public function getArchive()
404605f8e8dSAndreas Gohr    {
405605f8e8dSAndreas Gohr        $this->close();
406605f8e8dSAndreas Gohr
407605f8e8dSAndreas Gohr        return $this->memory;
408605f8e8dSAndreas Gohr    }
409605f8e8dSAndreas Gohr
410605f8e8dSAndreas Gohr    /**
411605f8e8dSAndreas Gohr     * Save the created in-memory archive data
412605f8e8dSAndreas Gohr     *
413605f8e8dSAndreas Gohr     * Note: It's more memory effective to specify the filename in the create() function and
414605f8e8dSAndreas Gohr     * let the library work on the new file directly.
415605f8e8dSAndreas Gohr     *
416605f8e8dSAndreas Gohr     * @param     $file
417605f8e8dSAndreas Gohr     * @throws ArchiveIOException
418605f8e8dSAndreas Gohr     */
419605f8e8dSAndreas Gohr    public function save($file)
420605f8e8dSAndreas Gohr    {
421605f8e8dSAndreas Gohr        if (!file_put_contents($file, $this->getArchive())) {
422605f8e8dSAndreas Gohr            throw new ArchiveIOException('Could not write to file: '.$file);
423605f8e8dSAndreas Gohr        }
424605f8e8dSAndreas Gohr    }
425605f8e8dSAndreas Gohr
426605f8e8dSAndreas Gohr    /**
427605f8e8dSAndreas Gohr     * Read the central directory
428605f8e8dSAndreas Gohr     *
429605f8e8dSAndreas Gohr     * This key-value list contains general information about the ZIP file
430605f8e8dSAndreas Gohr     *
431605f8e8dSAndreas Gohr     * @return array
432605f8e8dSAndreas Gohr     */
433605f8e8dSAndreas Gohr    protected function readCentralDir()
434605f8e8dSAndreas Gohr    {
435605f8e8dSAndreas Gohr        $size = filesize($this->file);
436605f8e8dSAndreas Gohr        if ($size < 277) {
437605f8e8dSAndreas Gohr            $maximum_size = $size;
438605f8e8dSAndreas Gohr        } else {
439605f8e8dSAndreas Gohr            $maximum_size = 277;
440605f8e8dSAndreas Gohr        }
441605f8e8dSAndreas Gohr
442605f8e8dSAndreas Gohr        @fseek($this->fh, $size - $maximum_size);
443605f8e8dSAndreas Gohr        $pos   = ftell($this->fh);
444605f8e8dSAndreas Gohr        $bytes = 0x00000000;
445605f8e8dSAndreas Gohr
446605f8e8dSAndreas Gohr        while ($pos < $size) {
447605f8e8dSAndreas Gohr            $byte  = @fread($this->fh, 1);
448605f8e8dSAndreas Gohr            $bytes = (($bytes << 8) & 0xFFFFFFFF) | ord($byte);
449605f8e8dSAndreas Gohr            if ($bytes == 0x504b0506) {
450605f8e8dSAndreas Gohr                break;
451605f8e8dSAndreas Gohr            }
452605f8e8dSAndreas Gohr            $pos++;
453605f8e8dSAndreas Gohr        }
454605f8e8dSAndreas Gohr
455605f8e8dSAndreas Gohr        $data = unpack(
456605f8e8dSAndreas Gohr            'vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size',
457605f8e8dSAndreas Gohr            fread($this->fh, 18)
458605f8e8dSAndreas Gohr        );
459605f8e8dSAndreas Gohr
460605f8e8dSAndreas Gohr        if ($data['comment_size'] != 0) {
461605f8e8dSAndreas Gohr            $centd['comment'] = fread($this->fh, $data['comment_size']);
462605f8e8dSAndreas Gohr        } else {
463605f8e8dSAndreas Gohr            $centd['comment'] = '';
464605f8e8dSAndreas Gohr        }
465605f8e8dSAndreas Gohr        $centd['entries']      = $data['entries'];
466605f8e8dSAndreas Gohr        $centd['disk_entries'] = $data['disk_entries'];
467605f8e8dSAndreas Gohr        $centd['offset']       = $data['offset'];
468605f8e8dSAndreas Gohr        $centd['disk_start']   = $data['disk_start'];
469605f8e8dSAndreas Gohr        $centd['size']         = $data['size'];
470605f8e8dSAndreas Gohr        $centd['disk']         = $data['disk'];
471605f8e8dSAndreas Gohr        return $centd;
472605f8e8dSAndreas Gohr    }
473605f8e8dSAndreas Gohr
474605f8e8dSAndreas Gohr    /**
475605f8e8dSAndreas Gohr     * Read the next central file header
476605f8e8dSAndreas Gohr     *
477605f8e8dSAndreas Gohr     * Assumes the current file pointer is pointing at the right position
478605f8e8dSAndreas Gohr     *
479605f8e8dSAndreas Gohr     * @return array
480605f8e8dSAndreas Gohr     */
481605f8e8dSAndreas Gohr    protected function readCentralFileHeader()
482605f8e8dSAndreas Gohr    {
483605f8e8dSAndreas Gohr        $binary_data = fread($this->fh, 46);
484605f8e8dSAndreas Gohr        $header      = unpack(
485605f8e8dSAndreas Gohr            'vchkid/vid/vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset',
486605f8e8dSAndreas Gohr            $binary_data
487605f8e8dSAndreas Gohr        );
488605f8e8dSAndreas Gohr
489605f8e8dSAndreas Gohr        if ($header['filename_len'] != 0) {
490605f8e8dSAndreas Gohr            $header['filename'] = fread($this->fh, $header['filename_len']);
491605f8e8dSAndreas Gohr        } else {
492605f8e8dSAndreas Gohr            $header['filename'] = '';
493605f8e8dSAndreas Gohr        }
494605f8e8dSAndreas Gohr
495605f8e8dSAndreas Gohr        if ($header['extra_len'] != 0) {
496605f8e8dSAndreas Gohr            $header['extra'] = fread($this->fh, $header['extra_len']);
497605f8e8dSAndreas Gohr        } else {
498605f8e8dSAndreas Gohr            $header['extra'] = '';
499605f8e8dSAndreas Gohr        }
500605f8e8dSAndreas Gohr
501605f8e8dSAndreas Gohr        if ($header['comment_len'] != 0) {
502605f8e8dSAndreas Gohr            $header['comment'] = fread($this->fh, $header['comment_len']);
503605f8e8dSAndreas Gohr        } else {
504605f8e8dSAndreas Gohr            $header['comment'] = '';
505605f8e8dSAndreas Gohr        }
506605f8e8dSAndreas Gohr
507*2b6c6819SAndreas Gohr        $header['mtime']           = $this->makeUnixTime($header['mdate'], $header['mtime']);
508605f8e8dSAndreas Gohr        $header['stored_filename'] = $header['filename'];
509605f8e8dSAndreas Gohr        $header['status']          = 'ok';
510605f8e8dSAndreas Gohr        if (substr($header['filename'], -1) == '/') {
511605f8e8dSAndreas Gohr            $header['external'] = 0x41FF0010;
512605f8e8dSAndreas Gohr        }
513605f8e8dSAndreas Gohr        $header['folder'] = ($header['external'] == 0x41FF0010 || $header['external'] == 16) ? 1 : 0;
514605f8e8dSAndreas Gohr
515605f8e8dSAndreas Gohr        return $header;
516605f8e8dSAndreas Gohr    }
517605f8e8dSAndreas Gohr
518605f8e8dSAndreas Gohr    /**
519605f8e8dSAndreas Gohr     * Reads the local file header
520605f8e8dSAndreas Gohr     *
521605f8e8dSAndreas Gohr     * This header precedes each individual file inside the zip file. Assumes the current file pointer is pointing at
522*2b6c6819SAndreas Gohr     * the right position already. Enhances the given central header with the data found at the local header.
523605f8e8dSAndreas Gohr     *
524605f8e8dSAndreas Gohr     * @param array $header the central file header read previously (see above)
525605f8e8dSAndreas Gohr     * @return array
526605f8e8dSAndreas Gohr     */
527*2b6c6819SAndreas Gohr    protected function readFileHeader($header)
528605f8e8dSAndreas Gohr    {
529605f8e8dSAndreas Gohr        $binary_data = fread($this->fh, 30);
530605f8e8dSAndreas Gohr        $data        = unpack(
531605f8e8dSAndreas Gohr            'vchk/vid/vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len',
532605f8e8dSAndreas Gohr            $binary_data
533605f8e8dSAndreas Gohr        );
534605f8e8dSAndreas Gohr
535605f8e8dSAndreas Gohr        $header['filename'] = fread($this->fh, $data['filename_len']);
536605f8e8dSAndreas Gohr        if ($data['extra_len'] != 0) {
537605f8e8dSAndreas Gohr            $header['extra'] = fread($this->fh, $data['extra_len']);
538605f8e8dSAndreas Gohr        } else {
539605f8e8dSAndreas Gohr            $header['extra'] = '';
540605f8e8dSAndreas Gohr        }
541605f8e8dSAndreas Gohr
542605f8e8dSAndreas Gohr        $header['compression'] = $data['compression'];
543605f8e8dSAndreas Gohr        foreach (array(
544605f8e8dSAndreas Gohr                     'size',
545605f8e8dSAndreas Gohr                     'compressed_size',
546605f8e8dSAndreas Gohr                     'crc'
547605f8e8dSAndreas Gohr                 ) as $hd) { // On ODT files, these headers are 0. Keep the previous value.
548605f8e8dSAndreas Gohr            if ($data[$hd] != 0) {
549605f8e8dSAndreas Gohr                $header[$hd] = $data[$hd];
550605f8e8dSAndreas Gohr            }
551605f8e8dSAndreas Gohr        }
552605f8e8dSAndreas Gohr        $header['flag']  = $data['flag'];
553*2b6c6819SAndreas Gohr        $header['mtime'] = $this->makeUnixTime($data['mdate'], $data['mtime']);
554605f8e8dSAndreas Gohr
555605f8e8dSAndreas Gohr        $header['stored_filename'] = $header['filename'];
556605f8e8dSAndreas Gohr        $header['status']          = "ok";
557605f8e8dSAndreas Gohr        $header['folder']          = ($header['external'] == 0x41FF0010 || $header['external'] == 16) ? 1 : 0;
558605f8e8dSAndreas Gohr        return $header;
559605f8e8dSAndreas Gohr    }
560605f8e8dSAndreas Gohr
561605f8e8dSAndreas Gohr    /**
562605f8e8dSAndreas Gohr     * Create fileinfo object from header data
563605f8e8dSAndreas Gohr     *
564605f8e8dSAndreas Gohr     * @param $header
565605f8e8dSAndreas Gohr     * @return FileInfo
566605f8e8dSAndreas Gohr     */
567605f8e8dSAndreas Gohr    protected function header2fileinfo($header)
568605f8e8dSAndreas Gohr    {
569605f8e8dSAndreas Gohr        $fileinfo = new FileInfo();
570605f8e8dSAndreas Gohr        $fileinfo->setPath($header['filename']);
571605f8e8dSAndreas Gohr        $fileinfo->setSize($header['size']);
572605f8e8dSAndreas Gohr        $fileinfo->setCompressedSize($header['compressed_size']);
573605f8e8dSAndreas Gohr        $fileinfo->setMtime($header['mtime']);
574605f8e8dSAndreas Gohr        $fileinfo->setComment($header['comment']);
575605f8e8dSAndreas Gohr        $fileinfo->setIsdir($header['external'] == 0x41FF0010 || $header['external'] == 16);
576605f8e8dSAndreas Gohr        return $fileinfo;
577605f8e8dSAndreas Gohr    }
578605f8e8dSAndreas Gohr
579605f8e8dSAndreas Gohr    /**
580605f8e8dSAndreas Gohr     * Write to the open filepointer or memory
581605f8e8dSAndreas Gohr     *
582605f8e8dSAndreas Gohr     * @param string $data
583605f8e8dSAndreas Gohr     * @throws ArchiveIOException
584605f8e8dSAndreas Gohr     * @return int number of bytes written
585605f8e8dSAndreas Gohr     */
586605f8e8dSAndreas Gohr    protected function writebytes($data)
587605f8e8dSAndreas Gohr    {
588605f8e8dSAndreas Gohr        if (!$this->file) {
589605f8e8dSAndreas Gohr            $this->memory .= $data;
590605f8e8dSAndreas Gohr            $written = strlen($data);
591605f8e8dSAndreas Gohr        } else {
592605f8e8dSAndreas Gohr            $written = @fwrite($this->fh, $data);
593605f8e8dSAndreas Gohr        }
594605f8e8dSAndreas Gohr        if ($written === false) {
595605f8e8dSAndreas Gohr            throw new ArchiveIOException('Failed to write to archive stream');
596605f8e8dSAndreas Gohr        }
597605f8e8dSAndreas Gohr        return $written;
598605f8e8dSAndreas Gohr    }
599605f8e8dSAndreas Gohr
600605f8e8dSAndreas Gohr    /**
601605f8e8dSAndreas Gohr     * Current data pointer position
602605f8e8dSAndreas Gohr     *
603605f8e8dSAndreas Gohr     * @fixme might need a -1
604605f8e8dSAndreas Gohr     * @return int
605605f8e8dSAndreas Gohr     */
606605f8e8dSAndreas Gohr    protected function dataOffset()
607605f8e8dSAndreas Gohr    {
608605f8e8dSAndreas Gohr        if ($this->file) {
609605f8e8dSAndreas Gohr            return ftell($this->fh);
610605f8e8dSAndreas Gohr        } else {
611605f8e8dSAndreas Gohr            return strlen($this->memory);
612605f8e8dSAndreas Gohr        }
613605f8e8dSAndreas Gohr    }
614605f8e8dSAndreas Gohr
615605f8e8dSAndreas Gohr    /**
616605f8e8dSAndreas Gohr     * Create a DOS timestamp from a UNIX timestamp
617605f8e8dSAndreas Gohr     *
618605f8e8dSAndreas Gohr     * DOS timestamps start at 1980-01-01, earlier UNIX stamps will be set to this date
619605f8e8dSAndreas Gohr     *
620605f8e8dSAndreas Gohr     * @param $time
621605f8e8dSAndreas Gohr     * @return int
622605f8e8dSAndreas Gohr     */
623605f8e8dSAndreas Gohr    protected function makeDosTime($time)
624605f8e8dSAndreas Gohr    {
625605f8e8dSAndreas Gohr        $timearray = getdate($time);
626605f8e8dSAndreas Gohr        if ($timearray['year'] < 1980) {
627605f8e8dSAndreas Gohr            $timearray['year']    = 1980;
628605f8e8dSAndreas Gohr            $timearray['mon']     = 1;
629605f8e8dSAndreas Gohr            $timearray['mday']    = 1;
630605f8e8dSAndreas Gohr            $timearray['hours']   = 0;
631605f8e8dSAndreas Gohr            $timearray['minutes'] = 0;
632605f8e8dSAndreas Gohr            $timearray['seconds'] = 0;
633605f8e8dSAndreas Gohr        }
634605f8e8dSAndreas Gohr        return (($timearray['year'] - 1980) << 25) |
635605f8e8dSAndreas Gohr        ($timearray['mon'] << 21) |
636605f8e8dSAndreas Gohr        ($timearray['mday'] << 16) |
637605f8e8dSAndreas Gohr        ($timearray['hours'] << 11) |
638605f8e8dSAndreas Gohr        ($timearray['minutes'] << 5) |
639605f8e8dSAndreas Gohr        ($timearray['seconds'] >> 1);
640605f8e8dSAndreas Gohr    }
641605f8e8dSAndreas Gohr
642*2b6c6819SAndreas Gohr    /**
643*2b6c6819SAndreas Gohr     * Create a UNIX timestamp from a DOS timestamp
644*2b6c6819SAndreas Gohr     *
645*2b6c6819SAndreas Gohr     * @param $mdate
646*2b6c6819SAndreas Gohr     * @param $mtime
647*2b6c6819SAndreas Gohr     * @return int
648*2b6c6819SAndreas Gohr     */
649*2b6c6819SAndreas Gohr    protected function makeUnixTime($mdate = null, $mtime = null)
650*2b6c6819SAndreas Gohr    {
651*2b6c6819SAndreas Gohr        if ($mdate && $mtime) {
652*2b6c6819SAndreas Gohr            $year = (($mdate & 0xFE00) >> 9) + 1980;
653*2b6c6819SAndreas Gohr            $month = ($mdate & 0x01E0) >> 5;
654*2b6c6819SAndreas Gohr            $day = $mdate & 0x001F;
655*2b6c6819SAndreas Gohr
656*2b6c6819SAndreas Gohr            $hour = ($mtime & 0xF800) >> 11;
657*2b6c6819SAndreas Gohr            $minute = ($mtime & 0x07E0) >> 5;
658*2b6c6819SAndreas Gohr            $seconde = ($mtime & 0x001F) << 1;
659*2b6c6819SAndreas Gohr
660*2b6c6819SAndreas Gohr            $mtime = mktime($hour, $minute, $seconde, $month, $day, $year);
661*2b6c6819SAndreas Gohr        } else {
662*2b6c6819SAndreas Gohr            $mtime = time();
663*2b6c6819SAndreas Gohr        }
664*2b6c6819SAndreas Gohr
665*2b6c6819SAndreas Gohr        return $mtime;
666*2b6c6819SAndreas Gohr    }
667*2b6c6819SAndreas Gohr
668*2b6c6819SAndreas Gohr    /**
669*2b6c6819SAndreas Gohr     * Returns a local file header for the given data
670*2b6c6819SAndreas Gohr     *
671*2b6c6819SAndreas Gohr     * @param int $offset location of the local header
672*2b6c6819SAndreas Gohr     * @param int $ts unix timestamp
673*2b6c6819SAndreas Gohr     * @param int $crc CRC32 checksum of the uncompressed data
674*2b6c6819SAndreas Gohr     * @param int $len length of the uncompressed data
675*2b6c6819SAndreas Gohr     * @param int $clen length of the compressed data
676*2b6c6819SAndreas Gohr     * @param string $name file name
677*2b6c6819SAndreas Gohr     * @param boolean|null $comp if compression is used, if null it's determined from $len != $clen
678*2b6c6819SAndreas Gohr     * @return string
679*2b6c6819SAndreas Gohr     */
680*2b6c6819SAndreas Gohr    protected function makeCentralFileRecord($offset, $ts, $crc, $len, $clen, $name, $comp = null)
681*2b6c6819SAndreas Gohr    {
682*2b6c6819SAndreas Gohr        if(is_null($comp)) $comp = $len != $clen;
683*2b6c6819SAndreas Gohr        $comp = $comp ? 8 : 0;
684*2b6c6819SAndreas Gohr        $dtime = dechex($this->makeDosTime($ts));
685*2b6c6819SAndreas Gohr
686*2b6c6819SAndreas Gohr        $header = "\x50\x4b\x01\x02"; // central file header signature
687*2b6c6819SAndreas Gohr        $header .= pack('v', 14); // version made by - VFAT
688*2b6c6819SAndreas Gohr        $header .= pack('v', 20); // version needed to extract - 2.0
689*2b6c6819SAndreas Gohr        $header .= pack('v', 0); // general purpose flag - no flags set
690*2b6c6819SAndreas Gohr        $header .= pack('v', $comp); // compression method - deflate|none
691*2b6c6819SAndreas Gohr        $header .= pack(
692*2b6c6819SAndreas Gohr            'H*',
693*2b6c6819SAndreas Gohr            $dtime[6] . $dtime[7] .
694*2b6c6819SAndreas Gohr            $dtime[4] . $dtime[5] .
695*2b6c6819SAndreas Gohr            $dtime[2] . $dtime[3] .
696*2b6c6819SAndreas Gohr            $dtime[0] . $dtime[1]
697*2b6c6819SAndreas Gohr        ); //  last mod file time and date
698*2b6c6819SAndreas Gohr        $header .= pack('V', $crc); // crc-32
699*2b6c6819SAndreas Gohr        $header .= pack('V', $clen); // compressed size
700*2b6c6819SAndreas Gohr        $header .= pack('V', $len); // uncompressed size
701*2b6c6819SAndreas Gohr        $header .= pack('v', strlen($name)); // file name length
702*2b6c6819SAndreas Gohr        $header .= pack('v', 0); // extra field length
703*2b6c6819SAndreas Gohr        $header .= pack('v', 0); // file comment length
704*2b6c6819SAndreas Gohr        $header .= pack('v', 0); // disk number start
705*2b6c6819SAndreas Gohr        $header .= pack('v', 0); // internal file attributes
706*2b6c6819SAndreas Gohr        $header .= pack('V', 0); // external file attributes  @todo was 0x32!?
707*2b6c6819SAndreas Gohr        $header .= pack('V', $offset); // relative offset of local header
708*2b6c6819SAndreas Gohr        $header .= $name; // file name
709*2b6c6819SAndreas Gohr
710*2b6c6819SAndreas Gohr        return $header;
711*2b6c6819SAndreas Gohr    }
712*2b6c6819SAndreas Gohr
713*2b6c6819SAndreas Gohr    /**
714*2b6c6819SAndreas Gohr     * Returns a local file header for the given data
715*2b6c6819SAndreas Gohr     *
716*2b6c6819SAndreas Gohr     * @param int $ts unix timestamp
717*2b6c6819SAndreas Gohr     * @param int $crc CRC32 checksum of the uncompressed data
718*2b6c6819SAndreas Gohr     * @param int $len length of the uncompressed data
719*2b6c6819SAndreas Gohr     * @param int $clen length of the compressed data
720*2b6c6819SAndreas Gohr     * @param string $name file name
721*2b6c6819SAndreas Gohr     * @param boolean|null $comp if compression is used, if null it's determined from $len != $clen
722*2b6c6819SAndreas Gohr     * @return string
723*2b6c6819SAndreas Gohr     */
724*2b6c6819SAndreas Gohr    protected function makeLocalFileHeader($ts, $crc, $len, $clen, $name, $comp = null)
725*2b6c6819SAndreas Gohr    {
726*2b6c6819SAndreas Gohr        if(is_null($comp)) $comp = $len != $clen;
727*2b6c6819SAndreas Gohr        $comp = $comp ? 8 : 0;
728*2b6c6819SAndreas Gohr        $dtime = dechex($this->makeDosTime($ts));
729*2b6c6819SAndreas Gohr
730*2b6c6819SAndreas Gohr        $header = "\x50\x4b\x03\x04"; //  local file header signature
731*2b6c6819SAndreas Gohr        $header .= pack('v', 20); // version needed to extract - 2.0
732*2b6c6819SAndreas Gohr        $header .= pack('v', 0); // general purpose flag - no flags set
733*2b6c6819SAndreas Gohr        $header .= pack('v', $comp); // compression method - deflate|none
734*2b6c6819SAndreas Gohr        $header .= pack(
735*2b6c6819SAndreas Gohr            'H*',
736*2b6c6819SAndreas Gohr            $dtime[6] . $dtime[7] .
737*2b6c6819SAndreas Gohr            $dtime[4] . $dtime[5] .
738*2b6c6819SAndreas Gohr            $dtime[2] . $dtime[3] .
739*2b6c6819SAndreas Gohr            $dtime[0] . $dtime[1]
740*2b6c6819SAndreas Gohr        ); //  last mod file time and date
741*2b6c6819SAndreas Gohr        $header .= pack('V', $crc); // crc-32
742*2b6c6819SAndreas Gohr        $header .= pack('V', $clen); // compressed size
743*2b6c6819SAndreas Gohr        $header .= pack('V', $len); // uncompressed size
744*2b6c6819SAndreas Gohr        $header .= pack('v', strlen($name)); // file name length
745*2b6c6819SAndreas Gohr        $header .= pack('v', 0); // extra field length
746*2b6c6819SAndreas Gohr        $header .= $name;
747*2b6c6819SAndreas Gohr        return $header;
748*2b6c6819SAndreas Gohr    }
749605f8e8dSAndreas Gohr}
750