Test Failed
Push — master ( a47c2b...4d6576 )
by Giancarlos
04:07
created

ZipFile::unix2DosTime()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.9379

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 9
cts 17
cp 0.5294
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 16
nc 4
nop 1
crap 3.9379
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 38
    public function unix2DosTime($unixtime = 0)
50
    {
51 38
        $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
52 38
        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
            echo "Menor de 1980";
60
        } // end if
61 38
        return (($timearray['year'] - 1980) << 25)
62 38
            | ($timearray['mon'] << 21)
63 38
            | ($timearray['mday'] << 16)
64 38
            | ($timearray['hours'] << 11)
65 38
            | ($timearray['minutes'] << 5)
66 38
            | ($timearray['seconds'] >> 1);
67
    }
68
69
 // end of the 'unix2DosTime()' method
70
71
    /**
72
     * Adds "file" to archive.
73
     *
74
     * @param string $data file contents
75
     * @param string $name name of the file in the archive (may contains the path)
76
     * @param int    $time the current timestamp
77
     */
78 38
    public function addFile($data, $name, $time = 0)
79
    {
80 38
        $name = str_replace('\\', '/', $name);
81 38
        $hexdtime = pack('V', $this->unix2DosTime($time));
82 38
        $frd = "\x50\x4b\x03\x04";
83 38
        $frd .= "\x14\x00";            // ver needed to extract
84 38
        $frd .= "\x00\x00";            // gen purpose bit flag
85 38
        $frd .= "\x08\x00";            // compression method
86 38
        $frd .= $hexdtime;             // last mod time and date
87
        // "local file header" segment
88 38
        $unc_len = strlen($data);
89 38
        $crc = crc32($data);
90 38
        $zdata = gzcompress($data);
91 38
        $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
92 38
        $c_len = strlen($zdata);
93 38
        $frd .= pack('V', $crc);             // crc32
94 38
        $frd .= pack('V', $c_len);           // compressed filesize
95 38
        $frd .= pack('V', $unc_len);         // uncompressed filesize
96 38
        $frd .= pack('v', strlen($name));    // length of filename
97 38
        $frd .= pack('v', 0);                // extra field length
98 38
        $frd .= $name;
99
        // "file data" segment
100 38
        $frd .= $zdata;
101
        // echo this entry on the fly, ...
102 38
        $this->datasec[] = $frd;
103
        // now add to central directory record
104 38
        $cdrec = "\x50\x4b\x01\x02";
105 38
        $cdrec .= "\x00\x00";                // version made by
106 38
        $cdrec .= "\x14\x00";                // version needed to extract
107 38
        $cdrec .= "\x00\x00";                // gen purpose bit flag
108 38
        $cdrec .= "\x08\x00";                // compression method
109 38
        $cdrec .= $hexdtime;                 // last mod time & date
110 38
        $cdrec .= pack('V', $crc);           // crc32
111 38
        $cdrec .= pack('V', $c_len);         // compressed filesize
112 38
        $cdrec .= pack('V', $unc_len);       // uncompressed filesize
113 38
        $cdrec .= pack('v', strlen($name)); // length of filename
114 38
        $cdrec .= pack('v', 0);             // extra field length
115 38
        $cdrec .= pack('v', 0);             // file comment length
116 38
        $cdrec .= pack('v', 0);             // disk number start
117 38
        $cdrec .= pack('v', 0);             // internal file attributes
118 38
        $cdrec .= pack('V', 32);            // external file attributes
119
        // - 'archive' bit set
120 38
        $cdrec .= pack('V', $this->old_offset); // relative offset of local header
121 38
        $this->old_offset += strlen($frd);
122 38
        $cdrec .= $name;
123
        // optional extra field, file comment goes here
124
        // save to central directory
125 38
        $this->ctrl_dir[] = $cdrec;
126 38
    }
127
128
 // end of the 'addFile()' method
129
130
    /**
131
     * Echo central dir if ->doWrite==true, else build string to return.
132
     *
133
     * @return string if ->doWrite {empty string} else the ZIP file contents
134
     */
135 38
    public function file()
136
    {
137 38
        $ctrldir = implode('', $this->ctrl_dir);
138
        $header = $ctrldir.
139 38
            $this->eof_ctrl_dir.
140 38
            pack('v', sizeof($this->ctrl_dir)). //total #of entries "on this disk"
141 38
            pack('v', sizeof($this->ctrl_dir)). //total #of entries overall
142 38
            pack('V', strlen($ctrldir)).          //size of central dir
143 38
            pack('V', $this->old_offset).       //offset to start of central dir
144 38
            "\x00\x00";                            //.zip file comment length
145
146
        // Return entire ZIP archive as string
147 38
        $data = implode('', $this->datasec);
148
149 38
        return $data.$header;
150
    }
151
152
 // end of the 'file()' method
153
}
154