Passed
Push — master ( d54f6c...8277b0 )
by Giancarlos
02:59
created

ZipFile::addFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 51
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 43
cts 43
cp 1
rs 9.4109
c 0
b 0
f 0
cc 2
eloc 41
nc 2
nop 3
crap 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
     * Whether to echo zip as it's built or return as string from -> file.
18
     *
19
     * @private  boolean  $doWrite
20
     */
21
    private $doWrite = false;
22
    /**
23
     * Array to store compressed data.
24
     *
25
     * @private  array    $datasec
26
     */
27
    private $datasec = array();
28
    /**
29
     * Central directory.
30
     *
31
     * @private  array    $ctrl_dir
32
     */
33
    private $ctrl_dir = array();
34
    /**
35
     * End of central directory record.
36
     *
37
     * @private  string   $eof_ctrl_dir
38
     */
39
    private $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
40
    /**
41
     * Last offset position.
42
     *
43
     * @private  integer  $old_offset
44
     */
45
    private $old_offset = 0;
46
47
    /**
48
     * Sets member privateiable this -> doWrite to true
49
     * - Should be called immediately after class instantiantion
50
     * - If set to true, then ZIP archive are echo'ed to STDOUT as each
51
     *   file is added via this -> addfile(), and central directories are
52
     *   echoed to STDOUT on final call to this -> file().  Also,
53
     *   this -> file() returns an empty string so it is safe to issue a
54
     *   "echo $zipfile;" command.
55
     */
56
    public function setDoWrite()
57
    {
58
        $this->doWrite = true;
59
    }
60
61
 // end of the 'setDoWrite()' method
62
63
    /**
64
     * Converts an Unix timestamp to a four byte DOS date and time format (date
65
     * in high two bytes, time in low two bytes allowing magnitude comparison).
66
     *
67
     * @param int $unixtime the current Unix timestamp
68
     *
69
     * @return int the current date in a four byte DOS format
70
     */
71 6
    public function unix2DosTime($unixtime = 0)
72
    {
73 6
        $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
74 6
        if ($timearray['year'] < 1980) {
75
            $timearray['year'] = 1980;
76
            $timearray['mon'] = 1;
77
            $timearray['mday'] = 1;
78
            $timearray['hours'] = 0;
79
            $timearray['minutes'] = 0;
80
            $timearray['seconds'] = 0;
81
        } // end if
82 6
        return (($timearray['year'] - 1980) << 25)
83 6
            | ($timearray['mon'] << 21)
84 6
            | ($timearray['mday'] << 16)
85 6
            | ($timearray['hours'] << 11)
86 6
            | ($timearray['minutes'] << 5)
87 6
            | ($timearray['seconds'] >> 1);
88
    }
89
90
 // end of the 'unix2DosTime()' method
91
92
    /**
93
     * Adds "file" to archive.
94
     *
95
     * @param string $data file contents
96
     * @param string $name name of the file in the archive (may contains the path)
97
     * @param int    $time the current timestamp
98
     */
99 6
    public function addFile($data, $name, $time = 0)
100
    {
101 6
        $name = str_replace('\\', '/', $name);
102 6
        $hexdtime = pack('V', $this->unix2DosTime($time));
103 6
        $frd = "\x50\x4b\x03\x04";
104 6
        $frd .= "\x14\x00";            // ver needed to extract
105 6
        $frd .= "\x00\x00";            // gen purpose bit flag
106 6
        $frd .= "\x08\x00";            // compression method
107 6
        $frd .= $hexdtime;             // last mod time and date
108
        // "local file header" segment
109 6
        $unc_len = strlen($data);
110 6
        $crc = crc32($data);
111 6
        $zdata = gzcompress($data);
112 6
        $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
113 6
        $c_len = strlen($zdata);
114 6
        $frd .= pack('V', $crc);             // crc32
115 6
        $frd .= pack('V', $c_len);           // compressed filesize
116 6
        $frd .= pack('V', $unc_len);         // uncompressed filesize
117 6
        $frd .= pack('v', strlen($name));    // length of filename
118 6
        $frd .= pack('v', 0);                // extra field length
119 6
        $frd .= $name;
120
        // "file data" segment
121 6
        $frd .= $zdata;
122
        // echo this entry on the fly, ...
123 6
        if (!$this->doWrite) {
124 6
            $this->datasec[] = $frd;
125 6
        }
126
        // now add to central directory record
127 6
        $cdrec = "\x50\x4b\x01\x02";
128 6
        $cdrec .= "\x00\x00";                // version made by
129 6
        $cdrec .= "\x14\x00";                // version needed to extract
130 6
        $cdrec .= "\x00\x00";                // gen purpose bit flag
131 6
        $cdrec .= "\x08\x00";                // compression method
132 6
        $cdrec .= $hexdtime;                 // last mod time & date
133 6
        $cdrec .= pack('V', $crc);           // crc32
134 6
        $cdrec .= pack('V', $c_len);         // compressed filesize
135 6
        $cdrec .= pack('V', $unc_len);       // uncompressed filesize
136 6
        $cdrec .= pack('v', strlen($name)); // length of filename
137 6
        $cdrec .= pack('v', 0);             // extra field length
138 6
        $cdrec .= pack('v', 0);             // file comment length
139 6
        $cdrec .= pack('v', 0);             // disk number start
140 6
        $cdrec .= pack('v', 0);             // internal file attributes
141 6
        $cdrec .= pack('V', 32);            // external file attributes
142
        // - 'archive' bit set
143 6
        $cdrec .= pack('V', $this->old_offset); // relative offset of local header
144 6
        $this->old_offset += strlen($frd);
145 6
        $cdrec .= $name;
146
        // optional extra field, file comment goes here
147
        // save to central directory
148 6
        $this->ctrl_dir[] = $cdrec;
149 6
    }
150
151
 // end of the 'addFile()' method
152
153
    /**
154
     * Echo central dir if ->doWrite==true, else build string to return.
155
     *
156
     * @return string if ->doWrite {empty string} else the ZIP file contents
157
     */
158 6
    public function file()
159
    {
160 6
        $ctrldir = implode('', $this->ctrl_dir);
161
        $header = $ctrldir.
162 6
            $this->eof_ctrl_dir.
163 6
            pack('v', sizeof($this->ctrl_dir)). //total #of entries "on this disk"
164 6
            pack('v', sizeof($this->ctrl_dir)). //total #of entries overall
165 6
            pack('V', strlen($ctrldir)).          //size of central dir
166 6
            pack('V', $this->old_offset).       //offset to start of central dir
167 6
            "\x00\x00";                            //.zip file comment length
168 6
        if ($this->doWrite) { // Send central directory & end ctrl dir to STDOUT
169
            return '';            // Return empty string
170
        }
171
172
        // Return entire ZIP archive as string
173 6
        $data = implode('', $this->datasec);
174
175 6
        return $data.$header;
176
    }
177
178
 // end of the 'file()' method
179
}
180