Passed
Push — master ( a43e58...4ea7aa )
by Giancarlos
08:09
created

ZipFile::setDoWrite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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