Commit | Line | Data |
a02d0f6f |
1 | package IO::Uncompress::Unzip; |
2 | |
3 | require 5.004 ; |
4 | |
5 | # for RFC1952 |
6 | |
7 | use strict ; |
8 | use warnings; |
9 | use bytes; |
10 | |
9b5fd1d4 |
11 | use IO::Uncompress::RawInflate 2.024 ; |
12 | use IO::Compress::Base::Common 2.024 qw(:Status createSelfTiedObject); |
13 | use IO::Uncompress::Adapter::Inflate 2.024 ; |
14 | use IO::Uncompress::Adapter::Identity 2.024 ; |
15 | use IO::Compress::Zlib::Extra 2.024 ; |
16 | use IO::Compress::Zip::Constants 2.024 ; |
a02d0f6f |
17 | |
9b5fd1d4 |
18 | use Compress::Raw::Zlib 2.024 qw(crc32) ; |
c70c1701 |
19 | |
2b4e0969 |
20 | BEGIN |
21 | { |
c70c1701 |
22 | eval { require IO::Uncompress::Adapter::Bunzip2 ; |
23 | import IO::Uncompress::Adapter::Bunzip2 } ; |
9b5fd1d4 |
24 | # eval { require IO::Uncompress::Adapter::UnLzma ; |
25 | # import IO::Uncompress::Adapter::UnLzma } ; |
2b4e0969 |
26 | } |
27 | |
28 | |
a02d0f6f |
29 | require Exporter ; |
30 | |
e7d45986 |
31 | our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $UnzipError, %headerLookup); |
a02d0f6f |
32 | |
9b5fd1d4 |
33 | $VERSION = '2.024'; |
a02d0f6f |
34 | $UnzipError = ''; |
35 | |
36 | @ISA = qw(Exporter IO::Uncompress::RawInflate); |
37 | @EXPORT_OK = qw( $UnzipError unzip ); |
38 | %EXPORT_TAGS = %IO::Uncompress::RawInflate::EXPORT_TAGS ; |
39 | push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ; |
40 | Exporter::export_ok_tags('all'); |
41 | |
e7d45986 |
42 | %headerLookup = ( |
43 | ZIP_CENTRAL_HDR_SIG, \&skipCentralDirectory, |
44 | ZIP_END_CENTRAL_HDR_SIG, \&skipEndCentralDirectory, |
45 | ZIP64_END_CENTRAL_REC_HDR_SIG, \&skipCentralDirectory64Rec, |
46 | ZIP64_END_CENTRAL_LOC_HDR_SIG, \&skipCentralDirectory64Loc, |
47 | ZIP64_ARCHIVE_EXTRA_SIG, \&skipArchiveExtra, |
48 | ZIP64_DIGITAL_SIGNATURE_SIG, \&skipDigitalSignature, |
49 | ); |
50 | |
a02d0f6f |
51 | sub new |
52 | { |
53 | my $class = shift ; |
54 | my $obj = createSelfTiedObject($class, \$UnzipError); |
55 | $obj->_create(undef, 0, @_); |
56 | } |
57 | |
58 | sub unzip |
59 | { |
60 | my $obj = createSelfTiedObject(undef, \$UnzipError); |
61 | return $obj->_inf(@_) ; |
62 | } |
63 | |
64 | sub getExtraParams |
65 | { |
9b5fd1d4 |
66 | use IO::Compress::Base::Common 2.024 qw(:Parse); |
a02d0f6f |
67 | |
68 | |
69 | return ( |
70 | # # Zip header fields |
10c2b2bb |
71 | 'Name' => [1, 1, Parse_any, undef], |
a02d0f6f |
72 | |
10c2b2bb |
73 | # 'Stream' => [1, 1, Parse_boolean, 1], |
74 | # This means reading the central directory to get |
75 | # 1. the local header offsets |
76 | # 2. The compressed data length |
a02d0f6f |
77 | ); |
78 | } |
79 | |
80 | sub ckParams |
81 | { |
82 | my $self = shift ; |
83 | my $got = shift ; |
84 | |
85 | # unzip always needs crc32 |
86 | $got->value('CRC32' => 1); |
87 | |
88 | *$self->{UnzipData}{Name} = $got->value('Name'); |
89 | |
90 | return 1; |
91 | } |
92 | |
a1787f24 |
93 | sub mkUncomp |
94 | { |
95 | my $self = shift ; |
96 | my $got = shift ; |
97 | |
98 | my $magic = $self->ckMagic() |
99 | or return 0; |
100 | |
101 | *$self->{Info} = $self->readHeader($magic) |
102 | or return undef ; |
103 | |
104 | return 1; |
105 | |
106 | } |
a02d0f6f |
107 | |
108 | sub ckMagic |
109 | { |
110 | my $self = shift; |
111 | |
112 | my $magic ; |
113 | $self->smartReadExact(\$magic, 4); |
114 | |
115 | *$self->{HeaderPending} = $magic ; |
116 | |
117 | return $self->HeaderError("Minimum header size is " . |
118 | 4 . " bytes") |
119 | if length $magic != 4 ; |
120 | |
121 | return $self->HeaderError("Bad Magic") |
122 | if ! _isZipMagic($magic) ; |
123 | |
124 | *$self->{Type} = 'zip'; |
125 | |
126 | return $magic ; |
127 | } |
128 | |
129 | |
10c2b2bb |
130 | sub fastForward |
131 | { |
132 | my $self = shift; |
133 | my $offset = shift; |
134 | |
135 | # TODO - if Stream isn't enabled & reading from file, use seek |
136 | |
137 | my $buffer = ''; |
138 | my $c = 1024 * 16; |
139 | |
140 | while ($offset > 0) |
141 | { |
142 | $c = length $offset |
143 | if length $offset < $c ; |
144 | |
145 | $offset -= $c; |
146 | |
147 | $self->smartReadExact(\$buffer, $c) |
148 | or return 0; |
149 | } |
150 | |
151 | return 1; |
152 | } |
153 | |
a02d0f6f |
154 | |
155 | sub readHeader |
156 | { |
157 | my $self = shift; |
158 | my $magic = shift ; |
159 | |
160 | my $name = *$self->{UnzipData}{Name} ; |
161 | my $hdr = $self->_readZipHeader($magic) ; |
162 | |
163 | while (defined $hdr) |
164 | { |
165 | if (! defined $name || $hdr->{Name} eq $name) |
166 | { |
167 | return $hdr ; |
168 | } |
169 | |
170 | # skip the data |
10c2b2bb |
171 | # TODO - when Stream is off, use seek |
a02d0f6f |
172 | my $buffer; |
173 | if (*$self->{ZipData}{Streaming}) { |
174 | |
175 | while (1) { |
176 | |
177 | my $b; |
178 | my $status = $self->smartRead(\$b, 1024 * 16); |
179 | return undef |
180 | if $status <= 0 ; |
181 | |
182 | my $temp_buf; |
183 | my $out; |
184 | $status = *$self->{Uncomp}->uncompr(\$b, \$temp_buf, 0, $out); |
185 | |
186 | return $self->saveErrorString(undef, *$self->{Uncomp}{Error}, |
187 | *$self->{Uncomp}{ErrorNo}) |
188 | if $self->saveStatus($status) == STATUS_ERROR; |
189 | |
190 | if ($status == STATUS_ENDSTREAM) { |
191 | *$self->{Uncomp}->reset(); |
192 | $self->pushBack($b) ; |
193 | last; |
194 | } |
195 | } |
196 | |
197 | # skip the trailer |
198 | $self->smartReadExact(\$buffer, $hdr->{TrailerLength}) |
199 | or return $self->saveErrorString(undef, "Truncated file"); |
200 | } |
201 | else { |
10c2b2bb |
202 | my $c = $hdr->{CompressedLength}->get64bit(); |
203 | $self->fastForward($c) |
a02d0f6f |
204 | or return $self->saveErrorString(undef, "Truncated file"); |
205 | $buffer = ''; |
206 | } |
207 | |
208 | $self->chkTrailer($buffer) == STATUS_OK |
209 | or return $self->saveErrorString(undef, "Truncated file"); |
210 | |
211 | $hdr = $self->_readFullZipHeader(); |
212 | |
213 | return $self->saveErrorString(undef, "Cannot find '$name'") |
214 | if $self->smartEof(); |
215 | } |
216 | |
217 | return undef; |
218 | } |
219 | |
220 | sub chkTrailer |
221 | { |
222 | my $self = shift; |
223 | my $trailer = shift; |
224 | |
225 | my ($sig, $CRC32, $cSize, $uSize) ; |
e7d45986 |
226 | my ($cSizeHi, $uSizeHi) = (0, 0); |
a02d0f6f |
227 | if (*$self->{ZipData}{Streaming}) { |
e7d45986 |
228 | $sig = unpack ("V", substr($trailer, 0, 4)); |
229 | $CRC32 = unpack ("V", substr($trailer, 4, 4)); |
230 | |
231 | if (*$self->{ZipData}{Zip64} ) { |
232 | $cSize = U64::newUnpack_V64 substr($trailer, 8, 8); |
233 | $uSize = U64::newUnpack_V64 substr($trailer, 16, 8); |
234 | } |
235 | else { |
236 | $cSize = U64::newUnpack_V32 substr($trailer, 8, 4); |
237 | $uSize = U64::newUnpack_V32 substr($trailer, 12, 4); |
238 | } |
239 | |
a02d0f6f |
240 | return $self->TrailerError("Data Descriptor signature, got $sig") |
c70c1701 |
241 | if $sig != ZIP_DATA_HDR_SIG; |
a02d0f6f |
242 | } |
243 | else { |
244 | ($CRC32, $cSize, $uSize) = |
245 | (*$self->{ZipData}{Crc32}, |
246 | *$self->{ZipData}{CompressedLen}, |
247 | *$self->{ZipData}{UnCompressedLen}); |
248 | } |
249 | |
10c2b2bb |
250 | *$self->{Info}{CRC32} = *$self->{ZipData}{CRC32} ; |
251 | *$self->{Info}{CompressedLength} = $cSize->get64bit(); |
252 | *$self->{Info}{UncompressedLength} = $uSize->get64bit(); |
253 | |
a02d0f6f |
254 | if (*$self->{Strict}) { |
2b4e0969 |
255 | return $self->TrailerError("CRC mismatch") |
256 | if $CRC32 != *$self->{ZipData}{CRC32} ; |
a02d0f6f |
257 | |
e7d45986 |
258 | return $self->TrailerError("CSIZE mismatch.") |
259 | if ! $cSize->equal(*$self->{CompSize}); |
a02d0f6f |
260 | |
e7d45986 |
261 | return $self->TrailerError("USIZE mismatch.") |
262 | if ! $uSize->equal(*$self->{UnCompSize}); |
a02d0f6f |
263 | } |
264 | |
265 | my $reachedEnd = STATUS_ERROR ; |
266 | # check for central directory or end of central directory |
267 | while (1) |
268 | { |
269 | my $magic ; |
270 | my $got = $self->smartRead(\$magic, 4); |
271 | |
272 | return $self->saveErrorString(STATUS_ERROR, "Truncated file") |
273 | if $got != 4 && *$self->{Strict}; |
274 | |
275 | if ($got == 0) { |
276 | return STATUS_EOF ; |
277 | } |
278 | elsif ($got < 0) { |
279 | return STATUS_ERROR ; |
280 | } |
281 | elsif ($got < 4) { |
282 | $self->pushBack($magic) ; |
283 | return STATUS_OK ; |
284 | } |
285 | |
286 | my $sig = unpack("V", $magic) ; |
287 | |
258133d1 |
288 | my $hdr; |
289 | if ($hdr = $headerLookup{$sig}) |
a02d0f6f |
290 | { |
258133d1 |
291 | if (&$hdr($self, $magic) != STATUS_OK ) { |
a02d0f6f |
292 | if (*$self->{Strict}) { |
293 | return STATUS_ERROR ; |
294 | } |
295 | else { |
296 | $self->clearError(); |
297 | return STATUS_OK ; |
298 | } |
299 | } |
e7d45986 |
300 | |
301 | if ($sig == ZIP_END_CENTRAL_HDR_SIG) |
302 | { |
303 | return STATUS_OK ; |
304 | last; |
a02d0f6f |
305 | } |
a02d0f6f |
306 | } |
c70c1701 |
307 | elsif ($sig == ZIP_LOCAL_HDR_SIG) |
a02d0f6f |
308 | { |
309 | $self->pushBack($magic) ; |
310 | return STATUS_OK ; |
311 | } |
312 | else |
313 | { |
314 | # put the data back |
315 | $self->pushBack($magic) ; |
316 | last; |
317 | } |
318 | } |
319 | |
320 | return $reachedEnd ; |
321 | } |
322 | |
323 | sub skipCentralDirectory |
324 | { |
325 | my $self = shift; |
326 | my $magic = shift ; |
327 | |
328 | my $buffer; |
329 | $self->smartReadExact(\$buffer, 46 - 4) |
330 | or return $self->TrailerError("Minimum header size is " . |
331 | 46 . " bytes") ; |
332 | |
333 | my $keep = $magic . $buffer ; |
334 | *$self->{HeaderPending} = $keep ; |
335 | |
336 | #my $versionMadeBy = unpack ("v", substr($buffer, 4-4, 2)); |
337 | #my $extractVersion = unpack ("v", substr($buffer, 6-4, 2)); |
338 | #my $gpFlag = unpack ("v", substr($buffer, 8-4, 2)); |
339 | #my $compressedMethod = unpack ("v", substr($buffer, 10-4, 2)); |
340 | #my $lastModTime = unpack ("V", substr($buffer, 12-4, 4)); |
341 | #my $crc32 = unpack ("V", substr($buffer, 16-4, 4)); |
e7d45986 |
342 | my $compressedLength = unpack ("V", substr($buffer, 20-4, 4)); |
343 | my $uncompressedLength = unpack ("V", substr($buffer, 24-4, 4)); |
a02d0f6f |
344 | my $filename_length = unpack ("v", substr($buffer, 28-4, 2)); |
345 | my $extra_length = unpack ("v", substr($buffer, 30-4, 2)); |
346 | my $comment_length = unpack ("v", substr($buffer, 32-4, 2)); |
347 | #my $disk_start = unpack ("v", substr($buffer, 34-4, 2)); |
348 | #my $int_file_attrib = unpack ("v", substr($buffer, 36-4, 2)); |
349 | #my $ext_file_attrib = unpack ("V", substr($buffer, 38-4, 2)); |
350 | #my $lcl_hdr_offset = unpack ("V", substr($buffer, 42-4, 2)); |
351 | |
352 | |
353 | my $filename; |
354 | my $extraField; |
355 | my $comment ; |
356 | if ($filename_length) |
357 | { |
358 | $self->smartReadExact(\$filename, $filename_length) |
2b4e0969 |
359 | or return $self->TruncatedTrailer("filename"); |
a02d0f6f |
360 | $keep .= $filename ; |
361 | } |
362 | |
363 | if ($extra_length) |
364 | { |
365 | $self->smartReadExact(\$extraField, $extra_length) |
2b4e0969 |
366 | or return $self->TruncatedTrailer("extra"); |
a02d0f6f |
367 | $keep .= $extraField ; |
368 | } |
369 | |
370 | if ($comment_length) |
371 | { |
372 | $self->smartReadExact(\$comment, $comment_length) |
2b4e0969 |
373 | or return $self->TruncatedTrailer("comment"); |
a02d0f6f |
374 | $keep .= $comment ; |
375 | } |
376 | |
377 | return STATUS_OK ; |
378 | } |
379 | |
e7d45986 |
380 | sub skipArchiveExtra |
381 | { |
382 | my $self = shift; |
383 | my $magic = shift ; |
384 | |
385 | my $buffer; |
386 | $self->smartReadExact(\$buffer, 4) |
387 | or return $self->TrailerError("Minimum header size is " . |
388 | 4 . " bytes") ; |
389 | |
390 | my $keep = $magic . $buffer ; |
391 | |
392 | my $size = unpack ("V", $buffer); |
393 | |
394 | $self->smartReadExact(\$buffer, $size) |
395 | or return $self->TrailerError("Minimum header size is " . |
396 | $size . " bytes") ; |
397 | |
398 | $keep .= $buffer ; |
399 | *$self->{HeaderPending} = $keep ; |
400 | |
401 | return STATUS_OK ; |
402 | } |
403 | |
404 | |
405 | sub skipCentralDirectory64Rec |
406 | { |
407 | my $self = shift; |
408 | my $magic = shift ; |
409 | |
410 | my $buffer; |
411 | $self->smartReadExact(\$buffer, 8) |
412 | or return $self->TrailerError("Minimum header size is " . |
413 | 8 . " bytes") ; |
414 | |
415 | my $keep = $magic . $buffer ; |
416 | |
417 | my ($sizeLo, $sizeHi) = unpack ("V V", $buffer); |
10c2b2bb |
418 | my $size = $sizeHi * 0xFFFFFFFF + $sizeLo; |
e7d45986 |
419 | |
10c2b2bb |
420 | $self->fastForward($size) |
e7d45986 |
421 | or return $self->TrailerError("Minimum header size is " . |
10c2b2bb |
422 | $size . " bytes") ; |
e7d45986 |
423 | |
10c2b2bb |
424 | #$keep .= $buffer ; |
425 | #*$self->{HeaderPending} = $keep ; |
e7d45986 |
426 | |
427 | #my $versionMadeBy = unpack ("v", substr($buffer, 0, 2)); |
428 | #my $extractVersion = unpack ("v", substr($buffer, 2, 2)); |
429 | #my $diskNumber = unpack ("V", substr($buffer, 4, 4)); |
430 | #my $cntrlDirDiskNo = unpack ("V", substr($buffer, 8, 4)); |
431 | #my $entriesInThisCD = unpack ("V V", substr($buffer, 12, 8)); |
432 | #my $entriesInCD = unpack ("V V", substr($buffer, 20, 8)); |
433 | #my $sizeOfCD = unpack ("V V", substr($buffer, 28, 8)); |
434 | #my $offsetToCD = unpack ("V V", substr($buffer, 36, 8)); |
435 | |
436 | return STATUS_OK ; |
437 | } |
438 | |
439 | sub skipCentralDirectory64Loc |
440 | { |
441 | my $self = shift; |
442 | my $magic = shift ; |
443 | |
444 | my $buffer; |
445 | $self->smartReadExact(\$buffer, 20 - 4) |
446 | or return $self->TrailerError("Minimum header size is " . |
447 | 20 . " bytes") ; |
448 | |
449 | my $keep = $magic . $buffer ; |
450 | *$self->{HeaderPending} = $keep ; |
451 | |
452 | #my $startCdDisk = unpack ("V", substr($buffer, 4-4, 4)); |
453 | #my $offsetToCD = unpack ("V V", substr($buffer, 8-4, 8)); |
454 | #my $diskCount = unpack ("V", substr($buffer, 16-4, 4)); |
455 | |
456 | return STATUS_OK ; |
457 | } |
458 | |
a02d0f6f |
459 | sub skipEndCentralDirectory |
460 | { |
461 | my $self = shift; |
462 | my $magic = shift ; |
463 | |
464 | my $buffer; |
465 | $self->smartReadExact(\$buffer, 22 - 4) |
466 | or return $self->TrailerError("Minimum header size is " . |
467 | 22 . " bytes") ; |
468 | |
469 | my $keep = $magic . $buffer ; |
470 | *$self->{HeaderPending} = $keep ; |
471 | |
472 | #my $diskNumber = unpack ("v", substr($buffer, 4-4, 2)); |
473 | #my $cntrlDirDiskNo = unpack ("v", substr($buffer, 6-4, 2)); |
474 | #my $entriesInThisCD = unpack ("v", substr($buffer, 8-4, 2)); |
475 | #my $entriesInCD = unpack ("v", substr($buffer, 10-4, 2)); |
476 | #my $sizeOfCD = unpack ("V", substr($buffer, 12-4, 2)); |
477 | #my $offsetToCD = unpack ("V", substr($buffer, 16-4, 2)); |
478 | my $comment_length = unpack ("v", substr($buffer, 20-4, 2)); |
479 | |
480 | |
481 | my $comment ; |
482 | if ($comment_length) |
483 | { |
484 | $self->smartReadExact(\$comment, $comment_length) |
2b4e0969 |
485 | or return $self->TruncatedTrailer("comment"); |
a02d0f6f |
486 | $keep .= $comment ; |
487 | } |
488 | |
489 | return STATUS_OK ; |
490 | } |
491 | |
492 | |
a02d0f6f |
493 | sub _isZipMagic |
494 | { |
495 | my $buffer = shift ; |
496 | return 0 if length $buffer < 4 ; |
497 | my $sig = unpack("V", $buffer) ; |
c70c1701 |
498 | return $sig == ZIP_LOCAL_HDR_SIG ; |
a02d0f6f |
499 | } |
500 | |
501 | |
502 | sub _readFullZipHeader($) |
503 | { |
504 | my ($self) = @_ ; |
505 | my $magic = '' ; |
506 | |
507 | $self->smartReadExact(\$magic, 4); |
508 | |
509 | *$self->{HeaderPending} = $magic ; |
510 | |
511 | return $self->HeaderError("Minimum header size is " . |
512 | 30 . " bytes") |
513 | if length $magic != 4 ; |
514 | |
515 | |
516 | return $self->HeaderError("Bad Magic") |
517 | if ! _isZipMagic($magic) ; |
518 | |
519 | my $status = $self->_readZipHeader($magic); |
520 | delete *$self->{Transparent} if ! defined $status ; |
521 | return $status ; |
522 | } |
523 | |
524 | sub _readZipHeader($) |
525 | { |
526 | my ($self, $magic) = @_ ; |
527 | my ($HeaderCRC) ; |
528 | my ($buffer) = '' ; |
529 | |
530 | $self->smartReadExact(\$buffer, 30 - 4) |
531 | or return $self->HeaderError("Minimum header size is " . |
532 | 30 . " bytes") ; |
533 | |
534 | my $keep = $magic . $buffer ; |
535 | *$self->{HeaderPending} = $keep ; |
536 | |
537 | my $extractVersion = unpack ("v", substr($buffer, 4-4, 2)); |
538 | my $gpFlag = unpack ("v", substr($buffer, 6-4, 2)); |
539 | my $compressedMethod = unpack ("v", substr($buffer, 8-4, 2)); |
540 | my $lastModTime = unpack ("V", substr($buffer, 10-4, 4)); |
541 | my $crc32 = unpack ("V", substr($buffer, 14-4, 4)); |
10c2b2bb |
542 | my $compressedLength = U64::newUnpack_V32 substr($buffer, 18-4, 4); |
543 | my $uncompressedLength = U64::newUnpack_V32 substr($buffer, 22-4, 4); |
a02d0f6f |
544 | my $filename_length = unpack ("v", substr($buffer, 26-4, 2)); |
545 | my $extra_length = unpack ("v", substr($buffer, 28-4, 2)); |
546 | |
547 | my $filename; |
548 | my $extraField; |
c70c1701 |
549 | my @EXTRA = (); |
550 | my $streamingMode = ($gpFlag & ZIP_GP_FLAG_STREAMING_MASK) ? 1 : 0 ; |
a02d0f6f |
551 | |
552 | return $self->HeaderError("Streamed Stored content not supported") |
553 | if $streamingMode && $compressedMethod == 0 ; |
554 | |
258133d1 |
555 | return $self->HeaderError("Encrypted content not supported") |
556 | if $gpFlag & (ZIP_GP_FLAG_ENCRYPTED_MASK|ZIP_GP_FLAG_STRONG_ENCRYPTED_MASK); |
557 | |
558 | return $self->HeaderError("Patch content not supported") |
559 | if $gpFlag & ZIP_GP_FLAG_PATCHED_MASK; |
560 | |
a02d0f6f |
561 | *$self->{ZipData}{Streaming} = $streamingMode; |
562 | |
a02d0f6f |
563 | |
564 | if ($filename_length) |
565 | { |
566 | $self->smartReadExact(\$filename, $filename_length) |
c70c1701 |
567 | or return $self->TruncatedHeader("Filename"); |
a02d0f6f |
568 | $keep .= $filename ; |
569 | } |
570 | |
e7d45986 |
571 | my $zip64 = 0 ; |
572 | |
a02d0f6f |
573 | if ($extra_length) |
574 | { |
575 | $self->smartReadExact(\$extraField, $extra_length) |
c70c1701 |
576 | or return $self->TruncatedHeader("Extra Field"); |
577 | |
578 | my $bad = IO::Compress::Zlib::Extra::parseRawExtra($extraField, |
579 | \@EXTRA, 1, 0); |
580 | return $self->HeaderError($bad) |
581 | if defined $bad; |
582 | |
a02d0f6f |
583 | $keep .= $extraField ; |
e7d45986 |
584 | |
585 | my %Extra ; |
586 | for (@EXTRA) |
587 | { |
588 | $Extra{$_->[0]} = \$_->[1]; |
589 | } |
590 | |
591 | if (defined $Extra{ZIP_EXTRA_ID_ZIP64()}) |
592 | { |
593 | $zip64 = 1 ; |
594 | |
595 | my $buff = ${ $Extra{ZIP_EXTRA_ID_ZIP64()} }; |
596 | |
10c2b2bb |
597 | # This code assumes that all the fields in the Zip64 |
258133d1 |
598 | # extra field aren't necessarily present. The spec says that |
599 | # they only exist if the equivalent local headers are -1. |
258133d1 |
600 | |
601 | if (! $streamingMode) { |
602 | my $offset = 0 ; |
603 | |
10c2b2bb |
604 | if ($uncompressedLength->get32bit() == 0xFFFFFFFF ) { |
605 | $uncompressedLength |
606 | = U64::newUnpack_V64 substr($buff, 0, 8); |
258133d1 |
607 | |
10c2b2bb |
608 | $offset += 8 ; |
609 | } |
258133d1 |
610 | |
10c2b2bb |
611 | if ($compressedLength->get32bit() == 0xFFFFFFFF) { |
258133d1 |
612 | |
10c2b2bb |
613 | $compressedLength |
614 | = U64::newUnpack_V64 substr($buff, $offset, 8); |
258133d1 |
615 | |
10c2b2bb |
616 | $offset += 8 ; |
617 | } |
258133d1 |
618 | } |
e7d45986 |
619 | } |
620 | } |
621 | |
622 | *$self->{ZipData}{Zip64} = $zip64; |
623 | |
624 | if (! $streamingMode) { |
625 | *$self->{ZipData}{Streaming} = 0; |
626 | *$self->{ZipData}{Crc32} = $crc32; |
627 | *$self->{ZipData}{CompressedLen} = $compressedLength; |
628 | *$self->{ZipData}{UnCompressedLen} = $uncompressedLength; |
629 | *$self->{CompressedInputLengthRemaining} = |
10c2b2bb |
630 | *$self->{CompressedInputLength} = $compressedLength->get64bit(); |
a02d0f6f |
631 | } |
632 | |
10c2b2bb |
633 | *$self->{ZipData}{CRC32} = crc32(undef); |
2b4e0969 |
634 | *$self->{ZipData}{Method} = $compressedMethod; |
c70c1701 |
635 | if ($compressedMethod == ZIP_CM_DEFLATE) |
a02d0f6f |
636 | { |
2b4e0969 |
637 | *$self->{Type} = 'zip-deflate'; |
a1787f24 |
638 | my $obj = IO::Uncompress::Adapter::Inflate::mkUncompObject(1,0,0); |
639 | |
640 | *$self->{Uncomp} = $obj; |
2b4e0969 |
641 | } |
c70c1701 |
642 | elsif ($compressedMethod == ZIP_CM_BZIP2) |
2b4e0969 |
643 | { |
a1787f24 |
644 | return $self->HeaderError("Unsupported Compression format $compressedMethod") |
645 | if ! defined $IO::Uncompress::Adapter::Bunzip2::VERSION ; |
2b4e0969 |
646 | |
647 | *$self->{Type} = 'zip-bzip2'; |
648 | |
a1787f24 |
649 | my $obj = IO::Uncompress::Adapter::Bunzip2::mkUncompObject(); |
2b4e0969 |
650 | |
651 | *$self->{Uncomp} = $obj; |
a02d0f6f |
652 | } |
9b5fd1d4 |
653 | # elsif ($compressedMethod == ZIP_CM_LZMA) |
654 | # { |
655 | # return $self->HeaderError("Unsupported Compression format $compressedMethod") |
656 | # if ! defined $IO::Uncompress::Adapter::UnLzma::VERSION ; |
657 | # |
658 | # *$self->{Type} = 'zip-lzma'; |
659 | # my $LzmaHeader; |
660 | # $self->smartReadExact(\$LzmaHeader, 4) |
661 | # or return $self->saveErrorString(undef, "Truncated file"); |
662 | # my ($verHi, $verLo) = unpack ("CC", substr($LzmaHeader, 0, 2)); |
663 | # my $LzmaPropertiesSize = unpack ("v", substr($LzmaHeader, 2, 2)); |
664 | # |
665 | # |
666 | # my $LzmaPropertyData; |
667 | # $self->smartReadExact(\$LzmaPropertyData, $LzmaPropertiesSize) |
668 | # or return $self->saveErrorString(undef, "Truncated file"); |
669 | # #my $LzmaInfo = unpack ("C", substr($LzmaPropertyData, 0, 1)); |
670 | # #my $LzmaDictSize = unpack ("V", substr($LzmaPropertyData, 1, 4)); |
671 | # |
672 | # # Create an LZMA_Alone header |
673 | # $self->pushBack($LzmaPropertyData . |
674 | # $uncompressedLength->getPacked_V64()); |
675 | # |
676 | # my $obj = |
677 | # IO::Uncompress::Adapter::UnLzma::mkUncompObject(); |
678 | # |
679 | # *$self->{Uncomp} = $obj; |
680 | # } |
c70c1701 |
681 | elsif ($compressedMethod == ZIP_CM_STORE) |
a02d0f6f |
682 | { |
683 | # TODO -- add support for reading uncompressed |
684 | |
2b4e0969 |
685 | *$self->{Type} = 'zip-stored'; |
a02d0f6f |
686 | |
a1787f24 |
687 | my $obj = IO::Uncompress::Adapter::Identity::mkUncompObject(); |
a02d0f6f |
688 | |
689 | *$self->{Uncomp} = $obj; |
a02d0f6f |
690 | } |
691 | else |
692 | { |
693 | return $self->HeaderError("Unsupported Compression format $compressedMethod"); |
694 | } |
695 | |
696 | return { |
697 | 'Type' => 'zip', |
698 | 'FingerprintLength' => 4, |
699 | #'HeaderLength' => $compressedMethod == 8 ? length $keep : 0, |
700 | 'HeaderLength' => length $keep, |
e7d45986 |
701 | 'Zip64' => $zip64, |
702 | 'TrailerLength' => ! $streamingMode ? 0 : $zip64 ? 24 : 16, |
a02d0f6f |
703 | 'Header' => $keep, |
704 | 'CompressedLength' => $compressedLength , |
705 | 'UncompressedLength' => $uncompressedLength , |
706 | 'CRC32' => $crc32 , |
707 | 'Name' => $filename, |
708 | 'Time' => _dosToUnixTime($lastModTime), |
709 | 'Stream' => $streamingMode, |
710 | |
711 | 'MethodID' => $compressedMethod, |
c70c1701 |
712 | 'MethodName' => $compressedMethod == ZIP_CM_DEFLATE |
a02d0f6f |
713 | ? "Deflated" |
c70c1701 |
714 | : $compressedMethod == ZIP_CM_BZIP2 |
715 | ? "Bzip2" |
10c2b2bb |
716 | : $compressedMethod == ZIP_CM_LZMA |
717 | ? "Lzma" |
718 | : $compressedMethod == ZIP_CM_STORE |
719 | ? "Stored" |
720 | : "Unknown" , |
a02d0f6f |
721 | |
722 | # 'TextFlag' => $flag & GZIP_FLG_FTEXT ? 1 : 0, |
723 | # 'HeaderCRCFlag' => $flag & GZIP_FLG_FHCRC ? 1 : 0, |
724 | # 'NameFlag' => $flag & GZIP_FLG_FNAME ? 1 : 0, |
725 | # 'CommentFlag' => $flag & GZIP_FLG_FCOMMENT ? 1 : 0, |
726 | # 'ExtraFlag' => $flag & GZIP_FLG_FEXTRA ? 1 : 0, |
727 | # 'Comment' => $comment, |
728 | # 'OsID' => $os, |
729 | # 'OsName' => defined $GZIP_OS_Names{$os} |
730 | # ? $GZIP_OS_Names{$os} : "Unknown", |
731 | # 'HeaderCRC' => $HeaderCRC, |
732 | # 'Flags' => $flag, |
733 | # 'ExtraFlags' => $xfl, |
c70c1701 |
734 | 'ExtraFieldRaw' => $extraField, |
735 | 'ExtraField' => [ @EXTRA ], |
a02d0f6f |
736 | |
737 | |
738 | } |
739 | } |
740 | |
2b4e0969 |
741 | sub filterUncompressed |
742 | { |
743 | my $self = shift ; |
744 | |
10c2b2bb |
745 | if (*$self->{ZipData}{Method} == ZIP_CM_DEFLATE) { |
746 | *$self->{ZipData}{CRC32} = *$self->{Uncomp}->crc32() ; |
2b4e0969 |
747 | } |
748 | else { |
10c2b2bb |
749 | *$self->{ZipData}{CRC32} = crc32(${$_[0]}, *$self->{ZipData}{CRC32}); |
2b4e0969 |
750 | } |
751 | } |
752 | |
753 | |
dc82791d |
754 | # from Archive::Zip & info-zip |
a02d0f6f |
755 | sub _dosToUnixTime |
756 | { |
a02d0f6f |
757 | my $dt = shift; |
758 | |
759 | my $year = ( ( $dt >> 25 ) & 0x7f ) + 80; |
760 | my $mon = ( ( $dt >> 21 ) & 0x0f ) - 1; |
761 | my $mday = ( ( $dt >> 16 ) & 0x1f ); |
762 | |
763 | my $hour = ( ( $dt >> 11 ) & 0x1f ); |
764 | my $min = ( ( $dt >> 5 ) & 0x3f ); |
765 | my $sec = ( ( $dt << 1 ) & 0x3e ); |
766 | |
dc82791d |
767 | |
768 | use POSIX 'mktime'; |
769 | |
770 | my $time_t = mktime( $sec, $min, $hour, $mday, $mon, $year, 0, 0, -1 ); |
771 | return 0 if ! defined $time_t; |
a02d0f6f |
772 | return $time_t; |
773 | } |
774 | |
775 | |
776 | 1; |
777 | |
778 | __END__ |
779 | |
780 | |
781 | =head1 NAME |
782 | |
cb7abd7f |
783 | IO::Uncompress::Unzip - Read zip files/buffers |
784 | |
a02d0f6f |
785 | =head1 SYNOPSIS |
786 | |
787 | use IO::Uncompress::Unzip qw(unzip $UnzipError) ; |
788 | |
789 | my $status = unzip $input => $output [,OPTS] |
790 | or die "unzip failed: $UnzipError\n"; |
791 | |
792 | my $z = new IO::Uncompress::Unzip $input [OPTS] |
793 | or die "unzip failed: $UnzipError\n"; |
794 | |
795 | $status = $z->read($buffer) |
796 | $status = $z->read($buffer, $length) |
797 | $status = $z->read($buffer, $length, $offset) |
798 | $line = $z->getline() |
799 | $char = $z->getc() |
800 | $char = $z->ungetc() |
801 | $char = $z->opened() |
802 | |
803 | $status = $z->inflateSync() |
804 | |
e7d45986 |
805 | $data = $z->trailingData() |
806 | $status = $z->nextStream() |
a02d0f6f |
807 | $data = $z->getHeaderInfo() |
808 | $z->tell() |
809 | $z->seek($position, $whence) |
810 | $z->binmode() |
811 | $z->fileno() |
812 | $z->eof() |
813 | $z->close() |
814 | |
815 | $UnzipError ; |
816 | |
817 | # IO::File mode |
818 | |
819 | <$z> |
820 | read($z, $buffer); |
821 | read($z, $buffer, $length); |
822 | read($z, $buffer, $length, $offset); |
823 | tell($z) |
824 | seek($z, $position, $whence) |
825 | binmode($z) |
826 | fileno($z) |
827 | eof($z) |
828 | close($z) |
829 | |
a02d0f6f |
830 | =head1 DESCRIPTION |
831 | |
a02d0f6f |
832 | This module provides a Perl interface that allows the reading of |
833 | zlib files/buffers. |
834 | |
835 | For writing zip files/buffers, see the companion module IO::Compress::Zip. |
836 | |
a02d0f6f |
837 | =head1 Functional Interface |
838 | |
839 | A top-level function, C<unzip>, is provided to carry out |
840 | "one-shot" uncompression between buffers and/or files. For finer |
841 | control over the uncompression process, see the L</"OO Interface"> |
842 | section. |
843 | |
844 | use IO::Uncompress::Unzip qw(unzip $UnzipError) ; |
845 | |
846 | unzip $input => $output [,OPTS] |
847 | or die "unzip failed: $UnzipError\n"; |
848 | |
a02d0f6f |
849 | The functional interface needs Perl5.005 or better. |
850 | |
a02d0f6f |
851 | =head2 unzip $input => $output [, OPTS] |
852 | |
a02d0f6f |
853 | C<unzip> expects at least two parameters, C<$input> and C<$output>. |
854 | |
855 | =head3 The C<$input> parameter |
856 | |
857 | The parameter, C<$input>, is used to define the source of |
858 | the compressed data. |
859 | |
860 | It can take one of the following forms: |
861 | |
862 | =over 5 |
863 | |
864 | =item A filename |
865 | |
866 | If the C<$input> parameter is a simple scalar, it is assumed to be a |
867 | filename. This file will be opened for reading and the input data |
868 | will be read from it. |
869 | |
870 | =item A filehandle |
871 | |
872 | If the C<$input> parameter is a filehandle, the input data will be |
873 | read from it. |
874 | The string '-' can be used as an alias for standard input. |
875 | |
876 | =item A scalar reference |
877 | |
878 | If C<$input> is a scalar reference, the input data will be read |
879 | from C<$$input>. |
880 | |
881 | =item An array reference |
882 | |
883 | If C<$input> is an array reference, each element in the array must be a |
884 | filename. |
885 | |
886 | The input data will be read from each file in turn. |
887 | |
888 | The complete array will be walked to ensure that it only |
889 | contains valid filenames before any data is uncompressed. |
890 | |
a02d0f6f |
891 | =item An Input FileGlob string |
892 | |
893 | If C<$input> is a string that is delimited by the characters "<" and ">" |
894 | C<unzip> will assume that it is an I<input fileglob string>. The |
895 | input is the list of files that match the fileglob. |
896 | |
897 | If the fileglob does not match any files ... |
898 | |
899 | See L<File::GlobMapper|File::GlobMapper> for more details. |
900 | |
a02d0f6f |
901 | =back |
902 | |
903 | If the C<$input> parameter is any other type, C<undef> will be returned. |
904 | |
a02d0f6f |
905 | =head3 The C<$output> parameter |
906 | |
907 | The parameter C<$output> is used to control the destination of the |
908 | uncompressed data. This parameter can take one of these forms. |
909 | |
910 | =over 5 |
911 | |
912 | =item A filename |
913 | |
914 | If the C<$output> parameter is a simple scalar, it is assumed to be a |
915 | filename. This file will be opened for writing and the uncompressed |
916 | data will be written to it. |
917 | |
918 | =item A filehandle |
919 | |
920 | If the C<$output> parameter is a filehandle, the uncompressed data |
921 | will be written to it. |
922 | The string '-' can be used as an alias for standard output. |
923 | |
a02d0f6f |
924 | =item A scalar reference |
925 | |
926 | If C<$output> is a scalar reference, the uncompressed data will be |
927 | stored in C<$$output>. |
928 | |
a02d0f6f |
929 | =item An Array Reference |
930 | |
931 | If C<$output> is an array reference, the uncompressed data will be |
932 | pushed onto the array. |
933 | |
934 | =item An Output FileGlob |
935 | |
936 | If C<$output> is a string that is delimited by the characters "<" and ">" |
937 | C<unzip> will assume that it is an I<output fileglob string>. The |
938 | output is the list of files that match the fileglob. |
939 | |
940 | When C<$output> is an fileglob string, C<$input> must also be a fileglob |
941 | string. Anything else is an error. |
942 | |
943 | =back |
944 | |
945 | If the C<$output> parameter is any other type, C<undef> will be returned. |
946 | |
a02d0f6f |
947 | =head2 Notes |
948 | |
c70c1701 |
949 | When C<$input> maps to multiple compressed files/buffers and C<$output> is |
950 | a single file/buffer, after uncompression C<$output> will contain a |
951 | concatenation of all the uncompressed data from each of the input |
952 | files/buffers. |
953 | |
a02d0f6f |
954 | =head2 Optional Parameters |
955 | |
956 | Unless specified below, the optional parameters for C<unzip>, |
957 | C<OPTS>, are the same as those used with the OO interface defined in the |
958 | L</"Constructor Options"> section below. |
959 | |
960 | =over 5 |
961 | |
e7d45986 |
962 | =item C<< AutoClose => 0|1 >> |
a02d0f6f |
963 | |
964 | This option applies to any input or output data streams to |
965 | C<unzip> that are filehandles. |
966 | |
967 | If C<AutoClose> is specified, and the value is true, it will result in all |
968 | input and/or output filehandles being closed once C<unzip> has |
969 | completed. |
970 | |
971 | This parameter defaults to 0. |
972 | |
e7d45986 |
973 | =item C<< BinModeOut => 0|1 >> |
a02d0f6f |
974 | |
975 | When writing to a file or filehandle, set C<binmode> before writing to the |
976 | file. |
977 | |
978 | Defaults to 0. |
979 | |
e7d45986 |
980 | =item C<< Append => 0|1 >> |
a02d0f6f |
981 | |
9b5fd1d4 |
982 | The behaviour of this option is dependent on the type of output data |
983 | stream. |
984 | |
985 | =over 5 |
986 | |
987 | =item * A Buffer |
988 | |
989 | If C<Append> is enabled, all uncompressed data will be append to the end of |
990 | the output buffer. Otherwise the output buffer will be cleared before any |
991 | uncompressed data is written to it. |
992 | |
993 | =item * A Filename |
994 | |
995 | If C<Append> is enabled, the file will be opened in append mode. Otherwise |
996 | the contents of the file, if any, will be truncated before any uncompressed |
997 | data is written to it. |
998 | |
999 | =item * A Filehandle |
1000 | |
1001 | If C<Append> is enabled, the filehandle will be positioned to the end of |
1002 | the file via a call to C<seek> before any uncompressed data is |
1003 | written to it. Otherwise the file pointer will not be moved. |
1004 | |
1005 | =back |
1006 | |
1007 | When C<Append> is specified, and set to true, it will I<append> all uncompressed |
1008 | data to the output data stream. |
1009 | |
1010 | So when the output is a filehandle it will carry out a seek to the eof |
1011 | before writing any uncompressed data. If the output is a filename, it will be opened for |
1012 | appending. If the output is a buffer, all uncompressed data will be appened to |
1013 | the existing buffer. |
1014 | |
1015 | Conversely when C<Append> is not specified, or it is present and is set to |
1016 | false, it will operate as follows. |
1017 | |
1018 | When the output is a filename, it will truncate the contents of the file |
1019 | before writing any uncompressed data. If the output is a filehandle |
1020 | its position will not be changed. If the output is a buffer, it will be |
1021 | wiped before any uncompressed data is output. |
1022 | |
1023 | Defaults to 0. |
a02d0f6f |
1024 | |
e7d45986 |
1025 | =item C<< MultiStream => 0|1 >> |
a02d0f6f |
1026 | |
e7d45986 |
1027 | If the input file/buffer contains multiple compressed data streams, this |
1028 | option will uncompress the whole lot as a single data stream. |
a02d0f6f |
1029 | |
e7d45986 |
1030 | Defaults to 0. |
a02d0f6f |
1031 | |
258133d1 |
1032 | =item C<< TrailingData => $scalar >> |
1033 | |
1034 | Returns the data, if any, that is present immediately after the compressed |
1035 | data stream once uncompression is complete. |
1036 | |
1037 | This option can be used when there is useful information immediately |
1038 | following the compressed data stream, and you don't know the length of the |
1039 | compressed data stream. |
1040 | |
1041 | If the input is a buffer, C<trailingData> will return everything from the |
1042 | end of the compressed data stream to the end of the buffer. |
1043 | |
1044 | If the input is a filehandle, C<trailingData> will return the data that is |
1045 | left in the filehandle input buffer once the end of the compressed data |
1046 | stream has been reached. You can then use the filehandle to read the rest |
1047 | of the input file. |
1048 | |
1049 | Don't bother using C<trailingData> if the input is a filename. |
1050 | |
258133d1 |
1051 | If you know the length of the compressed data stream before you start |
1052 | uncompressing, you can avoid having to use C<trailingData> by setting the |
1053 | C<InputLength> option. |
1054 | |
a02d0f6f |
1055 | =back |
1056 | |
a02d0f6f |
1057 | =head2 Examples |
1058 | |
1059 | To read the contents of the file C<file1.txt.zip> and write the |
10c2b2bb |
1060 | uncompressed data to the file C<file1.txt>. |
a02d0f6f |
1061 | |
1062 | use strict ; |
1063 | use warnings ; |
1064 | use IO::Uncompress::Unzip qw(unzip $UnzipError) ; |
1065 | |
1066 | my $input = "file1.txt.zip"; |
1067 | my $output = "file1.txt"; |
1068 | unzip $input => $output |
1069 | or die "unzip failed: $UnzipError\n"; |
1070 | |
a02d0f6f |
1071 | To read from an existing Perl filehandle, C<$input>, and write the |
1072 | uncompressed data to a buffer, C<$buffer>. |
1073 | |
1074 | use strict ; |
1075 | use warnings ; |
1076 | use IO::Uncompress::Unzip qw(unzip $UnzipError) ; |
1077 | use IO::File ; |
1078 | |
1079 | my $input = new IO::File "<file1.txt.zip" |
1080 | or die "Cannot open 'file1.txt.zip': $!\n" ; |
1081 | my $buffer ; |
1082 | unzip $input => \$buffer |
1083 | or die "unzip failed: $UnzipError\n"; |
1084 | |
1085 | To uncompress all files in the directory "/my/home" that match "*.txt.zip" and store the compressed data in the same directory |
1086 | |
1087 | use strict ; |
1088 | use warnings ; |
1089 | use IO::Uncompress::Unzip qw(unzip $UnzipError) ; |
1090 | |
1091 | unzip '</my/home/*.txt.zip>' => '</my/home/#1.txt>' |
1092 | or die "unzip failed: $UnzipError\n"; |
1093 | |
1094 | and if you want to compress each file one at a time, this will do the trick |
1095 | |
1096 | use strict ; |
1097 | use warnings ; |
1098 | use IO::Uncompress::Unzip qw(unzip $UnzipError) ; |
1099 | |
1100 | for my $input ( glob "/my/home/*.txt.zip" ) |
1101 | { |
1102 | my $output = $input; |
1103 | $output =~ s/.zip// ; |
1104 | unzip $input => $output |
1105 | or die "Error compressing '$input': $UnzipError\n"; |
1106 | } |
1107 | |
1108 | =head1 OO Interface |
1109 | |
1110 | =head2 Constructor |
1111 | |
1112 | The format of the constructor for IO::Uncompress::Unzip is shown below |
1113 | |
a02d0f6f |
1114 | my $z = new IO::Uncompress::Unzip $input [OPTS] |
1115 | or die "IO::Uncompress::Unzip failed: $UnzipError\n"; |
1116 | |
1117 | Returns an C<IO::Uncompress::Unzip> object on success and undef on failure. |
1118 | The variable C<$UnzipError> will contain an error message on failure. |
1119 | |
1120 | If you are running Perl 5.005 or better the object, C<$z>, returned from |
1121 | IO::Uncompress::Unzip can be used exactly like an L<IO::File|IO::File> filehandle. |
1122 | This means that all normal input file operations can be carried out with |
1123 | C<$z>. For example, to read a line from a compressed file/buffer you can |
1124 | use either of these forms |
1125 | |
1126 | $line = $z->getline(); |
1127 | $line = <$z>; |
1128 | |
1129 | The mandatory parameter C<$input> is used to determine the source of the |
1130 | compressed data. This parameter can take one of three forms. |
1131 | |
1132 | =over 5 |
1133 | |
1134 | =item A filename |
1135 | |
1136 | If the C<$input> parameter is a scalar, it is assumed to be a filename. This |
1137 | file will be opened for reading and the compressed data will be read from it. |
1138 | |
1139 | =item A filehandle |
1140 | |
1141 | If the C<$input> parameter is a filehandle, the compressed data will be |
1142 | read from it. |
1143 | The string '-' can be used as an alias for standard input. |
1144 | |
a02d0f6f |
1145 | =item A scalar reference |
1146 | |
1147 | If C<$input> is a scalar reference, the compressed data will be read from |
1148 | C<$$output>. |
1149 | |
1150 | =back |
1151 | |
1152 | =head2 Constructor Options |
1153 | |
a02d0f6f |
1154 | The option names defined below are case insensitive and can be optionally |
1155 | prefixed by a '-'. So all of the following are valid |
1156 | |
1157 | -AutoClose |
1158 | -autoclose |
1159 | AUTOCLOSE |
1160 | autoclose |
1161 | |
1162 | OPTS is a combination of the following options: |
1163 | |
1164 | =over 5 |
1165 | |
e7d45986 |
1166 | =item C<< AutoClose => 0|1 >> |
a02d0f6f |
1167 | |
1168 | This option is only valid when the C<$input> parameter is a filehandle. If |
1169 | specified, and the value is true, it will result in the file being closed once |
1170 | either the C<close> method is called or the IO::Uncompress::Unzip object is |
1171 | destroyed. |
1172 | |
1173 | This parameter defaults to 0. |
1174 | |
e7d45986 |
1175 | =item C<< MultiStream => 0|1 >> |
a02d0f6f |
1176 | |
e7d45986 |
1177 | Treats the complete zip file/buffer as a single compressed data |
1178 | stream. When reading in multi-stream mode each member of the zip |
1179 | file/buffer will be uncompressed in turn until the end of the file/buffer |
1180 | is encountered. |
a02d0f6f |
1181 | |
1182 | This parameter defaults to 0. |
1183 | |
e7d45986 |
1184 | =item C<< Prime => $string >> |
a02d0f6f |
1185 | |
1186 | This option will uncompress the contents of C<$string> before processing the |
1187 | input file/buffer. |
1188 | |
1189 | This option can be useful when the compressed data is embedded in another |
1190 | file/data structure and it is not possible to work out where the compressed |
1191 | data begins without having to read the first few bytes. If this is the |
1192 | case, the uncompression can be I<primed> with these bytes using this |
1193 | option. |
1194 | |
e7d45986 |
1195 | =item C<< Transparent => 0|1 >> |
a02d0f6f |
1196 | |
f6fd7794 |
1197 | If this option is set and the input file/buffer is not compressed data, |
a02d0f6f |
1198 | the module will allow reading of it anyway. |
1199 | |
f6fd7794 |
1200 | In addition, if the input file/buffer does contain compressed data and |
1201 | there is non-compressed data immediately following it, setting this option |
1202 | will make this module treat the whole file/bufffer as a single data stream. |
1203 | |
a02d0f6f |
1204 | This option defaults to 1. |
1205 | |
e7d45986 |
1206 | =item C<< BlockSize => $num >> |
a02d0f6f |
1207 | |
1208 | When reading the compressed input data, IO::Uncompress::Unzip will read it in |
1209 | blocks of C<$num> bytes. |
1210 | |
1211 | This option defaults to 4096. |
1212 | |
e7d45986 |
1213 | =item C<< InputLength => $size >> |
a02d0f6f |
1214 | |
1215 | When present this option will limit the number of compressed bytes read |
1216 | from the input file/buffer to C<$size>. This option can be used in the |
1217 | situation where there is useful data directly after the compressed data |
1218 | stream and you know beforehand the exact length of the compressed data |
1219 | stream. |
1220 | |
1221 | This option is mostly used when reading from a filehandle, in which case |
1222 | the file pointer will be left pointing to the first byte directly after the |
1223 | compressed data stream. |
1224 | |
a02d0f6f |
1225 | This option defaults to off. |
1226 | |
e7d45986 |
1227 | =item C<< Append => 0|1 >> |
a02d0f6f |
1228 | |
1229 | This option controls what the C<read> method does with uncompressed data. |
1230 | |
1231 | If set to 1, all uncompressed data will be appended to the output parameter |
1232 | of the C<read> method. |
1233 | |
1234 | If set to 0, the contents of the output parameter of the C<read> method |
1235 | will be overwritten by the uncompressed data. |
1236 | |
1237 | Defaults to 0. |
1238 | |
e7d45986 |
1239 | =item C<< Strict => 0|1 >> |
a02d0f6f |
1240 | |
a02d0f6f |
1241 | This option controls whether the extra checks defined below are used when |
1242 | carrying out the decompression. When Strict is on, the extra tests are |
1243 | carried out, when Strict is off they are not. |
1244 | |
1245 | The default for this option is off. |
1246 | |
a02d0f6f |
1247 | =back |
1248 | |
1249 | =head2 Examples |
1250 | |
1251 | TODO |
1252 | |
1253 | =head1 Methods |
1254 | |
1255 | =head2 read |
1256 | |
1257 | Usage is |
1258 | |
1259 | $status = $z->read($buffer) |
1260 | |
1261 | Reads a block of compressed data (the size the the compressed block is |
1262 | determined by the C<Buffer> option in the constructor), uncompresses it and |
1263 | writes any uncompressed data into C<$buffer>. If the C<Append> parameter is |
1264 | set in the constructor, the uncompressed data will be appended to the |
1265 | C<$buffer> parameter. Otherwise C<$buffer> will be overwritten. |
1266 | |
1267 | Returns the number of uncompressed bytes written to C<$buffer>, zero if eof |
1268 | or a negative number on error. |
1269 | |
1270 | =head2 read |
1271 | |
1272 | Usage is |
1273 | |
1274 | $status = $z->read($buffer, $length) |
1275 | $status = $z->read($buffer, $length, $offset) |
1276 | |
1277 | $status = read($z, $buffer, $length) |
1278 | $status = read($z, $buffer, $length, $offset) |
1279 | |
1280 | Attempt to read C<$length> bytes of uncompressed data into C<$buffer>. |
1281 | |
1282 | The main difference between this form of the C<read> method and the |
1283 | previous one, is that this one will attempt to return I<exactly> C<$length> |
1284 | bytes. The only circumstances that this function will not is if end-of-file |
1285 | or an IO error is encountered. |
1286 | |
1287 | Returns the number of uncompressed bytes written to C<$buffer>, zero if eof |
1288 | or a negative number on error. |
1289 | |
a02d0f6f |
1290 | =head2 getline |
1291 | |
1292 | Usage is |
1293 | |
1294 | $line = $z->getline() |
1295 | $line = <$z> |
1296 | |
1297 | Reads a single line. |
1298 | |
258133d1 |
1299 | This method fully supports the use of of the variable C<$/> (or |
1300 | C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to |
1301 | determine what constitutes an end of line. Paragraph mode, record mode and |
1302 | file slurp mode are all supported. |
a02d0f6f |
1303 | |
a02d0f6f |
1304 | =head2 getc |
1305 | |
1306 | Usage is |
1307 | |
1308 | $char = $z->getc() |
1309 | |
1310 | Read a single character. |
1311 | |
1312 | =head2 ungetc |
1313 | |
1314 | Usage is |
1315 | |
1316 | $char = $z->ungetc($string) |
1317 | |
a02d0f6f |
1318 | =head2 inflateSync |
1319 | |
1320 | Usage is |
1321 | |
1322 | $status = $z->inflateSync() |
1323 | |
1324 | TODO |
1325 | |
a02d0f6f |
1326 | =head2 getHeaderInfo |
1327 | |
1328 | Usage is |
1329 | |
1330 | $hdr = $z->getHeaderInfo(); |
1331 | @hdrs = $z->getHeaderInfo(); |
1332 | |
1333 | This method returns either a hash reference (in scalar context) or a list |
1334 | or hash references (in array context) that contains information about each |
1335 | of the header fields in the compressed data stream(s). |
1336 | |
a02d0f6f |
1337 | =head2 tell |
1338 | |
1339 | Usage is |
1340 | |
1341 | $z->tell() |
1342 | tell $z |
1343 | |
1344 | Returns the uncompressed file offset. |
1345 | |
1346 | =head2 eof |
1347 | |
1348 | Usage is |
1349 | |
1350 | $z->eof(); |
1351 | eof($z); |
1352 | |
a02d0f6f |
1353 | Returns true if the end of the compressed input stream has been reached. |
1354 | |
a02d0f6f |
1355 | =head2 seek |
1356 | |
1357 | $z->seek($position, $whence); |
1358 | seek($z, $position, $whence); |
1359 | |
a02d0f6f |
1360 | Provides a sub-set of the C<seek> functionality, with the restriction |
1361 | that it is only legal to seek forward in the input file/buffer. |
1362 | It is a fatal error to attempt to seek backward. |
1363 | |
a02d0f6f |
1364 | The C<$whence> parameter takes one the usual values, namely SEEK_SET, |
1365 | SEEK_CUR or SEEK_END. |
1366 | |
1367 | Returns 1 on success, 0 on failure. |
1368 | |
1369 | =head2 binmode |
1370 | |
1371 | Usage is |
1372 | |
1373 | $z->binmode |
1374 | binmode $z ; |
1375 | |
1376 | This is a noop provided for completeness. |
1377 | |
1378 | =head2 opened |
1379 | |
1380 | $z->opened() |
1381 | |
1382 | Returns true if the object currently refers to a opened file/buffer. |
1383 | |
1384 | =head2 autoflush |
1385 | |
1386 | my $prev = $z->autoflush() |
1387 | my $prev = $z->autoflush(EXPR) |
1388 | |
1389 | If the C<$z> object is associated with a file or a filehandle, this method |
1390 | returns the current autoflush setting for the underlying filehandle. If |
1391 | C<EXPR> is present, and is non-zero, it will enable flushing after every |
1392 | write/print operation. |
1393 | |
1394 | If C<$z> is associated with a buffer, this method has no effect and always |
1395 | returns C<undef>. |
1396 | |
1397 | B<Note> that the special variable C<$|> B<cannot> be used to set or |
1398 | retrieve the autoflush setting. |
1399 | |
1400 | =head2 input_line_number |
1401 | |
1402 | $z->input_line_number() |
1403 | $z->input_line_number(EXPR) |
1404 | |
a02d0f6f |
1405 | Returns the current uncompressed line number. If C<EXPR> is present it has |
1406 | the effect of setting the line number. Note that setting the line number |
1407 | does not change the current position within the file/buffer being read. |
1408 | |
1409 | The contents of C<$/> are used to to determine what constitutes a line |
1410 | terminator. |
1411 | |
a02d0f6f |
1412 | =head2 fileno |
1413 | |
1414 | $z->fileno() |
1415 | fileno($z) |
1416 | |
d54256af |
1417 | If the C<$z> object is associated with a file or a filehandle, C<fileno> |
1418 | will return the underlying file descriptor. Once the C<close> method is |
1419 | called C<fileno> will return C<undef>. |
a02d0f6f |
1420 | |
d54256af |
1421 | If the C<$z> object is is associated with a buffer, this method will return |
1422 | C<undef>. |
a02d0f6f |
1423 | |
1424 | =head2 close |
1425 | |
1426 | $z->close() ; |
1427 | close $z ; |
1428 | |
a02d0f6f |
1429 | Closes the output file/buffer. |
1430 | |
a02d0f6f |
1431 | For most versions of Perl this method will be automatically invoked if |
1432 | the IO::Uncompress::Unzip object is destroyed (either explicitly or by the |
1433 | variable with the reference to the object going out of scope). The |
1434 | exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In |
1435 | these cases, the C<close> method will be called automatically, but |
1436 | not until global destruction of all live objects when the program is |
1437 | terminating. |
1438 | |
1439 | Therefore, if you want your scripts to be able to run on all versions |
1440 | of Perl, you should call C<close> explicitly and not rely on automatic |
1441 | closing. |
1442 | |
1443 | Returns true on success, otherwise 0. |
1444 | |
1445 | If the C<AutoClose> option has been enabled when the IO::Uncompress::Unzip |
1446 | object was created, and the object is associated with a file, the |
1447 | underlying file will also be closed. |
1448 | |
e7d45986 |
1449 | =head2 nextStream |
1450 | |
1451 | Usage is |
1452 | |
1453 | my $status = $z->nextStream(); |
1454 | |
1455 | Skips to the next compressed data stream in the input file/buffer. If a new |
258133d1 |
1456 | compressed data stream is found, the eof marker will be cleared and C<$.> |
1457 | will be reset to 0. |
e7d45986 |
1458 | |
1459 | Returns 1 if a new stream was found, 0 if none was found, and -1 if an |
1460 | error was encountered. |
1461 | |
1462 | =head2 trailingData |
1463 | |
1464 | Usage is |
1465 | |
1466 | my $data = $z->trailingData(); |
1467 | |
258133d1 |
1468 | Returns the data, if any, that is present immediately after the compressed |
1469 | data stream once uncompression is complete. It only makes sense to call |
1470 | this method once the end of the compressed data stream has been |
1471 | encountered. |
1472 | |
1473 | This option can be used when there is useful information immediately |
1474 | following the compressed data stream, and you don't know the length of the |
1475 | compressed data stream. |
1476 | |
1477 | If the input is a buffer, C<trailingData> will return everything from the |
1478 | end of the compressed data stream to the end of the buffer. |
1479 | |
1480 | If the input is a filehandle, C<trailingData> will return the data that is |
1481 | left in the filehandle input buffer once the end of the compressed data |
1482 | stream has been reached. You can then use the filehandle to read the rest |
1483 | of the input file. |
1484 | |
1485 | Don't bother using C<trailingData> if the input is a filename. |
1486 | |
258133d1 |
1487 | If you know the length of the compressed data stream before you start |
1488 | uncompressing, you can avoid having to use C<trailingData> by setting the |
1489 | C<InputLength> option in the constructor. |
e7d45986 |
1490 | |
a02d0f6f |
1491 | =head1 Importing |
1492 | |
1493 | No symbolic constants are required by this IO::Uncompress::Unzip at present. |
1494 | |
1495 | =over 5 |
1496 | |
1497 | =item :all |
1498 | |
1499 | Imports C<unzip> and C<$UnzipError>. |
1500 | Same as doing this |
1501 | |
1502 | use IO::Uncompress::Unzip qw(unzip $UnzipError) ; |
1503 | |
1504 | =back |
1505 | |
1506 | =head1 EXAMPLES |
1507 | |
d54256af |
1508 | =head2 Working with Net::FTP |
1509 | |
1510 | See L<IO::Uncompress::Unzip::FAQ|IO::Uncompress::Unzip::FAQ/"Compressed files and Net::FTP"> |
1511 | |
a02d0f6f |
1512 | =head1 SEE ALSO |
1513 | |
9b5fd1d4 |
1514 | 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::Lzma>, L<IO::Uncompress::UnLzma>, L<IO::Compress::Xz>, L<IO::Uncompress::UnXz>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress> |
a02d0f6f |
1515 | |
1516 | L<Compress::Zlib::FAQ|Compress::Zlib::FAQ> |
1517 | |
1518 | L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>, |
1519 | L<Archive::Tar|Archive::Tar>, |
1520 | L<IO::Zlib|IO::Zlib> |
1521 | |
a02d0f6f |
1522 | For RFC 1950, 1951 and 1952 see |
1523 | F<http://www.faqs.org/rfcs/rfc1950.html>, |
1524 | F<http://www.faqs.org/rfcs/rfc1951.html> and |
1525 | F<http://www.faqs.org/rfcs/rfc1952.html> |
1526 | |
1527 | The I<zlib> compression library was written by Jean-loup Gailly |
1528 | F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>. |
1529 | |
1530 | The primary site for the I<zlib> compression library is |
1531 | F<http://www.zlib.org>. |
1532 | |
1533 | The primary site for gzip is F<http://www.gzip.org>. |
1534 | |
a02d0f6f |
1535 | =head1 AUTHOR |
1536 | |
cb7abd7f |
1537 | This module was written by Paul Marquess, F<pmqs@cpan.org>. |
a02d0f6f |
1538 | |
a02d0f6f |
1539 | =head1 MODIFICATION HISTORY |
1540 | |
1541 | See the Changes file. |
1542 | |
1543 | =head1 COPYRIGHT AND LICENSE |
a02d0f6f |
1544 | |
9b5fd1d4 |
1545 | Copyright (c) 2005-2010 Paul Marquess. All rights reserved. |
a02d0f6f |
1546 | |
1547 | This program is free software; you can redistribute it and/or |
1548 | modify it under the same terms as Perl itself. |
1549 | |