~yoboy-leguesh/ubuntu-fr-doc/maj20150810a

« back to all changes in this revision

Viewing changes to inc/ZipLib.class.php

  • Committer: YoBoY
  • Date: 2015-11-11 10:05:14 UTC
  • Revision ID: yoboy.leguesh@gmail.com-20151111100514-bw7p06lrhban4g2t
Mise à jour vers Dokuwiki 2015-08-10a avec nos patchs

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 * @link       http://forum.maxg.info
7
7
 *
8
8
 * Modified for Dokuwiki
 
9
 * @deprecated 2015-05-15 - use splitbrain\PHPArchive\Zip instead
9
10
 * @author    Christopher Smith <chris@jalakai.co.uk>
10
11
 */
11
12
class ZipLib {
16
17
    var $old_offset = 0;
17
18
    var $dirs = Array(".");
18
19
 
 
20
    /**
 
21
     * @param string $zip_name filename path to file
 
22
     * @return array|int
 
23
     */
19
24
    function get_List($zip_name) {
20
25
        $zip = @fopen($zip_name, 'rb');
21
26
        if(!$zip) return(0);
24
29
        @rewind($zip);
25
30
        @fseek($zip, $centd['offset']);
26
31
 
 
32
        $ret = array();
27
33
        for ($i=0; $i<$centd['entries']; $i++) {
28
34
            $header = $this->ReadCentralFileHeaders($zip);
29
35
            $header['index'] = $i;
30
36
 
 
37
            $info = array();
31
38
            $info['filename']        = $header['filename'];
32
39
            $info['stored_filename'] = $header['stored_filename'];
33
40
            $info['size']            = $header['size'];
45
52
        return $ret;
46
53
    }
47
54
 
 
55
    /**
 
56
     * @param array $files   array filled with array(string filename, string data)
 
57
     * @param bool  $compact
 
58
     * @return array
 
59
     */
48
60
    function Add($files,$compact) {
49
61
        if(!is_array($files[0])) $files=Array($files);
50
62
 
 
63
        $ret = array();
51
64
        for($i=0;$files[$i];$i++){
52
65
            $fn = $files[$i];
53
66
            if(!in_Array(dirname($fn[0]),$this->dirs))
60
73
 
61
74
    /**
62
75
     * Zips recursively the $folder directory, from the $basedir directory
 
76
     *
 
77
     * @param string $folder filename path to file
 
78
     * @param string|null $basedir
 
79
     * @param string|null $parent
63
80
     */
64
81
    function Compress($folder, $basedir=null, $parent=null) {
65
82
        $full_path = $basedir."/".$parent.$folder;
70
87
        }
71
88
        $dir = new DirectoryIterator($full_path);
72
89
        foreach($dir as $file) {
 
90
            /** @var DirectoryIterator $file */
73
91
            if(!$file->isDot()) {
74
92
                $filename = $file->getFilename();
75
93
                if($file->isDir()) {
84
102
 
85
103
    /**
86
104
     * Returns the Zip file
 
105
     *
 
106
     * @return string
87
107
     */
88
108
    function get_file() {
89
109
        $data = implode('', $this -> datasec);
94
114
            pack('V', strlen($ctrldir)) . pack('V', strlen($data)) . "\x00\x00";
95
115
    }
96
116
 
 
117
    /**
 
118
     * @param string $name the name of the directory
 
119
     */
97
120
    function add_dir($name) {
98
121
        $name = str_replace("\\", "/", $name);
99
122
        $fr = "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00";
117
140
 
118
141
    /**
119
142
     * Add a file named $name from a string $data
 
143
     *
 
144
     * @param string $data
 
145
     * @param string $name filename
 
146
     * @param bool $compact
 
147
     * @return bool
120
148
     */
121
 
    function add_File($data, $name, $compact = 1) {
 
149
    function add_File($data, $name, $compact = true) {
122
150
        $name     = str_replace('\\', '/', $name);
123
151
        $dtime    = dechex($this->DosTime());
124
152
 
166
194
        return true;
167
195
    }
168
196
 
 
197
    /**
 
198
     * @return int
 
199
     */
169
200
    function DosTime() {
170
201
        $timearray = getdate();
171
202
        if ($timearray['year'] < 1980) {
186
217
 
187
218
    /**
188
219
     * Extract a zip file $zn to the $to directory
 
220
     *
 
221
     * @param string $zn filename
 
222
     * @param string $to filename path to file
 
223
     * @param array $index
 
224
     * @return array|int
189
225
     */
190
226
    function Extract ( $zn, $to, $index = Array(-1) ) {
191
227
        if(!@is_dir($to)) $this->_mkdir($to);
192
 
        $ok = 0;
193
228
        $zip = @fopen($zn,'rb');
194
229
        if(!$zip) return(-1);
195
230
        $cdir = $this->ReadCentralDir($zip,$zn);
203
238
                return(-1);
204
239
        }
205
240
 
 
241
        $stat = array();
206
242
        for ($i=0; $i<$cdir['entries']; $i++) {
207
243
            @fseek($zip, $pos_entry);
208
244
            $header = $this->ReadCentralFileHeaders($zip);
218
254
        return $stat;
219
255
    }
220
256
 
 
257
    /**
 
258
     * @param resource $zip
 
259
     * @param array $header
 
260
     * @return array
 
261
     */
221
262
    function ReadFileHeader($zip, $header) {
222
263
        $binary_data = fread($zip, 30);
223
264
        $data = unpack('vchk/vid/vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $binary_data);
254
295
        return $header;
255
296
    }
256
297
 
 
298
    /**
 
299
     * @param resource $zip
 
300
     * @return array
 
301
     */
257
302
    function ReadCentralFileHeaders($zip){
258
303
        $binary_data = fread($zip, 46);
259
304
        $header = unpack('vchkid/vid/vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', $binary_data);
295
340
        return $header;
296
341
    }
297
342
 
 
343
    /**
 
344
     * @param resource $zip
 
345
     * @param string   $zip_name filename path to file
 
346
     * @return array
 
347
     */
298
348
    function ReadCentralDir($zip,$zip_name) {
299
349
        $size = filesize($zip_name);
300
350
        if ($size < 277){
320
370
        $data=unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size',
321
371
                fread($zip, 18));
322
372
 
 
373
        $centd = array();
323
374
        if ($data['comment_size'] != 0){
324
375
            $centd['comment'] = fread($zip, $data['comment_size']);
325
376
        } else {
334
385
        return $centd;
335
386
    }
336
387
 
 
388
    /**
 
389
     * @param array    $header
 
390
     * @param string   $to     filename path to file
 
391
     * @param resource $zip
 
392
     * @return bool|int
 
393
     */
337
394
    function ExtractFile($header,$to,$zip) {
338
395
        $header = $this->readfileheader($zip, $header);
339
396
 
414
471
     * centralize mkdir calls and use dokuwiki io functions
415
472
     *
416
473
     * @author Christopher Smith <chris@jalakai.co.uk>
 
474
     *
 
475
     * @param string $d filename path to file
 
476
     * @return bool|int|string
417
477
     */
418
478
    function _mkdir($d) {
419
479
        return io_mkdir_p($d);
420
480
    }
421
481
 
422
 
 
 
482
    /**
 
483
     * @param string $zn
 
484
     * @param string $name
 
485
     * @return null|string
 
486
     */
423
487
    function ExtractStr($zn, $name) {
424
 
        $ok = 0;
425
488
        $zip = @fopen($zn,'rb');
426
489
        if(!$zip) return(null);
427
490
        $cdir = $this->ReadCentralDir($zip,$zn);
445
508
        return null;
446
509
    }
447
510
 
 
511
    /**
 
512
     * @param array $header
 
513
     * @param resource $zip
 
514
     * @return null|string
 
515
     */
448
516
    function ExtractStrFile($header,$zip) {
449
 
        $hdr = $this->readfileheader($zip);
 
517
        $hdr = $this->readfileheader($zip, $header);
450
518
        $binary_data = '';
451
519
        if (!($header['external']==0x41FF0010) && !($header['external']==16)) {
452
520
            if ($header['compression']==0) {
484
552
        return null;
485
553
    }
486
554
 
 
555
    /**
 
556
     * @param string $val
 
557
     * @return int|string
 
558
     */
487
559
    function _ret_bytes($val) {
488
560
        $val = trim($val);
489
561
        $last = $val{strlen($val)-1};