Test Failed
Push — master ( de8f0c...763734 )
by Giancarlos
03:36
created

ZipFile   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 89.55%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 139
ccs 60
cts 67
cp 0.8955
rs 10
c 0
b 0
f 0

3 Methods

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