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