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