Commit | Line | Data |
25f0751f |
1 | package IO::Compress::Zip ; |
2 | |
3 | use strict ; |
4 | use warnings; |
5 | use bytes; |
6 | |
dcfdccf9 |
7 | use IO::Compress::Base::Common 2.020 qw(:Status createSelfTiedObject); |
8 | use IO::Compress::RawDeflate 2.020 ; |
9 | use IO::Compress::Adapter::Deflate 2.020 ; |
10 | use IO::Compress::Adapter::Identity 2.020 ; |
11 | use IO::Compress::Zlib::Extra 2.020 ; |
12 | use IO::Compress::Zip::Constants 2.020 ; |
c70c1701 |
13 | |
25f0751f |
14 | |
dcfdccf9 |
15 | use Compress::Raw::Zlib 2.020 qw(crc32) ; |
2b4e0969 |
16 | BEGIN |
17 | { |
b0cda13f |
18 | eval { require IO::Compress::Adapter::Bzip2 ; |
dcfdccf9 |
19 | import IO::Compress::Adapter::Bzip2 2.020 ; |
b0cda13f |
20 | require IO::Compress::Bzip2 ; |
dcfdccf9 |
21 | import IO::Compress::Bzip2 2.020 ; |
2b4e0969 |
22 | } ; |
319fab50 |
23 | # eval { require IO::Compress::Adapter::Lzma ; |
ea6efd2c |
24 | # import IO::Compress::Adapter::Lzma 2.018 ; |
319fab50 |
25 | # require IO::Compress::Lzma ; |
ea6efd2c |
26 | # import IO::Compress::Lzma 2.018 ; |
319fab50 |
27 | # } ; |
2b4e0969 |
28 | } |
29 | |
30 | |
25f0751f |
31 | require Exporter ; |
32 | |
33 | our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $ZipError); |
34 | |
dcfdccf9 |
35 | $VERSION = '2.020'; |
25f0751f |
36 | $ZipError = ''; |
37 | |
38 | @ISA = qw(Exporter IO::Compress::RawDeflate); |
39 | @EXPORT_OK = qw( $ZipError zip ) ; |
40 | %EXPORT_TAGS = %IO::Compress::RawDeflate::DEFLATE_CONSTANTS ; |
41 | push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ; |
2b4e0969 |
42 | |
43 | $EXPORT_TAGS{zip_method} = [qw( ZIP_CM_STORE ZIP_CM_DEFLATE ZIP_CM_BZIP2 )]; |
44 | push @{ $EXPORT_TAGS{all} }, @{ $EXPORT_TAGS{zip_method} }; |
45 | |
25f0751f |
46 | Exporter::export_ok_tags('all'); |
47 | |
25f0751f |
48 | sub new |
49 | { |
50 | my $class = shift ; |
51 | |
52 | my $obj = createSelfTiedObject($class, \$ZipError); |
53 | $obj->_create(undef, @_); |
54 | } |
55 | |
56 | sub zip |
57 | { |
58 | my $obj = createSelfTiedObject(undef, \$ZipError); |
59 | return $obj->_def(@_); |
60 | } |
61 | |
62 | sub mkComp |
63 | { |
64 | my $self = shift ; |
25f0751f |
65 | my $got = shift ; |
66 | |
67 | my ($obj, $errstr, $errno) ; |
68 | |
2b4e0969 |
69 | if (*$self->{ZipData}{Method} == ZIP_CM_STORE) { |
25f0751f |
70 | ($obj, $errstr, $errno) = IO::Compress::Adapter::Identity::mkCompObject( |
25f0751f |
71 | $got->value('Level'), |
72 | $got->value('Strategy') |
73 | ); |
ea6efd2c |
74 | *$self->{ZipData}{CRC32} = crc32(undef); |
25f0751f |
75 | } |
2b4e0969 |
76 | elsif (*$self->{ZipData}{Method} == ZIP_CM_DEFLATE) { |
25f0751f |
77 | ($obj, $errstr, $errno) = IO::Compress::Adapter::Deflate::mkCompObject( |
78 | $got->value('CRC32'), |
79 | $got->value('Adler32'), |
80 | $got->value('Level'), |
81 | $got->value('Strategy') |
82 | ); |
83 | } |
2b4e0969 |
84 | elsif (*$self->{ZipData}{Method} == ZIP_CM_BZIP2) { |
85 | ($obj, $errstr, $errno) = IO::Compress::Adapter::Bzip2::mkCompObject( |
86 | $got->value('BlockSize100K'), |
87 | $got->value('WorkFactor'), |
88 | $got->value('Verbosity') |
89 | ); |
90 | *$self->{ZipData}{CRC32} = crc32(undef); |
91 | } |
319fab50 |
92 | # elsif (*$self->{ZipData}{Method} == ZIP_CM_LZMA) { |
93 | # ($obj, $errstr, $errno) = IO::Compress::Adapter::Lzma::mkCompObject( |
94 | # ); |
95 | # *$self->{ZipData}{CRC32} = crc32(undef); |
96 | # } |
25f0751f |
97 | |
98 | return $self->saveErrorString(undef, $errstr, $errno) |
99 | if ! defined $obj; |
100 | |
101 | if (! defined *$self->{ZipData}{StartOffset}) { |
e7d45986 |
102 | *$self->{ZipData}{StartOffset} = 0; |
103 | *$self->{ZipData}{Offset} = new U64 ; |
25f0751f |
104 | } |
105 | |
106 | return $obj; |
107 | } |
108 | |
2b4e0969 |
109 | sub reset |
110 | { |
111 | my $self = shift ; |
112 | |
113 | *$self->{Compress}->reset(); |
114 | *$self->{ZipData}{CRC32} = Compress::Raw::Zlib::crc32(''); |
115 | |
116 | return STATUS_OK; |
117 | } |
118 | |
119 | sub filterUncompressed |
120 | { |
121 | my $self = shift ; |
25f0751f |
122 | |
2b4e0969 |
123 | if (*$self->{ZipData}{Method} == ZIP_CM_DEFLATE) { |
124 | *$self->{ZipData}{CRC32} = *$self->{Compress}->crc32(); |
125 | } |
126 | else { |
127 | *$self->{ZipData}{CRC32} = crc32(${$_[0]}, *$self->{ZipData}{CRC32}); |
128 | |
129 | } |
130 | } |
25f0751f |
131 | |
132 | sub mkHeader |
133 | { |
134 | my $self = shift; |
135 | my $param = shift ; |
136 | |
e7d45986 |
137 | *$self->{ZipData}{StartOffset} = *$self->{ZipData}{Offset}->get32bit() ; |
c70c1701 |
138 | |
25f0751f |
139 | my $filename = ''; |
140 | $filename = $param->value('Name') || ''; |
141 | |
142 | my $comment = ''; |
143 | $comment = $param->value('Comment') || ''; |
144 | |
25f0751f |
145 | my $hdr = ''; |
146 | |
147 | my $time = _unixToDosTime($param->value('Time')); |
25f0751f |
148 | |
c70c1701 |
149 | my $extra = ''; |
150 | my $ctlExtra = ''; |
e7d45986 |
151 | my $empty = 0; |
4e7676c7 |
152 | my $osCode = $param->value('OS_Code') ; |
153 | my $extFileAttr = 0 ; |
d54256af |
154 | |
155 | # This code assumes Unix. |
156 | $extFileAttr = 0666 << 16 |
157 | if $osCode == ZIP_OS_CODE_UNIX ; |
e7d45986 |
158 | |
159 | if (*$self->{ZipData}{Zip64}) { |
319fab50 |
160 | $empty = 0xFFFFFFFF; |
e7d45986 |
161 | |
162 | my $x = ''; |
163 | $x .= pack "V V", 0, 0 ; # uncompressedLength |
164 | $x .= pack "V V", 0, 0 ; # compressedLength |
165 | $x .= *$self->{ZipData}{Offset}->getPacked_V64() ; # offset to local hdr |
319fab50 |
166 | $x .= pack "V", 0 ; # disk no |
e7d45986 |
167 | |
168 | $x = IO::Compress::Zlib::Extra::mkSubField(ZIP_EXTRA_ID_ZIP64, $x); |
169 | $extra .= $x; |
170 | $ctlExtra .= $x; |
171 | } |
c70c1701 |
172 | |
173 | if (! $param->value('Minimal')) { |
174 | if (defined $param->value('exTime')) |
175 | { |
176 | $extra .= mkExtendedTime($param->value('MTime'), |
177 | $param->value('ATime'), |
178 | $param->value('CTime')); |
179 | |
180 | $ctlExtra .= mkExtendedTime($param->value('MTime')); |
181 | } |
182 | |
4e7676c7 |
183 | if ( $param->value('UID') && $osCode == ZIP_OS_CODE_UNIX) |
184 | { |
185 | $extra .= mkUnix2Extra( $param->value('UID'), $param->value('GID')); |
186 | $ctlExtra .= mkUnix2Extra(); |
187 | } |
188 | |
4e7676c7 |
189 | $extFileAttr = $param->value('ExtAttr') |
190 | if defined $param->value('ExtAttr') ; |
c70c1701 |
191 | |
192 | $extra .= $param->value('ExtraFieldLocal') |
193 | if defined $param->value('ExtraFieldLocal'); |
194 | |
195 | $ctlExtra .= $param->value('ExtraFieldCentral') |
196 | if defined $param->value('ExtraFieldCentral'); |
197 | } |
198 | |
c70c1701 |
199 | my $gpFlag = 0 ; |
200 | $gpFlag |= ZIP_GP_FLAG_STREAMING_MASK |
201 | if *$self->{ZipData}{Stream} ; |
202 | |
2b4e0969 |
203 | my $method = *$self->{ZipData}{Method} ; |
204 | |
e7d45986 |
205 | my $version = $ZIP_CM_MIN_VERSIONS{$method}; |
206 | $version = ZIP64_MIN_VERSION |
207 | if ZIP64_MIN_VERSION > $version && *$self->{ZipData}{Zip64}; |
208 | my $madeBy = ($param->value('OS_Code') << 8) + $version; |
209 | my $extract = $version; |
210 | |
211 | *$self->{ZipData}{Version} = $version; |
212 | *$self->{ZipData}{MadeBy} = $madeBy; |
c70c1701 |
213 | |
214 | my $ifa = 0; |
215 | $ifa |= ZIP_IFA_TEXT_MASK |
216 | if $param->value('TextFlag'); |
25f0751f |
217 | |
2b4e0969 |
218 | $hdr .= pack "V", ZIP_LOCAL_HDR_SIG ; # signature |
25f0751f |
219 | $hdr .= pack 'v', $extract ; # extract Version & OS |
c70c1701 |
220 | $hdr .= pack 'v', $gpFlag ; # general purpose flag (set streaming mode) |
25f0751f |
221 | $hdr .= pack 'v', $method ; # compression method (deflate) |
222 | $hdr .= pack 'V', $time ; # last mod date/time |
223 | $hdr .= pack 'V', 0 ; # crc32 - 0 when streaming |
e7d45986 |
224 | $hdr .= pack 'V', $empty ; # compressed length - 0 when streaming |
225 | $hdr .= pack 'V', $empty ; # uncompressed length - 0 when streaming |
25f0751f |
226 | $hdr .= pack 'v', length $filename ; # filename length |
c70c1701 |
227 | $hdr .= pack 'v', length $extra ; # extra length |
25f0751f |
228 | |
229 | $hdr .= $filename ; |
c70c1701 |
230 | $hdr .= $extra ; |
25f0751f |
231 | |
232 | |
233 | my $ctl = ''; |
234 | |
2b4e0969 |
235 | $ctl .= pack "V", ZIP_CENTRAL_HDR_SIG ; # signature |
c70c1701 |
236 | $ctl .= pack 'v', $madeBy ; # version made by |
25f0751f |
237 | $ctl .= pack 'v', $extract ; # extract Version |
c70c1701 |
238 | $ctl .= pack 'v', $gpFlag ; # general purpose flag (streaming mode) |
25f0751f |
239 | $ctl .= pack 'v', $method ; # compression method (deflate) |
240 | $ctl .= pack 'V', $time ; # last mod date/time |
241 | $ctl .= pack 'V', 0 ; # crc32 |
e7d45986 |
242 | $ctl .= pack 'V', $empty ; # compressed length |
243 | $ctl .= pack 'V', $empty ; # uncompressed length |
25f0751f |
244 | $ctl .= pack 'v', length $filename ; # filename length |
c70c1701 |
245 | $ctl .= pack 'v', length $ctlExtra ; # extra length |
25f0751f |
246 | $ctl .= pack 'v', length $comment ; # file comment length |
247 | $ctl .= pack 'v', 0 ; # disk number start |
c70c1701 |
248 | $ctl .= pack 'v', $ifa ; # internal file attributes |
4e7676c7 |
249 | $ctl .= pack 'V', $extFileAttr ; # external file attributes |
e7d45986 |
250 | if (! *$self->{ZipData}{Zip64}) { |
251 | $ctl .= pack 'V', *$self->{ZipData}{Offset}->get32bit() ; # offset to local header |
252 | } |
253 | else { |
254 | $ctl .= pack 'V', $empty ; # offset to local header |
255 | } |
25f0751f |
256 | |
257 | $ctl .= $filename ; |
e7d45986 |
258 | *$self->{ZipData}{StartOffset64} = 4 + length $ctl; |
c70c1701 |
259 | $ctl .= $ctlExtra ; |
25f0751f |
260 | $ctl .= $comment ; |
261 | |
e7d45986 |
262 | *$self->{ZipData}{Offset}->add(length $hdr) ; |
25f0751f |
263 | |
264 | *$self->{ZipData}{CentralHeader} = $ctl; |
265 | |
266 | return $hdr; |
267 | } |
268 | |
269 | sub mkTrailer |
270 | { |
271 | my $self = shift ; |
272 | |
2b4e0969 |
273 | my $crc32 ; |
274 | if (*$self->{ZipData}{Method} == ZIP_CM_DEFLATE) { |
e7d45986 |
275 | $crc32 = pack "V", *$self->{Compress}->crc32(); |
2b4e0969 |
276 | } |
277 | else { |
e7d45986 |
278 | $crc32 = pack "V", *$self->{ZipData}{CRC32}; |
2b4e0969 |
279 | } |
280 | |
e7d45986 |
281 | my $ctl = *$self->{ZipData}{CentralHeader} ; |
282 | |
283 | my $sizes ; |
284 | if (! *$self->{ZipData}{Zip64}) { |
285 | $sizes .= *$self->{CompSize}->getPacked_V32() ; # Compressed size |
286 | $sizes .= *$self->{UnCompSize}->getPacked_V32() ; # Uncompressed size |
287 | } |
288 | else { |
289 | $sizes .= *$self->{CompSize}->getPacked_V64() ; # Compressed size |
290 | $sizes .= *$self->{UnCompSize}->getPacked_V64() ; # Uncompressed size |
291 | } |
292 | |
293 | my $data = $crc32 . $sizes ; |
25f0751f |
294 | |
25f0751f |
295 | |
296 | my $hdr = ''; |
297 | |
298 | if (*$self->{ZipData}{Stream}) { |
2b4e0969 |
299 | $hdr = pack "V", ZIP_DATA_HDR_SIG ; # signature |
25f0751f |
300 | $hdr .= $data ; |
301 | } |
302 | else { |
303 | $self->writeAt(*$self->{ZipData}{StartOffset} + 14, $data) |
304 | or return undef; |
305 | } |
306 | |
e7d45986 |
307 | if (! *$self->{ZipData}{Zip64}) |
308 | { substr($ctl, 16, length $data) = $data } |
309 | else { |
310 | substr($ctl, 16, length $crc32) = $crc32 ; |
311 | my $s = *$self->{UnCompSize}->getPacked_V64() ; # Uncompressed size |
312 | $s .= *$self->{CompSize}->getPacked_V64() ; # Compressed size |
313 | substr($ctl, *$self->{ZipData}{StartOffset64}, length $s) = $s ; |
314 | } |
25f0751f |
315 | |
e7d45986 |
316 | *$self->{ZipData}{Offset}->add(length($hdr)); |
317 | *$self->{ZipData}{Offset}->add( *$self->{CompSize} ); |
25f0751f |
318 | push @{ *$self->{ZipData}{CentralDir} }, $ctl ; |
319 | |
320 | return $hdr; |
321 | } |
322 | |
323 | sub mkFinalTrailer |
324 | { |
325 | my $self = shift ; |
326 | |
327 | my $comment = ''; |
328 | $comment = *$self->{ZipData}{ZipComment} ; |
329 | |
e7d45986 |
330 | my $cd_offset = *$self->{ZipData}{Offset}->get32bit() ; # offset to start central dir |
331 | |
25f0751f |
332 | my $entries = @{ *$self->{ZipData}{CentralDir} }; |
333 | my $cd = join '', @{ *$self->{ZipData}{CentralDir} }; |
e7d45986 |
334 | my $cd_len = length $cd ; |
335 | |
336 | my $z64e = ''; |
337 | |
338 | if ( *$self->{ZipData}{Zip64} ) { |
339 | |
340 | my $v = *$self->{ZipData}{Version} ; |
341 | my $mb = *$self->{ZipData}{MadeBy} ; |
342 | $z64e .= pack 'v', $v ; # Version made by |
343 | $z64e .= pack 'v', $mb ; # Version to extract |
344 | $z64e .= pack 'V', 0 ; # number of disk |
345 | $z64e .= pack 'V', 0 ; # number of disk with central dir |
346 | $z64e .= U64::pack_V64 $entries ; # entries in central dir on this disk |
347 | $z64e .= U64::pack_V64 $entries ; # entries in central dir |
348 | $z64e .= U64::pack_V64 $cd_len ; # size of central dir |
349 | $z64e .= *$self->{ZipData}{Offset}->getPacked_V64() ; # offset to start central dir |
350 | |
351 | $z64e = pack("V", ZIP64_END_CENTRAL_REC_HDR_SIG) # signature |
352 | . U64::pack_V64(length $z64e) |
353 | . $z64e ; |
354 | |
355 | *$self->{ZipData}{Offset}->add(length $cd) ; |
356 | |
357 | $z64e .= pack "V", ZIP64_END_CENTRAL_LOC_HDR_SIG; # signature |
358 | $z64e .= pack 'V', 0 ; # number of disk with central dir |
359 | $z64e .= *$self->{ZipData}{Offset}->getPacked_V64() ; # offset to end zip64 central dir |
360 | $z64e .= pack 'V', 1 ; # Total number of disks |
361 | |
362 | # TODO - fix these when info-zip 3 is fixed. |
363 | #$cd_len = |
364 | #$cd_offset = |
365 | $entries = 0xFFFF ; |
366 | } |
25f0751f |
367 | |
368 | my $ecd = ''; |
2b4e0969 |
369 | $ecd .= pack "V", ZIP_END_CENTRAL_HDR_SIG ; # signature |
25f0751f |
370 | $ecd .= pack 'v', 0 ; # number of disk |
e7d45986 |
371 | $ecd .= pack 'v', 0 ; # number of disk with central dir |
25f0751f |
372 | $ecd .= pack 'v', $entries ; # entries in central dir on this disk |
373 | $ecd .= pack 'v', $entries ; # entries in central dir |
e7d45986 |
374 | $ecd .= pack 'V', $cd_len ; # size of central dir |
375 | $ecd .= pack 'V', $cd_offset ; # offset to start central dir |
25f0751f |
376 | $ecd .= pack 'v', length $comment ; # zipfile comment length |
377 | $ecd .= $comment; |
378 | |
e7d45986 |
379 | return $cd . $z64e . $ecd ; |
25f0751f |
380 | } |
381 | |
382 | sub ckParams |
383 | { |
384 | my $self = shift ; |
385 | my $got = shift; |
386 | |
387 | $got->value('CRC32' => 1); |
388 | |
389 | if (! $got->parsed('Time') ) { |
390 | # Modification time defaults to now. |
391 | $got->value('Time' => time) ; |
392 | } |
393 | |
ea6efd2c |
394 | if ($got->parsed('exTime') ) { |
c70c1701 |
395 | my $timeRef = $got->value('exTime'); |
396 | if ( defined $timeRef) { |
397 | return $self->saveErrorString(undef, "exTime not a 3-element array ref") |
398 | if ref $timeRef ne 'ARRAY' || @$timeRef != 3; |
399 | } |
400 | |
401 | $got->value("MTime", $timeRef->[1]); |
402 | $got->value("ATime", $timeRef->[0]); |
403 | $got->value("CTime", $timeRef->[2]); |
404 | } |
d54256af |
405 | |
406 | # Unix2 Extended Attribute |
ea6efd2c |
407 | if ($got->parsed('exUnix2') ) { |
d54256af |
408 | my $timeRef = $got->value('exUnix2'); |
409 | if ( defined $timeRef) { |
410 | return $self->saveErrorString(undef, "exUnix2 not a 2-element array ref") |
411 | if ref $timeRef ne 'ARRAY' || @$timeRef != 2; |
412 | } |
413 | |
414 | $got->value("UID", $timeRef->[0]); |
415 | $got->value("GID", $timeRef->[1]); |
416 | } |
c70c1701 |
417 | |
e7d45986 |
418 | *$self->{ZipData}{Zip64} = $got->value('Zip64'); |
25f0751f |
419 | *$self->{ZipData}{Stream} = $got->value('Stream'); |
2b4e0969 |
420 | |
e7d45986 |
421 | return $self->saveErrorString(undef, "Zip64 only supported if Stream enabled") |
422 | if *$self->{ZipData}{Zip64} && ! *$self->{ZipData}{Stream} ; |
423 | |
2b4e0969 |
424 | my $method = $got->value('Method'); |
2b4e0969 |
425 | return $self->saveErrorString(undef, "Unknown Method '$method'") |
426 | if ! defined $ZIP_CM_MIN_VERSIONS{$method}; |
427 | |
428 | return $self->saveErrorString(undef, "Bzip2 not available") |
429 | if $method == ZIP_CM_BZIP2 and |
430 | ! defined $IO::Compress::Adapter::Bzip2::VERSION; |
431 | |
319fab50 |
432 | # return $self->saveErrorString(undef, "Lzma not available") |
433 | # if $method == ZIP_CM_LZMA and |
434 | # ! defined $IO::Compress::Adapter::Lzma::VERSION; |
435 | |
2b4e0969 |
436 | *$self->{ZipData}{Method} = $method; |
437 | |
25f0751f |
438 | *$self->{ZipData}{ZipComment} = $got->value('ZipComment') ; |
439 | |
c70c1701 |
440 | for my $name (qw( ExtraFieldLocal ExtraFieldCentral )) |
441 | { |
442 | my $data = $got->value($name) ; |
443 | if (defined $data) { |
444 | my $bad = IO::Compress::Zlib::Extra::parseExtraField($data, 1, 0) ; |
445 | return $self->saveErrorString(undef, "Error with $name Parameter: $bad") |
446 | if $bad ; |
447 | |
448 | $got->value($name, $data) ; |
449 | } |
450 | } |
451 | |
2b4e0969 |
452 | return undef |
453 | if defined $IO::Compress::Bzip2::VERSION |
454 | and ! IO::Compress::Bzip2::ckParams($self, $got); |
25f0751f |
455 | |
456 | return 1 ; |
457 | } |
458 | |
459 | #sub newHeader |
460 | #{ |
461 | # my $self = shift ; |
462 | # |
463 | # return $self->mkHeader(*$self->{Got}); |
464 | #} |
465 | |
466 | sub getExtraParams |
467 | { |
468 | my $self = shift ; |
469 | |
dcfdccf9 |
470 | use IO::Compress::Base::Common 2.020 qw(:Parse); |
471 | use Compress::Raw::Zlib 2.020 qw(Z_DEFLATED Z_DEFAULT_COMPRESSION Z_DEFAULT_STRATEGY); |
25f0751f |
472 | |
2b4e0969 |
473 | my @Bzip2 = (); |
474 | |
475 | @Bzip2 = IO::Compress::Bzip2::getExtraParams($self) |
476 | if defined $IO::Compress::Bzip2::VERSION; |
25f0751f |
477 | |
478 | return ( |
479 | # zlib behaviour |
480 | $self->getZlibParams(), |
481 | |
482 | 'Stream' => [1, 1, Parse_boolean, 1], |
2b4e0969 |
483 | #'Store' => [0, 1, Parse_boolean, 0], |
484 | 'Method' => [0, 1, Parse_unsigned, ZIP_CM_DEFLATE], |
25f0751f |
485 | |
486 | # # Zip header fields |
c70c1701 |
487 | 'Minimal' => [0, 1, Parse_boolean, 0], |
e7d45986 |
488 | 'Zip64' => [0, 1, Parse_boolean, 0], |
25f0751f |
489 | 'Comment' => [0, 1, Parse_any, ''], |
490 | 'ZipComment'=> [0, 1, Parse_any, ''], |
491 | 'Name' => [0, 1, Parse_any, ''], |
492 | 'Time' => [0, 1, Parse_any, undef], |
c70c1701 |
493 | 'exTime' => [0, 1, Parse_any, undef], |
d54256af |
494 | 'exUnix2' => [0, 1, Parse_any, undef], |
4e7676c7 |
495 | 'ExtAttr' => [0, 1, Parse_any, 0], |
25f0751f |
496 | 'OS_Code' => [0, 1, Parse_unsigned, $Compress::Raw::Zlib::gzip_os_code], |
497 | |
c70c1701 |
498 | 'TextFlag' => [0, 1, Parse_boolean, 0], |
499 | 'ExtraFieldLocal' => [0, 1, Parse_any, undef], |
500 | 'ExtraFieldCentral'=> [0, 1, Parse_any, undef], |
2b4e0969 |
501 | |
502 | @Bzip2, |
25f0751f |
503 | ); |
504 | } |
505 | |
506 | sub getInverseClass |
507 | { |
508 | return ('IO::Uncompress::Unzip', |
509 | \$IO::Uncompress::Unzip::UnzipError); |
510 | } |
511 | |
512 | sub getFileInfo |
513 | { |
514 | my $self = shift ; |
515 | my $params = shift; |
516 | my $filename = shift ; |
517 | |
c70c1701 |
518 | my ($mode, $uid, $gid, $atime, $mtime, $ctime) |
519 | = (stat($filename))[2, 4,5, 8,9,10] ; |
25f0751f |
520 | |
521 | $params->value('Name' => $filename) |
522 | if ! $params->parsed('Name') ; |
523 | |
c70c1701 |
524 | $params->value('Time' => $mtime) |
25f0751f |
525 | if ! $params->parsed('Time') ; |
526 | |
c70c1701 |
527 | if ( ! $params->parsed('exTime')) |
528 | { |
529 | $params->value('MTime' => $mtime) ; |
530 | $params->value('ATime' => $atime) ; |
4e7676c7 |
531 | $params->value('CTime' => undef) ; # No Creation time |
ea6efd2c |
532 | $params->value("exTime", [$mtime, $atime, undef]); |
c70c1701 |
533 | } |
534 | |
d54256af |
535 | # NOTE - Unix specific code alert |
4e7676c7 |
536 | $params->value('ExtAttr' => $mode << 16) |
537 | if ! $params->parsed('ExtAttr'); |
c70c1701 |
538 | |
539 | $params->value('UID' => $uid) ; |
540 | $params->value('GID' => $gid) ; |
25f0751f |
541 | |
542 | } |
543 | |
c70c1701 |
544 | sub mkExtendedTime |
545 | { |
546 | # order expected is m, a, c |
547 | |
548 | my $times = ''; |
549 | my $bit = 1 ; |
550 | my $flags = 0; |
551 | |
552 | for my $time (@_) |
553 | { |
554 | if (defined $time) |
555 | { |
556 | $flags |= $bit; |
557 | $times .= pack("V", $time); |
558 | } |
559 | |
560 | $bit <<= 1 ; |
561 | } |
562 | |
c70c1701 |
563 | return IO::Compress::Zlib::Extra::mkSubField(ZIP_EXTRA_ID_EXT_TIMESTAMP, |
564 | pack("C", $flags) . $times); |
565 | } |
566 | |
4e7676c7 |
567 | sub mkUnix2Extra |
c70c1701 |
568 | { |
569 | my $ids = ''; |
570 | for my $id (@_) |
571 | { |
572 | $ids .= pack("v", $id); |
573 | } |
574 | |
4e7676c7 |
575 | return IO::Compress::Zlib::Extra::mkSubField(ZIP_EXTRA_ID_INFO_ZIP_UNIX2, |
576 | $ids); |
c70c1701 |
577 | } |
578 | |
579 | |
25f0751f |
580 | # from Archive::Zip |
581 | sub _unixToDosTime # Archive::Zip::Member |
582 | { |
583 | my $time_t = shift; |
584 | # TODO - add something to cope with unix time < 1980 |
585 | my ( $sec, $min, $hour, $mday, $mon, $year ) = localtime($time_t); |
586 | my $dt = 0; |
587 | $dt += ( $sec >> 1 ); |
588 | $dt += ( $min << 5 ); |
589 | $dt += ( $hour << 11 ); |
590 | $dt += ( $mday << 16 ); |
591 | $dt += ( ( $mon + 1 ) << 21 ); |
592 | $dt += ( ( $year - 80 ) << 25 ); |
593 | return $dt; |
594 | } |
595 | |
596 | 1; |
597 | |
598 | __END__ |
599 | |
600 | =head1 NAME |
601 | |
cb7abd7f |
602 | IO::Compress::Zip - Write zip files/buffers |
603 | |
25f0751f |
604 | |
605 | |
606 | =head1 SYNOPSIS |
607 | |
608 | use IO::Compress::Zip qw(zip $ZipError) ; |
609 | |
25f0751f |
610 | my $status = zip $input => $output [,OPTS] |
611 | or die "zip failed: $ZipError\n"; |
612 | |
613 | my $z = new IO::Compress::Zip $output [,OPTS] |
614 | or die "zip failed: $ZipError\n"; |
615 | |
616 | $z->print($string); |
617 | $z->printf($format, $string); |
618 | $z->write($string); |
619 | $z->syswrite($string [, $length, $offset]); |
620 | $z->flush(); |
621 | $z->tell(); |
622 | $z->eof(); |
623 | $z->seek($position, $whence); |
624 | $z->binmode(); |
625 | $z->fileno(); |
626 | $z->opened(); |
627 | $z->autoflush(); |
628 | $z->input_line_number(); |
629 | $z->newStream( [OPTS] ); |
630 | |
631 | $z->deflateParams(); |
632 | |
633 | $z->close() ; |
634 | |
635 | $ZipError ; |
636 | |
637 | # IO::File mode |
638 | |
639 | print $z $string; |
640 | printf $z $format, $string; |
641 | tell $z |
642 | eof $z |
643 | seek $z, $position, $whence |
644 | binmode $z |
645 | fileno $z |
646 | close $z ; |
647 | |
648 | |
649 | =head1 DESCRIPTION |
650 | |
25f0751f |
651 | This module provides a Perl interface that allows writing zip |
652 | compressed data to files or buffer. |
653 | |
258133d1 |
654 | The primary purpose of this module is to provide streaming write access to |
655 | zip files and buffers. It is not a general-purpose file archiver. If that |
656 | is what you want, check out C<Archive::Zip>. |
657 | |
4e7676c7 |
658 | At present three compression methods are supported by IO::Compress::Zip, |
659 | namely Store (no compression at all), Deflate and Bzip2. |
660 | |
661 | Note that to create Bzip2 content, the module C<IO::Compress::Bzip2> must |
662 | be installed. |
663 | |
25f0751f |
664 | For reading zip files/buffers, see the companion module |
665 | L<IO::Uncompress::Unzip|IO::Uncompress::Unzip>. |
666 | |
25f0751f |
667 | =head1 Functional Interface |
668 | |
669 | A top-level function, C<zip>, is provided to carry out |
670 | "one-shot" compression between buffers and/or files. For finer |
671 | control over the compression process, see the L</"OO Interface"> |
672 | section. |
673 | |
674 | use IO::Compress::Zip qw(zip $ZipError) ; |
675 | |
676 | zip $input => $output [,OPTS] |
677 | or die "zip failed: $ZipError\n"; |
678 | |
25f0751f |
679 | The functional interface needs Perl5.005 or better. |
680 | |
25f0751f |
681 | =head2 zip $input => $output [, OPTS] |
682 | |
25f0751f |
683 | C<zip> expects at least two parameters, C<$input> and C<$output>. |
684 | |
685 | =head3 The C<$input> parameter |
686 | |
687 | The parameter, C<$input>, is used to define the source of |
688 | the uncompressed data. |
689 | |
690 | It can take one of the following forms: |
691 | |
692 | =over 5 |
693 | |
694 | =item A filename |
695 | |
696 | If the C<$input> parameter is a simple scalar, it is assumed to be a |
697 | filename. This file will be opened for reading and the input data |
698 | will be read from it. |
699 | |
700 | =item A filehandle |
701 | |
702 | If the C<$input> parameter is a filehandle, the input data will be |
703 | read from it. |
704 | The string '-' can be used as an alias for standard input. |
705 | |
706 | =item A scalar reference |
707 | |
708 | If C<$input> is a scalar reference, the input data will be read |
709 | from C<$$input>. |
710 | |
711 | =item An array reference |
712 | |
713 | If C<$input> is an array reference, each element in the array must be a |
714 | filename. |
715 | |
716 | The input data will be read from each file in turn. |
717 | |
718 | The complete array will be walked to ensure that it only |
719 | contains valid filenames before any data is compressed. |
720 | |
25f0751f |
721 | =item An Input FileGlob string |
722 | |
723 | If C<$input> is a string that is delimited by the characters "<" and ">" |
724 | C<zip> will assume that it is an I<input fileglob string>. The |
725 | input is the list of files that match the fileglob. |
726 | |
727 | If the fileglob does not match any files ... |
728 | |
729 | See L<File::GlobMapper|File::GlobMapper> for more details. |
730 | |
25f0751f |
731 | =back |
732 | |
733 | If the C<$input> parameter is any other type, C<undef> will be returned. |
734 | |
25f0751f |
735 | In addition, if C<$input> is a simple filename, the default values for |
4e7676c7 |
736 | the C<Name>, C<Time>, C<ExtAttr> and C<exTime> options will be sourced from that file. |
25f0751f |
737 | |
738 | If you do not want to use these defaults they can be overridden by |
4e7676c7 |
739 | explicitly setting the C<Name>, C<Time>, C<ExtAttr> and C<exTime> options or by setting the |
25f0751f |
740 | C<Minimal> parameter. |
741 | |
25f0751f |
742 | =head3 The C<$output> parameter |
743 | |
744 | The parameter C<$output> is used to control the destination of the |
745 | compressed data. This parameter can take one of these forms. |
746 | |
747 | =over 5 |
748 | |
749 | =item A filename |
750 | |
751 | If the C<$output> parameter is a simple scalar, it is assumed to be a |
752 | filename. This file will be opened for writing and the compressed |
753 | data will be written to it. |
754 | |
755 | =item A filehandle |
756 | |
757 | If the C<$output> parameter is a filehandle, the compressed data |
758 | will be written to it. |
759 | The string '-' can be used as an alias for standard output. |
760 | |
25f0751f |
761 | =item A scalar reference |
762 | |
763 | If C<$output> is a scalar reference, the compressed data will be |
764 | stored in C<$$output>. |
765 | |
25f0751f |
766 | =item An Array Reference |
767 | |
768 | If C<$output> is an array reference, the compressed data will be |
769 | pushed onto the array. |
770 | |
771 | =item An Output FileGlob |
772 | |
773 | If C<$output> is a string that is delimited by the characters "<" and ">" |
774 | C<zip> will assume that it is an I<output fileglob string>. The |
775 | output is the list of files that match the fileglob. |
776 | |
777 | When C<$output> is an fileglob string, C<$input> must also be a fileglob |
778 | string. Anything else is an error. |
779 | |
780 | =back |
781 | |
782 | If the C<$output> parameter is any other type, C<undef> will be returned. |
783 | |
25f0751f |
784 | =head2 Notes |
785 | |
786 | When C<$input> maps to multiple files/buffers and C<$output> is a single |
c70c1701 |
787 | file/buffer the input files/buffers will each be stored |
788 | in C<$output> as a distinct entry. |
789 | |
25f0751f |
790 | =head2 Optional Parameters |
791 | |
792 | Unless specified below, the optional parameters for C<zip>, |
793 | C<OPTS>, are the same as those used with the OO interface defined in the |
794 | L</"Constructor Options"> section below. |
795 | |
796 | =over 5 |
797 | |
e7d45986 |
798 | =item C<< AutoClose => 0|1 >> |
25f0751f |
799 | |
800 | This option applies to any input or output data streams to |
801 | C<zip> that are filehandles. |
802 | |
803 | If C<AutoClose> is specified, and the value is true, it will result in all |
804 | input and/or output filehandles being closed once C<zip> has |
805 | completed. |
806 | |
807 | This parameter defaults to 0. |
808 | |
e7d45986 |
809 | =item C<< BinModeIn => 0|1 >> |
25f0751f |
810 | |
811 | When reading from a file or filehandle, set C<binmode> before reading. |
812 | |
813 | Defaults to 0. |
814 | |
e7d45986 |
815 | =item C<< Append => 0|1 >> |
25f0751f |
816 | |
817 | TODO |
818 | |
25f0751f |
819 | =back |
820 | |
25f0751f |
821 | =head2 Examples |
822 | |
823 | To read the contents of the file C<file1.txt> and write the compressed |
824 | data to the file C<file1.txt.zip>. |
825 | |
826 | use strict ; |
827 | use warnings ; |
828 | use IO::Compress::Zip qw(zip $ZipError) ; |
829 | |
830 | my $input = "file1.txt"; |
831 | zip $input => "$input.zip" |
832 | or die "zip failed: $ZipError\n"; |
833 | |
25f0751f |
834 | To read from an existing Perl filehandle, C<$input>, and write the |
835 | compressed data to a buffer, C<$buffer>. |
836 | |
837 | use strict ; |
838 | use warnings ; |
839 | use IO::Compress::Zip qw(zip $ZipError) ; |
840 | use IO::File ; |
841 | |
842 | my $input = new IO::File "<file1.txt" |
843 | or die "Cannot open 'file1.txt': $!\n" ; |
844 | my $buffer ; |
845 | zip $input => \$buffer |
846 | or die "zip failed: $ZipError\n"; |
847 | |
848 | To compress all files in the directory "/my/home" that match "*.txt" |
849 | and store the compressed data in the same directory |
850 | |
851 | use strict ; |
852 | use warnings ; |
853 | use IO::Compress::Zip qw(zip $ZipError) ; |
854 | |
855 | zip '</my/home/*.txt>' => '<*.zip>' |
856 | or die "zip failed: $ZipError\n"; |
857 | |
858 | and if you want to compress each file one at a time, this will do the trick |
859 | |
860 | use strict ; |
861 | use warnings ; |
862 | use IO::Compress::Zip qw(zip $ZipError) ; |
863 | |
864 | for my $input ( glob "/my/home/*.txt" ) |
865 | { |
866 | my $output = "$input.zip" ; |
867 | zip $input => $output |
868 | or die "Error compressing '$input': $ZipError\n"; |
869 | } |
870 | |
25f0751f |
871 | =head1 OO Interface |
872 | |
873 | =head2 Constructor |
874 | |
875 | The format of the constructor for C<IO::Compress::Zip> is shown below |
876 | |
877 | my $z = new IO::Compress::Zip $output [,OPTS] |
878 | or die "IO::Compress::Zip failed: $ZipError\n"; |
879 | |
880 | It returns an C<IO::Compress::Zip> object on success and undef on failure. |
881 | The variable C<$ZipError> will contain an error message on failure. |
882 | |
883 | If you are running Perl 5.005 or better the object, C<$z>, returned from |
884 | IO::Compress::Zip can be used exactly like an L<IO::File|IO::File> filehandle. |
885 | This means that all normal output file operations can be carried out |
886 | with C<$z>. |
887 | For example, to write to a compressed file/buffer you can use either of |
888 | these forms |
889 | |
890 | $z->print("hello world\n"); |
891 | print $z "hello world\n"; |
892 | |
893 | The mandatory parameter C<$output> is used to control the destination |
894 | of the compressed data. This parameter can take one of these forms. |
895 | |
896 | =over 5 |
897 | |
898 | =item A filename |
899 | |
900 | If the C<$output> parameter is a simple scalar, it is assumed to be a |
901 | filename. This file will be opened for writing and the compressed data |
902 | will be written to it. |
903 | |
904 | =item A filehandle |
905 | |
906 | If the C<$output> parameter is a filehandle, the compressed data will be |
907 | written to it. |
908 | The string '-' can be used as an alias for standard output. |
909 | |
25f0751f |
910 | =item A scalar reference |
911 | |
912 | If C<$output> is a scalar reference, the compressed data will be stored |
913 | in C<$$output>. |
914 | |
915 | =back |
916 | |
917 | If the C<$output> parameter is any other type, C<IO::Compress::Zip>::new will |
918 | return undef. |
919 | |
920 | =head2 Constructor Options |
921 | |
922 | C<OPTS> is any combination of the following options: |
923 | |
924 | =over 5 |
925 | |
e7d45986 |
926 | =item C<< AutoClose => 0|1 >> |
25f0751f |
927 | |
928 | This option is only valid when the C<$output> parameter is a filehandle. If |
929 | specified, and the value is true, it will result in the C<$output> being |
930 | closed once either the C<close> method is called or the C<IO::Compress::Zip> |
931 | object is destroyed. |
932 | |
933 | This parameter defaults to 0. |
934 | |
e7d45986 |
935 | =item C<< Append => 0|1 >> |
25f0751f |
936 | |
937 | Opens C<$output> in append mode. |
938 | |
939 | The behaviour of this option is dependent on the type of C<$output>. |
940 | |
941 | =over 5 |
942 | |
943 | =item * A Buffer |
944 | |
945 | If C<$output> is a buffer and C<Append> is enabled, all compressed data |
946 | will be append to the end if C<$output>. Otherwise C<$output> will be |
947 | cleared before any data is written to it. |
948 | |
949 | =item * A Filename |
950 | |
951 | If C<$output> is a filename and C<Append> is enabled, the file will be |
952 | opened in append mode. Otherwise the contents of the file, if any, will be |
953 | truncated before any compressed data is written to it. |
954 | |
955 | =item * A Filehandle |
956 | |
957 | If C<$output> is a filehandle, the file pointer will be positioned to the |
958 | end of the file via a call to C<seek> before any compressed data is written |
959 | to it. Otherwise the file pointer will not be moved. |
960 | |
961 | =back |
962 | |
963 | This parameter defaults to 0. |
964 | |
e7d45986 |
965 | =item C<< Name => $string >> |
2b4e0969 |
966 | |
967 | Stores the contents of C<$string> in the zip filename header field. If |
968 | C<Name> is not specified, no zip filename field will be created. |
969 | |
e7d45986 |
970 | =item C<< Time => $number >> |
2b4e0969 |
971 | |
972 | Sets the last modified time field in the zip header to $number. |
973 | |
974 | This field defaults to the time the C<IO::Compress::Zip> object was created |
975 | if this option is not specified. |
976 | |
4e7676c7 |
977 | =item C<< ExtAttr => $attr >> |
978 | |
979 | This option controls the "external file attributes" field in the central |
980 | header of the zip file. This is a 4 byte field. |
981 | |
d54256af |
982 | If you are running a Unix derivative this value defaults to |
983 | |
984 | 0666 << 16 |
985 | |
986 | This should allow read/write access to any files that are extracted from |
987 | the zip file/buffer. |
988 | |
989 | For all other systems it defaults to 0. |
4e7676c7 |
990 | |
e7d45986 |
991 | =item C<< exTime => [$atime, $mtime, $ctime] >> |
c70c1701 |
992 | |
993 | This option expects an array reference with exactly three elements: |
994 | C<$atime>, C<mtime> and C<$ctime>. These correspond to the last access |
995 | time, last modification time and creation time respectively. |
996 | |
d54256af |
997 | It uses these values to set the extended timestamp field (ID is "UT") in |
998 | the local zip header using the three values, $atime, $mtime, $ctime. In |
999 | addition it sets the extended timestamp field in the central zip header |
1000 | using C<$mtime>. |
c70c1701 |
1001 | |
4e7676c7 |
1002 | If any of the three values is C<undef> that time value will not be used. |
1003 | So, for example, to set only the C<$mtime> you would use this |
1004 | |
1005 | exTime => [undef, $mtime, undef] |
1006 | |
c70c1701 |
1007 | If the C<Minimal> option is set to true, this option will be ignored. |
1008 | |
1009 | By default no extended time field is created. |
1010 | |
d54256af |
1011 | =item C<< exUnix2 => [$uid, $gid] >> |
1012 | |
1013 | This option expects an array reference with exactly two elements: C<$uid> |
1014 | and C<$gid>. These values correspond to the numeric user ID and group ID |
1015 | of the owner of the files respectively. |
1016 | |
1017 | When the C<exUnix2> option is present it will trigger the creation of a |
1018 | Unix2 extra field (ID is "Ux") in the local zip. This will be populated |
1019 | with C<$uid> and C<$gid>. In addition an empty Unix2 extra field will also |
1020 | be created in the central zip header |
1021 | |
1022 | If the C<Minimal> option is set to true, this option will be ignored. |
1023 | |
1024 | By default no Unix2 extra field is created. |
1025 | |
e7d45986 |
1026 | =item C<< Comment => $comment >> |
c70c1701 |
1027 | |
1028 | Stores the contents of C<$comment> in the Central File Header of |
1029 | the zip file. |
1030 | |
1031 | By default, no comment field is written to the zip file. |
1032 | |
e7d45986 |
1033 | =item C<< ZipComment => $comment >> |
c70c1701 |
1034 | |
1035 | Stores the contents of C<$comment> in the End of Central Directory record |
1036 | of the zip file. |
1037 | |
1038 | By default, no comment field is written to the zip file. |
1039 | |
e7d45986 |
1040 | =item C<< Method => $method >> |
2b4e0969 |
1041 | |
1042 | Controls which compression method is used. At present three compression |
1043 | methods are supported, namely Store (no compression at all), Deflate and |
1044 | Bzip2. |
1045 | |
1046 | The symbols, ZIP_CM_STORE, ZIP_CM_DEFLATE and ZIP_CM_BZIP2 are used to |
1047 | select the compression method. |
1048 | |
1049 | These constants are not imported by C<IO::Compress::Zip> by default. |
1050 | |
1051 | use IO::Compress::Zip qw(:zip_method); |
1052 | use IO::Compress::Zip qw(:constants); |
1053 | use IO::Compress::Zip qw(:all); |
1054 | |
1055 | Note that to create Bzip2 content, the module C<IO::Compress::Bzip2> must |
1056 | be installed. A fatal error will be thrown if you attempt to create Bzip2 |
1057 | content when C<IO::Compress::Bzip2> is not available. |
1058 | |
1059 | The default method is ZIP_CM_DEFLATE. |
1060 | |
e7d45986 |
1061 | =item C<< Stream => 0|1 >> |
2b4e0969 |
1062 | |
1063 | This option controls whether the zip file/buffer output is created in |
1064 | streaming mode. |
1065 | |
c70c1701 |
1066 | Note that when outputting to a file with streaming mode disabled (C<Stream> |
1067 | is 0), the output file must be seekable. |
1068 | |
2b4e0969 |
1069 | The default is 1. |
1070 | |
4e7676c7 |
1071 | =item C<< Zip64 => 0|1 >> |
258133d1 |
1072 | |
4e7676c7 |
1073 | Create a Zip64 zip file/buffer. This option should only be used if you want |
1074 | to store files larger than 4 Gig. |
1075 | |
1076 | If you intend to manipulate the Zip64 zip files created with this module |
1077 | using an external zip/unzip make sure that it supports streaming Zip64. |
1078 | |
1079 | In particular, if you are using Info-Zip you need to have zip version 3.x |
1080 | or better to update a Zip64 archive and unzip version 6.x to read a zip64 |
1081 | archive. At the time of writing both are beta status. |
1082 | |
1083 | When the C<Zip64> option is enabled, the C<Stream> option I<must> be |
1084 | enabled as well. |
1085 | |
1086 | The default is 0. |
258133d1 |
1087 | |
e7d45986 |
1088 | =item C<< TextFlag => 0|1 >> |
c70c1701 |
1089 | |
1090 | This parameter controls the setting of a bit in the zip central header. It |
1091 | is used to signal that the data stored in the zip file/buffer is probably |
1092 | text. |
1093 | |
1094 | The default is 0. |
1095 | |
e7d45986 |
1096 | =item C<< ExtraFieldLocal => $data >> |
1097 | =item C<< ExtraFieldCentral => $data >> |
c70c1701 |
1098 | |
d54256af |
1099 | The C<ExtraFieldLocal> option is used to store additional metadata in the |
1100 | local header for the zip file/buffer. The C<ExtraFieldCentral> does the |
1101 | same for the matching central header. |
c70c1701 |
1102 | |
1103 | An extra field consists of zero or more subfields. Each subfield consists |
1104 | of a two byte header followed by the subfield data. |
1105 | |
1106 | The list of subfields can be supplied in any of the following formats |
1107 | |
1108 | ExtraFieldLocal => [$id1, $data1, |
1109 | $id2, $data2, |
1110 | ... |
1111 | ] |
1112 | |
1113 | ExtraFieldLocal => [ [$id1 => $data1], |
1114 | [$id2 => $data2], |
1115 | ... |
1116 | ] |
1117 | |
1118 | ExtraFieldLocal => { $id1 => $data1, |
1119 | $id2 => $data2, |
1120 | ... |
1121 | } |
1122 | |
1123 | Where C<$id1>, C<$id2> are two byte subfield ID's. |
1124 | |
1125 | If you use the hash syntax, you have no control over the order in which |
1126 | the ExtraSubFields are stored, plus you cannot have SubFields with |
1127 | duplicate ID. |
1128 | |
1129 | Alternatively the list of subfields can by supplied as a scalar, thus |
1130 | |
1131 | ExtraField => $rawdata |
1132 | |
d54256af |
1133 | The Extended Time field (ID "UT"), set using the C<exTime> option, and the |
1134 | Unix2 extra field (ID "Ux), set using the C<exUnix2> option, are examples |
1135 | of extra fields. |
c70c1701 |
1136 | |
1137 | If the C<Minimal> option is set to true, this option will be ignored. |
1138 | |
1139 | The maximum size of an extra field 65535 bytes. |
1140 | |
e7d45986 |
1141 | =item C<< Minimal => 1|0 >> |
c70c1701 |
1142 | |
d54256af |
1143 | If specified, this option will disable the creation of all extra fields |
1144 | in the zip local and central headers. So the C<exTime>, C<exUnix2>, |
1145 | C<ExtraFieldLocal> and C<ExtraFieldCentral> options will be ignored. |
c70c1701 |
1146 | |
1147 | This parameter defaults to 0. |
1148 | |
e7d45986 |
1149 | =item C<< BlockSize100K => number >> |
2b4e0969 |
1150 | |
1151 | Specify the number of 100K blocks bzip2 uses during compression. |
1152 | |
1153 | Valid values are from 1 to 9, where 9 is best compression. |
1154 | |
1155 | This option is only valid if the C<Method> is ZIP_CM_BZIP2. It is ignored |
1156 | otherwise. |
1157 | |
1158 | The default is 1. |
1159 | |
e7d45986 |
1160 | =item C<< WorkFactor => number >> |
2b4e0969 |
1161 | |
1162 | Specifies how much effort bzip2 should take before resorting to a slower |
1163 | fallback compression algorithm. |
1164 | |
1165 | Valid values range from 0 to 250, where 0 means use the default value 30. |
1166 | |
1167 | This option is only valid if the C<Method> is ZIP_CM_BZIP2. It is ignored |
1168 | otherwise. |
1169 | |
1170 | The default is 0. |
1171 | |
25f0751f |
1172 | =item -Level |
1173 | |
1174 | Defines the compression level used by zlib. The value should either be |
1175 | a number between 0 and 9 (0 means no compression and 9 is maximum |
1176 | compression), or one of the symbolic constants defined below. |
1177 | |
1178 | Z_NO_COMPRESSION |
1179 | Z_BEST_SPEED |
1180 | Z_BEST_COMPRESSION |
1181 | Z_DEFAULT_COMPRESSION |
1182 | |
1183 | The default is Z_DEFAULT_COMPRESSION. |
1184 | |
1185 | Note, these constants are not imported by C<IO::Compress::Zip> by default. |
1186 | |
1187 | use IO::Compress::Zip qw(:strategy); |
1188 | use IO::Compress::Zip qw(:constants); |
1189 | use IO::Compress::Zip qw(:all); |
1190 | |
1191 | =item -Strategy |
1192 | |
1193 | Defines the strategy used to tune the compression. Use one of the symbolic |
1194 | constants defined below. |
1195 | |
1196 | Z_FILTERED |
1197 | Z_HUFFMAN_ONLY |
1198 | Z_RLE |
1199 | Z_FIXED |
1200 | Z_DEFAULT_STRATEGY |
1201 | |
1202 | The default is Z_DEFAULT_STRATEGY. |
1203 | |
e7d45986 |
1204 | =item C<< Strict => 0|1 >> |
25f0751f |
1205 | |
25f0751f |
1206 | This is a placeholder option. |
1207 | |
25f0751f |
1208 | =back |
1209 | |
1210 | =head2 Examples |
1211 | |
1212 | TODO |
1213 | |
1214 | =head1 Methods |
1215 | |
1216 | =head2 print |
1217 | |
1218 | Usage is |
1219 | |
1220 | $z->print($data) |
1221 | print $z $data |
1222 | |
1223 | Compresses and outputs the contents of the C<$data> parameter. This |
1224 | has the same behaviour as the C<print> built-in. |
1225 | |
1226 | Returns true if successful. |
1227 | |
1228 | =head2 printf |
1229 | |
1230 | Usage is |
1231 | |
1232 | $z->printf($format, $data) |
1233 | printf $z $format, $data |
1234 | |
1235 | Compresses and outputs the contents of the C<$data> parameter. |
1236 | |
1237 | Returns true if successful. |
1238 | |
1239 | =head2 syswrite |
1240 | |
1241 | Usage is |
1242 | |
1243 | $z->syswrite $data |
1244 | $z->syswrite $data, $length |
1245 | $z->syswrite $data, $length, $offset |
1246 | |
1247 | Compresses and outputs the contents of the C<$data> parameter. |
1248 | |
1249 | Returns the number of uncompressed bytes written, or C<undef> if |
1250 | unsuccessful. |
1251 | |
1252 | =head2 write |
1253 | |
1254 | Usage is |
1255 | |
1256 | $z->write $data |
1257 | $z->write $data, $length |
1258 | $z->write $data, $length, $offset |
1259 | |
1260 | Compresses and outputs the contents of the C<$data> parameter. |
1261 | |
1262 | Returns the number of uncompressed bytes written, or C<undef> if |
1263 | unsuccessful. |
1264 | |
1265 | =head2 flush |
1266 | |
1267 | Usage is |
1268 | |
25f0751f |
1269 | $z->flush; |
1270 | $z->flush($flush_type); |
1271 | |
25f0751f |
1272 | Flushes any pending compressed data to the output file/buffer. |
1273 | |
25f0751f |
1274 | This method takes an optional parameter, C<$flush_type>, that controls |
1275 | how the flushing will be carried out. By default the C<$flush_type> |
1276 | used is C<Z_FINISH>. Other valid values for C<$flush_type> are |
1277 | C<Z_NO_FLUSH>, C<Z_SYNC_FLUSH>, C<Z_FULL_FLUSH> and C<Z_BLOCK>. It is |
1278 | strongly recommended that you only set the C<flush_type> parameter if |
1279 | you fully understand the implications of what it does - overuse of C<flush> |
1280 | can seriously degrade the level of compression achieved. See the C<zlib> |
1281 | documentation for details. |
1282 | |
25f0751f |
1283 | Returns true on success. |
1284 | |
25f0751f |
1285 | =head2 tell |
1286 | |
1287 | Usage is |
1288 | |
1289 | $z->tell() |
1290 | tell $z |
1291 | |
1292 | Returns the uncompressed file offset. |
1293 | |
1294 | =head2 eof |
1295 | |
1296 | Usage is |
1297 | |
1298 | $z->eof(); |
1299 | eof($z); |
1300 | |
25f0751f |
1301 | Returns true if the C<close> method has been called. |
1302 | |
25f0751f |
1303 | =head2 seek |
1304 | |
1305 | $z->seek($position, $whence); |
1306 | seek($z, $position, $whence); |
1307 | |
25f0751f |
1308 | Provides a sub-set of the C<seek> functionality, with the restriction |
1309 | that it is only legal to seek forward in the output file/buffer. |
1310 | It is a fatal error to attempt to seek backward. |
1311 | |
1312 | Empty parts of the file/buffer will have NULL (0x00) bytes written to them. |
1313 | |
25f0751f |
1314 | The C<$whence> parameter takes one the usual values, namely SEEK_SET, |
1315 | SEEK_CUR or SEEK_END. |
1316 | |
1317 | Returns 1 on success, 0 on failure. |
1318 | |
1319 | =head2 binmode |
1320 | |
1321 | Usage is |
1322 | |
1323 | $z->binmode |
1324 | binmode $z ; |
1325 | |
1326 | This is a noop provided for completeness. |
1327 | |
1328 | =head2 opened |
1329 | |
1330 | $z->opened() |
1331 | |
1332 | Returns true if the object currently refers to a opened file/buffer. |
1333 | |
1334 | =head2 autoflush |
1335 | |
1336 | my $prev = $z->autoflush() |
1337 | my $prev = $z->autoflush(EXPR) |
1338 | |
1339 | If the C<$z> object is associated with a file or a filehandle, this method |
1340 | returns the current autoflush setting for the underlying filehandle. If |
1341 | C<EXPR> is present, and is non-zero, it will enable flushing after every |
1342 | write/print operation. |
1343 | |
1344 | If C<$z> is associated with a buffer, this method has no effect and always |
1345 | returns C<undef>. |
1346 | |
1347 | B<Note> that the special variable C<$|> B<cannot> be used to set or |
1348 | retrieve the autoflush setting. |
1349 | |
1350 | =head2 input_line_number |
1351 | |
1352 | $z->input_line_number() |
1353 | $z->input_line_number(EXPR) |
1354 | |
25f0751f |
1355 | This method always returns C<undef> when compressing. |
1356 | |
25f0751f |
1357 | =head2 fileno |
1358 | |
1359 | $z->fileno() |
1360 | fileno($z) |
1361 | |
d54256af |
1362 | If the C<$z> object is associated with a file or a filehandle, C<fileno> |
1363 | will return the underlying file descriptor. Once the C<close> method is |
1364 | called C<fileno> will return C<undef>. |
25f0751f |
1365 | |
d54256af |
1366 | If the C<$z> object is is associated with a buffer, this method will return |
1367 | C<undef>. |
25f0751f |
1368 | |
1369 | =head2 close |
1370 | |
1371 | $z->close() ; |
1372 | close $z ; |
1373 | |
25f0751f |
1374 | Flushes any pending compressed data and then closes the output file/buffer. |
1375 | |
25f0751f |
1376 | For most versions of Perl this method will be automatically invoked if |
1377 | the IO::Compress::Zip object is destroyed (either explicitly or by the |
1378 | variable with the reference to the object going out of scope). The |
1379 | exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In |
1380 | these cases, the C<close> method will be called automatically, but |
1381 | not until global destruction of all live objects when the program is |
1382 | terminating. |
1383 | |
1384 | Therefore, if you want your scripts to be able to run on all versions |
1385 | of Perl, you should call C<close> explicitly and not rely on automatic |
1386 | closing. |
1387 | |
1388 | Returns true on success, otherwise 0. |
1389 | |
1390 | If the C<AutoClose> option has been enabled when the IO::Compress::Zip |
1391 | object was created, and the object is associated with a file, the |
1392 | underlying file will also be closed. |
1393 | |
25f0751f |
1394 | =head2 newStream([OPTS]) |
1395 | |
1396 | Usage is |
1397 | |
1398 | $z->newStream( [OPTS] ) |
1399 | |
1400 | Closes the current compressed data stream and starts a new one. |
1401 | |
e7d45986 |
1402 | OPTS consists of any of the the options that are available when creating |
1403 | the C<$z> object. |
25f0751f |
1404 | |
e7d45986 |
1405 | See the L</"Constructor Options"> section for more details. |
25f0751f |
1406 | |
25f0751f |
1407 | =head2 deflateParams |
1408 | |
1409 | Usage is |
1410 | |
1411 | $z->deflateParams |
1412 | |
1413 | TODO |
1414 | |
25f0751f |
1415 | =head1 Importing |
1416 | |
25f0751f |
1417 | A number of symbolic constants are required by some methods in |
1418 | C<IO::Compress::Zip>. None are imported by default. |
1419 | |
25f0751f |
1420 | =over 5 |
1421 | |
1422 | =item :all |
1423 | |
25f0751f |
1424 | Imports C<zip>, C<$ZipError> and all symbolic |
1425 | constants that can be used by C<IO::Compress::Zip>. Same as doing this |
1426 | |
1427 | use IO::Compress::Zip qw(zip $ZipError :constants) ; |
1428 | |
1429 | =item :constants |
1430 | |
1431 | Import all symbolic constants. Same as doing this |
1432 | |
2b4e0969 |
1433 | use IO::Compress::Zip qw(:flush :level :strategy :zip_method) ; |
1434 | |
25f0751f |
1435 | =item :flush |
1436 | |
1437 | These symbolic constants are used by the C<flush> method. |
1438 | |
1439 | Z_NO_FLUSH |
1440 | Z_PARTIAL_FLUSH |
1441 | Z_SYNC_FLUSH |
1442 | Z_FULL_FLUSH |
1443 | Z_FINISH |
1444 | Z_BLOCK |
1445 | |
1446 | =item :level |
1447 | |
1448 | These symbolic constants are used by the C<Level> option in the constructor. |
1449 | |
1450 | Z_NO_COMPRESSION |
1451 | Z_BEST_SPEED |
1452 | Z_BEST_COMPRESSION |
1453 | Z_DEFAULT_COMPRESSION |
1454 | |
25f0751f |
1455 | =item :strategy |
1456 | |
1457 | These symbolic constants are used by the C<Strategy> option in the constructor. |
1458 | |
1459 | Z_FILTERED |
1460 | Z_HUFFMAN_ONLY |
1461 | Z_RLE |
1462 | Z_FIXED |
1463 | Z_DEFAULT_STRATEGY |
2b4e0969 |
1464 | |
2b4e0969 |
1465 | =item :zip_method |
1466 | |
1467 | These symbolic constants are used by the C<Method> option in the |
1468 | constructor. |
1469 | |
1470 | ZIP_CM_STORE |
1471 | ZIP_CM_DEFLATE |
1472 | ZIP_CM_BZIP2 |
1473 | |
1474 | |
25f0751f |
1475 | |
1476 | |
1477 | =back |
1478 | |
25f0751f |
1479 | =head1 EXAMPLES |
1480 | |
d54256af |
1481 | =head2 Apache::GZip Revisited |
e7d45986 |
1482 | |
319fab50 |
1483 | See L<IO::Compress::FAQ|IO::Compress::FAQ/"Apache::GZip Revisited"> |
e7d45986 |
1484 | |
d54256af |
1485 | |
e7d45986 |
1486 | |
d54256af |
1487 | =head2 Working with Net::FTP |
e7d45986 |
1488 | |
319fab50 |
1489 | See L<IO::Compress::FAQ|IO::Compress::FAQ/"Compressed files and Net::FTP"> |
e7d45986 |
1490 | |
25f0751f |
1491 | =head1 SEE ALSO |
1492 | |
258133d1 |
1493 | L<Compress::Zlib>, L<IO::Compress::Gzip>, L<IO::Uncompress::Gunzip>, L<IO::Compress::Deflate>, L<IO::Uncompress::Inflate>, L<IO::Compress::RawDeflate>, L<IO::Uncompress::RawInflate>, L<IO::Compress::Bzip2>, L<IO::Uncompress::Bunzip2>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress> |
25f0751f |
1494 | |
1495 | L<Compress::Zlib::FAQ|Compress::Zlib::FAQ> |
1496 | |
1497 | L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>, |
1498 | L<Archive::Tar|Archive::Tar>, |
1499 | L<IO::Zlib|IO::Zlib> |
1500 | |
25f0751f |
1501 | For RFC 1950, 1951 and 1952 see |
1502 | F<http://www.faqs.org/rfcs/rfc1950.html>, |
1503 | F<http://www.faqs.org/rfcs/rfc1951.html> and |
1504 | F<http://www.faqs.org/rfcs/rfc1952.html> |
1505 | |
1506 | The I<zlib> compression library was written by Jean-loup Gailly |
1507 | F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>. |
1508 | |
1509 | The primary site for the I<zlib> compression library is |
1510 | F<http://www.zlib.org>. |
1511 | |
1512 | The primary site for gzip is F<http://www.gzip.org>. |
1513 | |
25f0751f |
1514 | =head1 AUTHOR |
1515 | |
cb7abd7f |
1516 | This module was written by Paul Marquess, F<pmqs@cpan.org>. |
25f0751f |
1517 | |
25f0751f |
1518 | =head1 MODIFICATION HISTORY |
1519 | |
1520 | See the Changes file. |
1521 | |
1522 | =head1 COPYRIGHT AND LICENSE |
25f0751f |
1523 | |
319fab50 |
1524 | Copyright (c) 2005-2009 Paul Marquess. All rights reserved. |
25f0751f |
1525 | |
1526 | This program is free software; you can redistribute it and/or |
1527 | modify it under the same terms as Perl itself. |
1528 | |