Test Failed
Push — master ( 11d608...27c98c )
by Giancarlos
02:46
created

ZipFile::unix2DosTime()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.7536

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 9
cts 16
cp 0.5625
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 15
nc 4
nop 1
crap 3.7536
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Giansalex
5
 * Date: 16/07/2017
6
 * Time: 13:00.
7
 */
8
9
namespace Greenter\Zip;
10
11
/**
12
 * Class ZipFile.
13
 */
14
class ZipFile
15
{
16
    /**
17
     * Array to store compressed data.
18
     *
19
     * @private  array    $datasec
20
     */
21
    private $datasec = [];
22
    /**
23
     * Central directory.
24
     *
25
     * @private  array    $ctrl_dir
26
     */
27
    private $ctrl_dir = [];
28
    /**
29
     * End of central directory record.
30
     *
31
     * @private  string   $eof_ctrl_dir
32
     */
33
    private $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
34
    /**
35
     * Last offset position.
36
     *
37
     * @private  integer  $old_offset
38
     */
39
    private $old_offset = 0;
40
41
    /**
42
     * Converts an Unix timestamp to a four byte DOS date and time format (date
43
     * in high two bytes, time in low two bytes allowing magnitude comparison).
44
     *
45
     * @param int $unixtime the current Unix timestamp
46
     *
47
     * @return int the current date in a four byte DOS format
48
     */
49 2
    public function unix2DosTime($unixtime = 0)
50
    {
51 2
        $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
52 2
        if ($timearray['year'] < 1980) {
53
            $timearray['year'] = 1980;
54
            $timearray['mon'] = 1;
55
            $timearray['mday'] = 1;
56
            $timearray['hours'] = 0;
57
            $timearray['minutes'] = 0;
58
            $timearray['seconds'] = 0;
59
        } // end if
60 2
        return (($timearray['year'] - 1980) << 25)
61 2
            | ($timearray['mon'] << 21)
62 2
            | ($timearray['mday'] << 16)
63 2
            | ($timearray['hours'] << 11)
64 2
            | ($timearray['minutes'] << 5)
65 2
            | ($timearray['seconds'] >> 1);
66
    }
67
68
 // end of the 'unix2DosTime()' method
69
70
    /**
71
     * Adds "file" to archive.
72
     *
73
     * @param string $data file contents
74
     * @param string $name name of the file in the archive (may contains the path)
75
     * @param int    $time the current timestamp
76
     */
77 2
    public function addFile($data, $name, $time = 0)
78
    {
79 2
        $name = str_replace('\\', '/', $name);
80 2
        $hexdtime = pack('V', $this->unix2DosTime($time));
81 2
        $frd = "\x50\x4b\x03\x04";
82 2
        $frd .= "\x14\x00";            // ver needed to extract
83 2
        $frd .= "\x00\x00";            // gen purpose bit flag
84 2
        $frd .= "\x08\x00";            // compression method
85 2
        $frd .= $hexdtime;             // last mod time and date
86
        // "local file header" segment
87 2
        $unc_len = strlen($data);
88 2
        $crc = crc32($data);
89 2
        $zdata = gzcompress($data);
90 2
        $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
91 2
        $c_len = strlen($zdata);
92 2
        $frd .= pack('V', $crc);             // crc32
93 2
        $frd .= pack('V', $c_len);           // compressed filesize
94 2
        $frd .= pack('V', $unc_len);         // uncompressed filesize
95 2
        $frd .= pack('v', strlen($name));    // length of filename
96 2
        $frd .= pack('v', 0);                // extra field length
97 2
        $frd .= $name;
98
        // "file data" segment
99 2
        $frd .= $zdata;
100
        // echo this entry on the fly, ...
101 2
        $this->datasec[] = $frd;
102
        // now add to central directory record
103 2
        $cdrec = "\x50\x4b\x01\x02";
104 2
        $cdrec .= "\x00\x00";                // version made by
105 2
        $cdrec .= "\x14\x00";                // version needed to extract
106 2
        $cdrec .= "\x00\x00";                // gen purpose bit flag
107 2
        $cdrec .= "\x08\x00";                // compression method
108 2
        $cdrec .= $hexdtime;                 // last mod time & date
109 2
        $cdrec .= pack('V', $crc);           // crc32
110 2
        $cdrec .= pack('V', $c_len);         // compressed filesize
111 2
        $cdrec .= pack('V', $unc_len);       // uncompressed filesize
112 2
        $cdrec .= pack('v', strlen($name)); // length of filename
113 2
        $cdrec .= pack('v', 0);             // extra field length
114 2
        $cdrec .= pack('v', 0);             // file comment length
115 2
        $cdrec .= pack('v', 0);             // disk number start
116 2
        $cdrec .= pack('v', 0);             // internal file attributes
117 2
        $cdrec .= pack('V', 32);            // external file attributes
118
        // - 'archive' bit set
119 2
        $cdrec .= pack('V', $this->old_offset); // relative offset of local header
120 2
        $this->old_offset += strlen($frd);
121 2
        $cdrec .= $name;
122
        // optional extra field, file comment goes here
123
        // save to central directory
124 2
        $this->ctrl_dir[] = $cdrec;
125 2
    }
126
127
 // end of the 'addFile()' method
128
129
    /**
130
     * Echo central dir if ->doWrite==true, else build string to return.
131
     *
132
     * @return string if ->doWrite {empty string} else the ZIP file contents
133
     */
134 2
    public function file()
135
    {
136 2
        $ctrldir = implode('', $this->ctrl_dir);
137
        $header = $ctrldir.
138 2
            $this->eof_ctrl_dir.
139 2
            pack('v', sizeof($this->ctrl_dir)). //total #of entries "on this disk"
140 2
            pack('v', sizeof($this->ctrl_dir)). //total #of entries overall
141 2
            pack('V', strlen($ctrldir)).          //size of central dir
142 2
            pack('V', $this->old_offset).       //offset to start of central dir
143 2
            "\x00\x00";                            //.zip file comment length
144
145
        // Return entire ZIP archive as string
146 2
        $data = implode('', $this->datasec);
147
148 2
        return $data.$header;
149
    }
150
151
 // end of the 'file()' method
152
}
153