zipfile::addFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 65
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 45
c 3
b 0
f 0
nc 1
nop 3
dl 0
loc 65
rs 9.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
*******************************************************
4
***													***
5
*** backpack										***
6
*** Cedric MONTUY pour CHG-WEB                      ***
7
*** Original author : Yoshi Sakai					***
8
***													***
9
*******************************************************
10
*/
11
12
class zipfile
13
{
14
    public $datasec      = [];
15
    public $ctrl_dir     = [];
16
    public $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
17
    public $old_offset   = 0;
18
19
    public function __construct()
20
    {
21
    }
22
23
    protected function unix2DosTime($unixtime = 0)
24
    {
25
        $timearray = (0 == $unixtime) ? getdate() : getdate($unixtime);
26
27
        if ($timearray['year'] < 1980) {
28
            $timearray['year']    = 1980;
29
            $timearray['mon']     = 1;
30
            $timearray['mday']    = 1;
31
            $timearray['hours']   = 0;
32
            $timearray['minutes'] = 0;
33
            $timearray['seconds'] = 0;
34
        } // end if
35
36
        return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) | ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
37
    } // end of the 'unix2DosTime()' method
38
39
    public function addFile($data, $name, $time = 0)
40
    {
41
        $name = str_replace('\\', '/', $name);
42
43
        $dtime    = dechex($this->unix2DosTime($time));
44
        $hexdtime = '\x' . $dtime[6] . $dtime[7] . '\x' . $dtime[4] . $dtime[5] . '\x' . $dtime[2] . $dtime[3] . '\x' . $dtime[0] . $dtime[1];
45
        eval('$hexdtime = \'' . $hexdtime . '\';');
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
46
47
        $fr = "\x50\x4b\x03\x04";
48
        $fr .= "\x14\x00";            // ver needed to extract
49
        $fr .= "\x00\x00";            // gen purpose bit flag
50
        $fr .= "\x08\x00";            // compression method
51
        $fr .= $hexdtime;             // last mod time and date
52
53
        // "local file header" segment
54
        $unc_len = strlen($data);
55
        $crc     = crc32($data);
56
        $zdata   = gzcompress($data);
57
        $zdata   = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
58
        $c_len   = strlen($zdata);
59
        $fr      .= pack('V', $crc);             // crc32
60
        $fr      .= pack('V', $c_len);           // compressed filesize
61
        $fr      .= pack('V', $unc_len);         // uncompressed filesize
62
        $fr      .= pack('v', strlen($name));    // length of filename
63
        $fr      .= pack('v', 0);                // extra field length
64
        $fr      .= $name;
65
66
        // "file data" segment
67
        $fr .= $zdata;
68
69
        // "data descriptor" segment (optional but necessary if archive is not
70
        // served as file)
71
        $fr .= pack('V', $crc);                 // crc32
72
        $fr .= pack('V', $c_len);               // compressed filesize
73
        $fr .= pack('V', $unc_len);             // uncompressed filesize
74
75
        // add this entry to array
76
        $this->datasec[] = $fr;
77
        $new_offset      = strlen(implode('', $this->datasec));
78
79
        // now add to central directory record
80
        $cdrec = "\x50\x4b\x01\x02";
81
        $cdrec .= "\x00\x00";                // version made by
82
        $cdrec .= "\x14\x00";                // version needed to extract
83
        $cdrec .= "\x00\x00";                // gen purpose bit flag
84
        $cdrec .= "\x08\x00";                // compression method
85
        $cdrec .= $hexdtime;                 // last mod time & date
86
        $cdrec .= pack('V', $crc);           // crc32
87
        $cdrec .= pack('V', $c_len);         // compressed filesize
88
        $cdrec .= pack('V', $unc_len);       // uncompressed filesize
89
        $cdrec .= pack('v', strlen($name)); // length of filename
90
        $cdrec .= pack('v', 0);             // extra field length
91
        $cdrec .= pack('v', 0);             // file comment length
92
        $cdrec .= pack('v', 0);             // disk number start
93
        $cdrec .= pack('v', 0);             // internal file attributes
94
        $cdrec .= pack('V', 32);            // external file attributes - 'archive' bit set
95
96
        $cdrec            .= pack('V', $this->old_offset); // relative offset of local header
97
        $this->old_offset = $new_offset;
98
99
        $cdrec .= $name;
100
101
        // optional extra field, file comment goes here
102
        // save to central directory
103
        $this->ctrl_dir[] = $cdrec;
104
    } // end of the 'addFile()' method
105
106
    public function file()
107
    {
108
        $data    = implode('', $this->datasec);
109
        $ctrldir = implode('', $this->ctrl_dir);
110
111
        return $data . $ctrldir . $this->eof_ctrl_dir . pack('v', count($this->ctrl_dir)) .  // total # of entries "on this disk"
112
               pack('v', count($this->ctrl_dir)) .  // total # of entries overall
113
               pack('V', strlen($ctrldir)) .           // size of central dir
114
               pack('V', strlen($data)) .              // offset to start of central dir
115
               "\x00\x00";                             // .zip file comment length
116
    } // end of the 'file()' method
117
} // end of the 'zipfile' class
118