Commit | Line | Data |
642e522c |
1 | |
2 | package IO::Uncompress::Gunzip ; |
3 | |
4 | require 5.004 ; |
5 | |
6 | # for RFC1952 |
7 | |
8 | use strict ; |
9 | use warnings; |
a02d0f6f |
10 | use bytes; |
642e522c |
11 | |
1a6a8453 |
12 | use IO::Uncompress::RawInflate ; |
13 | |
a02d0f6f |
14 | use Compress::Raw::Zlib qw( crc32 ) ; |
15 | use IO::Compress::Base::Common qw(:Status createSelfTiedObject); |
16 | use IO::Compress::Gzip::Constants; |
c70c1701 |
17 | use IO::Compress::Zlib::Extra; |
1a6a8453 |
18 | |
642e522c |
19 | require Exporter ; |
20 | |
21 | our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $GunzipError); |
22 | |
1a6a8453 |
23 | @ISA = qw( Exporter IO::Uncompress::RawInflate ); |
642e522c |
24 | @EXPORT_OK = qw( $GunzipError gunzip ); |
1a6a8453 |
25 | %EXPORT_TAGS = %IO::Uncompress::RawInflate::DEFLATE_CONSTANTS ; |
642e522c |
26 | push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ; |
27 | Exporter::export_ok_tags('all'); |
28 | |
642e522c |
29 | $GunzipError = ''; |
30 | |
c70c1701 |
31 | $VERSION = '2.000_12'; |
642e522c |
32 | |
1a6a8453 |
33 | sub new |
642e522c |
34 | { |
1a6a8453 |
35 | my $class = shift ; |
36 | $GunzipError = ''; |
37 | my $obj = createSelfTiedObject($class, \$GunzipError); |
642e522c |
38 | |
1a6a8453 |
39 | $obj->_create(undef, 0, @_); |
642e522c |
40 | } |
41 | |
1a6a8453 |
42 | sub gunzip |
642e522c |
43 | { |
1a6a8453 |
44 | my $obj = createSelfTiedObject(undef, \$GunzipError); |
45 | return $obj->_inf(@_) ; |
642e522c |
46 | } |
47 | |
1a6a8453 |
48 | sub getExtraParams |
642e522c |
49 | { |
a02d0f6f |
50 | use IO::Compress::Base::Common qw(:Parse); |
1a6a8453 |
51 | return ( 'ParseExtra' => [1, 1, Parse_boolean, 0] ) ; |
642e522c |
52 | } |
53 | |
1a6a8453 |
54 | sub ckParams |
642e522c |
55 | { |
1a6a8453 |
56 | my $self = shift ; |
57 | my $got = shift ; |
642e522c |
58 | |
1a6a8453 |
59 | # gunzip always needs crc32 |
60 | $got->value('CRC32' => 1); |
642e522c |
61 | |
1a6a8453 |
62 | return 1; |
642e522c |
63 | } |
64 | |
1a6a8453 |
65 | sub ckMagic |
642e522c |
66 | { |
1a6a8453 |
67 | my $self = shift; |
642e522c |
68 | |
1a6a8453 |
69 | my $magic ; |
70 | $self->smartReadExact(\$magic, GZIP_ID_SIZE); |
642e522c |
71 | |
1a6a8453 |
72 | *$self->{HeaderPending} = $magic ; |
642e522c |
73 | |
1a6a8453 |
74 | return $self->HeaderError("Minimum header size is " . |
75 | GZIP_MIN_HEADER_SIZE . " bytes") |
76 | if length $magic != GZIP_ID_SIZE ; |
642e522c |
77 | |
1a6a8453 |
78 | return $self->HeaderError("Bad Magic") |
79 | if ! isGzipMagic($magic) ; |
642e522c |
80 | |
1a6a8453 |
81 | *$self->{Type} = 'rfc1952'; |
642e522c |
82 | |
1a6a8453 |
83 | return $magic ; |
642e522c |
84 | } |
85 | |
1a6a8453 |
86 | sub readHeader |
642e522c |
87 | { |
1a6a8453 |
88 | my $self = shift; |
89 | my $magic = shift; |
642e522c |
90 | |
1a6a8453 |
91 | return $self->_readGzipHeader($magic); |
642e522c |
92 | } |
93 | |
1a6a8453 |
94 | sub chkTrailer |
642e522c |
95 | { |
1a6a8453 |
96 | my $self = shift; |
97 | my $trailer = shift; |
642e522c |
98 | |
1a6a8453 |
99 | # Check CRC & ISIZE |
100 | my ($CRC32, $ISIZE) = unpack("V V", $trailer) ; |
101 | *$self->{Info}{CRC32} = $CRC32; |
102 | *$self->{Info}{ISIZE} = $ISIZE; |
103 | |
104 | if (*$self->{Strict}) { |
105 | return $self->TrailerError("CRC mismatch") |
106 | if $CRC32 != *$self->{Uncomp}->crc32() ; |
107 | |
108 | my $exp_isize = *$self->{Uncomp}->uncompressedBytes(); |
109 | return $self->TrailerError("ISIZE mismatch. Got $ISIZE" |
110 | . ", expected $exp_isize") |
111 | if $ISIZE != $exp_isize ; |
642e522c |
112 | } |
113 | |
a02d0f6f |
114 | return STATUS_OK; |
1a6a8453 |
115 | } |
642e522c |
116 | |
1a6a8453 |
117 | sub isGzipMagic |
118 | { |
119 | my $buffer = shift ; |
120 | return 0 if length $buffer < GZIP_ID_SIZE ; |
121 | my ($id1, $id2) = unpack("C C", $buffer) ; |
122 | return $id1 == GZIP_ID1 && $id2 == GZIP_ID2 ; |
642e522c |
123 | } |
124 | |
1a6a8453 |
125 | sub _readFullGzipHeader($) |
642e522c |
126 | { |
1a6a8453 |
127 | my ($self) = @_ ; |
128 | my $magic = '' ; |
642e522c |
129 | |
1a6a8453 |
130 | $self->smartReadExact(\$magic, GZIP_ID_SIZE); |
642e522c |
131 | |
1a6a8453 |
132 | *$self->{HeaderPending} = $magic ; |
642e522c |
133 | |
1a6a8453 |
134 | return $self->HeaderError("Minimum header size is " . |
135 | GZIP_MIN_HEADER_SIZE . " bytes") |
136 | if length $magic != GZIP_ID_SIZE ; |
642e522c |
137 | |
642e522c |
138 | |
1a6a8453 |
139 | return $self->HeaderError("Bad Magic") |
140 | if ! isGzipMagic($magic) ; |
642e522c |
141 | |
1a6a8453 |
142 | my $status = $self->_readGzipHeader($magic); |
143 | delete *$self->{Transparent} if ! defined $status ; |
144 | return $status ; |
642e522c |
145 | } |
146 | |
1a6a8453 |
147 | sub _readGzipHeader($) |
642e522c |
148 | { |
1a6a8453 |
149 | my ($self, $magic) = @_ ; |
150 | my ($HeaderCRC) ; |
151 | my ($buffer) = '' ; |
642e522c |
152 | |
1a6a8453 |
153 | $self->smartReadExact(\$buffer, GZIP_MIN_HEADER_SIZE - GZIP_ID_SIZE) |
154 | or return $self->HeaderError("Minimum header size is " . |
155 | GZIP_MIN_HEADER_SIZE . " bytes") ; |
642e522c |
156 | |
1a6a8453 |
157 | my $keep = $magic . $buffer ; |
158 | *$self->{HeaderPending} = $keep ; |
642e522c |
159 | |
1a6a8453 |
160 | # now split out the various parts |
161 | my ($cm, $flag, $mtime, $xfl, $os) = unpack("C C V C C", $buffer) ; |
642e522c |
162 | |
1a6a8453 |
163 | $cm == GZIP_CM_DEFLATED |
164 | or return $self->HeaderError("Not Deflate (CM is $cm)") ; |
642e522c |
165 | |
1a6a8453 |
166 | # check for use of reserved bits |
167 | return $self->HeaderError("Use of Reserved Bits in FLG field.") |
168 | if $flag & GZIP_FLG_RESERVED ; |
642e522c |
169 | |
1a6a8453 |
170 | my $EXTRA ; |
171 | my @EXTRA = () ; |
172 | if ($flag & GZIP_FLG_FEXTRA) { |
173 | $EXTRA = "" ; |
174 | $self->smartReadExact(\$buffer, GZIP_FEXTRA_HEADER_SIZE) |
175 | or return $self->TruncatedHeader("FEXTRA Length") ; |
642e522c |
176 | |
1a6a8453 |
177 | my ($XLEN) = unpack("v", $buffer) ; |
178 | $self->smartReadExact(\$EXTRA, $XLEN) |
179 | or return $self->TruncatedHeader("FEXTRA Body"); |
180 | $keep .= $buffer . $EXTRA ; |
642e522c |
181 | |
1a6a8453 |
182 | if ($XLEN && *$self->{'ParseExtra'}) { |
c70c1701 |
183 | my $bad = IO::Compress::Zlib::Extra::parseRawExtra($EXTRA, |
184 | \@EXTRA, 1, 1); |
185 | return $self->HeaderError($bad) |
186 | if defined $bad; |
1a6a8453 |
187 | } |
188 | } |
642e522c |
189 | |
1a6a8453 |
190 | my $origname ; |
191 | if ($flag & GZIP_FLG_FNAME) { |
192 | $origname = "" ; |
193 | while (1) { |
194 | $self->smartReadExact(\$buffer, 1) |
195 | or return $self->TruncatedHeader("FNAME"); |
196 | last if $buffer eq GZIP_NULL_BYTE ; |
197 | $origname .= $buffer |
198 | } |
199 | $keep .= $origname . GZIP_NULL_BYTE ; |
642e522c |
200 | |
1a6a8453 |
201 | return $self->HeaderError("Non ISO 8859-1 Character found in Name") |
202 | if *$self->{Strict} && $origname =~ /$GZIP_FNAME_INVALID_CHAR_RE/o ; |
203 | } |
642e522c |
204 | |
1a6a8453 |
205 | my $comment ; |
206 | if ($flag & GZIP_FLG_FCOMMENT) { |
207 | $comment = ""; |
208 | while (1) { |
209 | $self->smartReadExact(\$buffer, 1) |
210 | or return $self->TruncatedHeader("FCOMMENT"); |
211 | last if $buffer eq GZIP_NULL_BYTE ; |
212 | $comment .= $buffer |
213 | } |
214 | $keep .= $comment . GZIP_NULL_BYTE ; |
642e522c |
215 | |
1a6a8453 |
216 | return $self->HeaderError("Non ISO 8859-1 Character found in Comment") |
217 | if *$self->{Strict} && $comment =~ /$GZIP_FCOMMENT_INVALID_CHAR_RE/o ; |
218 | } |
642e522c |
219 | |
1a6a8453 |
220 | if ($flag & GZIP_FLG_FHCRC) { |
221 | $self->smartReadExact(\$buffer, GZIP_FHCRC_SIZE) |
222 | or return $self->TruncatedHeader("FHCRC"); |
642e522c |
223 | |
1a6a8453 |
224 | $HeaderCRC = unpack("v", $buffer) ; |
225 | my $crc16 = crc32($keep) & 0xFF ; |
642e522c |
226 | |
1a6a8453 |
227 | return $self->HeaderError("CRC16 mismatch.") |
228 | if *$self->{Strict} && $crc16 != $HeaderCRC; |
642e522c |
229 | |
1a6a8453 |
230 | $keep .= $buffer ; |
231 | } |
642e522c |
232 | |
1a6a8453 |
233 | # Assume compression method is deflated for xfl tests |
234 | #if ($xfl) { |
235 | #} |
642e522c |
236 | |
1a6a8453 |
237 | *$self->{Type} = 'rfc1952'; |
642e522c |
238 | |
1a6a8453 |
239 | return { |
240 | 'Type' => 'rfc1952', |
241 | 'FingerprintLength' => 2, |
242 | 'HeaderLength' => length $keep, |
243 | 'TrailerLength' => GZIP_TRAILER_SIZE, |
244 | 'Header' => $keep, |
245 | 'isMinimalHeader' => $keep eq GZIP_MINIMUM_HEADER ? 1 : 0, |
642e522c |
246 | |
1a6a8453 |
247 | 'MethodID' => $cm, |
248 | 'MethodName' => $cm == GZIP_CM_DEFLATED ? "Deflated" : "Unknown" , |
249 | 'TextFlag' => $flag & GZIP_FLG_FTEXT ? 1 : 0, |
250 | 'HeaderCRCFlag' => $flag & GZIP_FLG_FHCRC ? 1 : 0, |
251 | 'NameFlag' => $flag & GZIP_FLG_FNAME ? 1 : 0, |
252 | 'CommentFlag' => $flag & GZIP_FLG_FCOMMENT ? 1 : 0, |
253 | 'ExtraFlag' => $flag & GZIP_FLG_FEXTRA ? 1 : 0, |
254 | 'Name' => $origname, |
255 | 'Comment' => $comment, |
256 | 'Time' => $mtime, |
257 | 'OsID' => $os, |
258 | 'OsName' => defined $GZIP_OS_Names{$os} |
259 | ? $GZIP_OS_Names{$os} : "Unknown", |
260 | 'HeaderCRC' => $HeaderCRC, |
261 | 'Flags' => $flag, |
262 | 'ExtraFlags' => $xfl, |
263 | 'ExtraFieldRaw' => $EXTRA, |
264 | 'ExtraField' => [ @EXTRA ], |
642e522c |
265 | |
642e522c |
266 | |
1a6a8453 |
267 | #'CompSize'=> $compsize, |
268 | #'CRC32'=> $CRC32, |
269 | #'OrigSize'=> $ISIZE, |
270 | } |
642e522c |
271 | } |
272 | |
273 | |
1a6a8453 |
274 | 1; |
642e522c |
275 | |
642e522c |
276 | __END__ |
277 | |
278 | |
279 | =head1 NAME |
280 | |
a02d0f6f |
281 | |
cb7abd7f |
282 | |
283 | IO::Uncompress::Gunzip - Read RFC 1952 files/buffers |
284 | |
642e522c |
285 | |
a02d0f6f |
286 | |
642e522c |
287 | =head1 SYNOPSIS |
288 | |
289 | use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; |
290 | |
291 | my $status = gunzip $input => $output [,OPTS] |
292 | or die "gunzip failed: $GunzipError\n"; |
293 | |
294 | my $z = new IO::Uncompress::Gunzip $input [OPTS] |
295 | or die "gunzip failed: $GunzipError\n"; |
296 | |
297 | $status = $z->read($buffer) |
298 | $status = $z->read($buffer, $length) |
299 | $status = $z->read($buffer, $length, $offset) |
300 | $line = $z->getline() |
301 | $char = $z->getc() |
302 | $char = $z->ungetc() |
a02d0f6f |
303 | $char = $z->opened() |
304 | |
642e522c |
305 | $status = $z->inflateSync() |
a02d0f6f |
306 | |
642e522c |
307 | $z->trailingData() |
308 | $data = $z->getHeaderInfo() |
309 | $z->tell() |
310 | $z->seek($position, $whence) |
311 | $z->binmode() |
312 | $z->fileno() |
313 | $z->eof() |
314 | $z->close() |
315 | |
316 | $GunzipError ; |
317 | |
318 | # IO::File mode |
319 | |
320 | <$z> |
321 | read($z, $buffer); |
322 | read($z, $buffer, $length); |
323 | read($z, $buffer, $length, $offset); |
324 | tell($z) |
325 | seek($z, $position, $whence) |
326 | binmode($z) |
327 | fileno($z) |
328 | eof($z) |
329 | close($z) |
330 | |
331 | |
332 | =head1 DESCRIPTION |
333 | |
334 | |
335 | |
336 | B<WARNING -- This is a Beta release>. |
337 | |
338 | =over 5 |
339 | |
340 | =item * DO NOT use in production code. |
341 | |
342 | =item * The documentation is incomplete in places. |
343 | |
344 | =item * Parts of the interface defined here are tentative. |
345 | |
346 | =item * Please report any problems you find. |
347 | |
348 | =back |
349 | |
350 | |
351 | |
352 | |
353 | |
1a6a8453 |
354 | This module provides a Perl interface that allows the reading of |
642e522c |
355 | files/buffers that conform to RFC 1952. |
356 | |
1a6a8453 |
357 | For writing RFC 1952 files/buffers, see the companion module IO::Compress::Gzip. |
642e522c |
358 | |
359 | |
360 | |
cb7abd7f |
361 | |
362 | |
363 | |
642e522c |
364 | =head1 Functional Interface |
365 | |
1a6a8453 |
366 | A top-level function, C<gunzip>, is provided to carry out |
367 | "one-shot" uncompression between buffers and/or files. For finer |
368 | control over the uncompression process, see the L</"OO Interface"> |
369 | section. |
642e522c |
370 | |
371 | use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; |
372 | |
373 | gunzip $input => $output [,OPTS] |
374 | or die "gunzip failed: $GunzipError\n"; |
375 | |
1a6a8453 |
376 | |
642e522c |
377 | |
378 | The functional interface needs Perl5.005 or better. |
379 | |
380 | |
381 | =head2 gunzip $input => $output [, OPTS] |
382 | |
1a6a8453 |
383 | |
384 | C<gunzip> expects at least two parameters, C<$input> and C<$output>. |
642e522c |
385 | |
386 | =head3 The C<$input> parameter |
387 | |
388 | The parameter, C<$input>, is used to define the source of |
389 | the compressed data. |
390 | |
391 | It can take one of the following forms: |
392 | |
393 | =over 5 |
394 | |
395 | =item A filename |
396 | |
397 | If the C<$input> parameter is a simple scalar, it is assumed to be a |
398 | filename. This file will be opened for reading and the input data |
399 | will be read from it. |
400 | |
401 | =item A filehandle |
402 | |
403 | If the C<$input> parameter is a filehandle, the input data will be |
404 | read from it. |
405 | The string '-' can be used as an alias for standard input. |
406 | |
407 | =item A scalar reference |
408 | |
409 | If C<$input> is a scalar reference, the input data will be read |
410 | from C<$$input>. |
411 | |
412 | =item An array reference |
413 | |
1a6a8453 |
414 | If C<$input> is an array reference, each element in the array must be a |
415 | filename. |
416 | |
417 | The input data will be read from each file in turn. |
418 | |
642e522c |
419 | The complete array will be walked to ensure that it only |
1a6a8453 |
420 | contains valid filenames before any data is uncompressed. |
421 | |
422 | |
642e522c |
423 | |
424 | =item An Input FileGlob string |
425 | |
426 | If C<$input> is a string that is delimited by the characters "<" and ">" |
427 | C<gunzip> will assume that it is an I<input fileglob string>. The |
428 | input is the list of files that match the fileglob. |
429 | |
430 | If the fileglob does not match any files ... |
431 | |
432 | See L<File::GlobMapper|File::GlobMapper> for more details. |
433 | |
434 | |
435 | =back |
436 | |
437 | If the C<$input> parameter is any other type, C<undef> will be returned. |
438 | |
439 | |
440 | |
441 | =head3 The C<$output> parameter |
442 | |
443 | The parameter C<$output> is used to control the destination of the |
444 | uncompressed data. This parameter can take one of these forms. |
445 | |
446 | =over 5 |
447 | |
448 | =item A filename |
449 | |
1a6a8453 |
450 | If the C<$output> parameter is a simple scalar, it is assumed to be a |
451 | filename. This file will be opened for writing and the uncompressed |
452 | data will be written to it. |
642e522c |
453 | |
454 | =item A filehandle |
455 | |
1a6a8453 |
456 | If the C<$output> parameter is a filehandle, the uncompressed data |
457 | will be written to it. |
642e522c |
458 | The string '-' can be used as an alias for standard output. |
459 | |
460 | |
461 | =item A scalar reference |
462 | |
1a6a8453 |
463 | If C<$output> is a scalar reference, the uncompressed data will be |
464 | stored in C<$$output>. |
642e522c |
465 | |
466 | |
642e522c |
467 | |
468 | =item An Array Reference |
469 | |
1a6a8453 |
470 | If C<$output> is an array reference, the uncompressed data will be |
471 | pushed onto the array. |
642e522c |
472 | |
473 | =item An Output FileGlob |
474 | |
475 | If C<$output> is a string that is delimited by the characters "<" and ">" |
476 | C<gunzip> will assume that it is an I<output fileglob string>. The |
477 | output is the list of files that match the fileglob. |
478 | |
479 | When C<$output> is an fileglob string, C<$input> must also be a fileglob |
480 | string. Anything else is an error. |
481 | |
482 | =back |
483 | |
484 | If the C<$output> parameter is any other type, C<undef> will be returned. |
485 | |
642e522c |
486 | |
642e522c |
487 | |
488 | =head2 Notes |
489 | |
c70c1701 |
490 | |
491 | When C<$input> maps to multiple compressed files/buffers and C<$output> is |
492 | a single file/buffer, after uncompression C<$output> will contain a |
493 | concatenation of all the uncompressed data from each of the input |
494 | files/buffers. |
495 | |
496 | |
642e522c |
497 | |
498 | |
499 | |
500 | =head2 Optional Parameters |
501 | |
502 | Unless specified below, the optional parameters for C<gunzip>, |
503 | C<OPTS>, are the same as those used with the OO interface defined in the |
504 | L</"Constructor Options"> section below. |
505 | |
506 | =over 5 |
507 | |
508 | =item AutoClose =E<gt> 0|1 |
509 | |
1a6a8453 |
510 | This option applies to any input or output data streams to |
511 | C<gunzip> that are filehandles. |
642e522c |
512 | |
513 | If C<AutoClose> is specified, and the value is true, it will result in all |
514 | input and/or output filehandles being closed once C<gunzip> has |
515 | completed. |
516 | |
517 | This parameter defaults to 0. |
518 | |
519 | |
520 | |
1a6a8453 |
521 | =item BinModeOut =E<gt> 0|1 |
522 | |
523 | When writing to a file or filehandle, set C<binmode> before writing to the |
524 | file. |
525 | |
526 | Defaults to 0. |
527 | |
528 | |
529 | |
530 | |
531 | |
642e522c |
532 | =item -Append =E<gt> 0|1 |
533 | |
534 | TODO |
535 | |
1a6a8453 |
536 | =item -MultiStream =E<gt> 0|1 |
537 | |
538 | Creates a new stream after each file. |
539 | |
540 | Defaults to 1. |
541 | |
642e522c |
542 | |
543 | |
544 | =back |
545 | |
546 | |
547 | |
548 | |
549 | =head2 Examples |
550 | |
551 | To read the contents of the file C<file1.txt.gz> and write the |
552 | compressed data to the file C<file1.txt>. |
553 | |
554 | use strict ; |
555 | use warnings ; |
556 | use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; |
557 | |
558 | my $input = "file1.txt.gz"; |
559 | my $output = "file1.txt"; |
560 | gunzip $input => $output |
561 | or die "gunzip failed: $GunzipError\n"; |
562 | |
563 | |
564 | To read from an existing Perl filehandle, C<$input>, and write the |
565 | uncompressed data to a buffer, C<$buffer>. |
566 | |
567 | use strict ; |
568 | use warnings ; |
569 | use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; |
570 | use IO::File ; |
571 | |
572 | my $input = new IO::File "<file1.txt.gz" |
573 | or die "Cannot open 'file1.txt.gz': $!\n" ; |
574 | my $buffer ; |
575 | gunzip $input => \$buffer |
576 | or die "gunzip failed: $GunzipError\n"; |
577 | |
578 | To uncompress all files in the directory "/my/home" that match "*.txt.gz" and store the compressed data in the same directory |
579 | |
580 | use strict ; |
581 | use warnings ; |
582 | use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; |
583 | |
584 | gunzip '</my/home/*.txt.gz>' => '</my/home/#1.txt>' |
585 | or die "gunzip failed: $GunzipError\n"; |
586 | |
587 | and if you want to compress each file one at a time, this will do the trick |
588 | |
589 | use strict ; |
590 | use warnings ; |
591 | use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; |
592 | |
593 | for my $input ( glob "/my/home/*.txt.gz" ) |
594 | { |
595 | my $output = $input; |
596 | $output =~ s/.gz// ; |
597 | gunzip $input => $output |
598 | or die "Error compressing '$input': $GunzipError\n"; |
599 | } |
600 | |
601 | =head1 OO Interface |
602 | |
603 | =head2 Constructor |
604 | |
605 | The format of the constructor for IO::Uncompress::Gunzip is shown below |
606 | |
607 | |
608 | my $z = new IO::Uncompress::Gunzip $input [OPTS] |
609 | or die "IO::Uncompress::Gunzip failed: $GunzipError\n"; |
610 | |
611 | Returns an C<IO::Uncompress::Gunzip> object on success and undef on failure. |
612 | The variable C<$GunzipError> will contain an error message on failure. |
613 | |
1a6a8453 |
614 | If you are running Perl 5.005 or better the object, C<$z>, returned from |
615 | IO::Uncompress::Gunzip can be used exactly like an L<IO::File|IO::File> filehandle. |
616 | This means that all normal input file operations can be carried out with |
617 | C<$z>. For example, to read a line from a compressed file/buffer you can |
618 | use either of these forms |
642e522c |
619 | |
620 | $line = $z->getline(); |
621 | $line = <$z>; |
622 | |
623 | The mandatory parameter C<$input> is used to determine the source of the |
624 | compressed data. This parameter can take one of three forms. |
625 | |
626 | =over 5 |
627 | |
628 | =item A filename |
629 | |
630 | If the C<$input> parameter is a scalar, it is assumed to be a filename. This |
631 | file will be opened for reading and the compressed data will be read from it. |
632 | |
633 | =item A filehandle |
634 | |
635 | If the C<$input> parameter is a filehandle, the compressed data will be |
636 | read from it. |
637 | The string '-' can be used as an alias for standard input. |
638 | |
639 | |
640 | =item A scalar reference |
641 | |
642 | If C<$input> is a scalar reference, the compressed data will be read from |
643 | C<$$output>. |
644 | |
645 | =back |
646 | |
647 | =head2 Constructor Options |
648 | |
649 | |
650 | The option names defined below are case insensitive and can be optionally |
651 | prefixed by a '-'. So all of the following are valid |
652 | |
653 | -AutoClose |
654 | -autoclose |
655 | AUTOCLOSE |
656 | autoclose |
657 | |
658 | OPTS is a combination of the following options: |
659 | |
660 | =over 5 |
661 | |
662 | =item -AutoClose =E<gt> 0|1 |
663 | |
664 | This option is only valid when the C<$input> parameter is a filehandle. If |
665 | specified, and the value is true, it will result in the file being closed once |
666 | either the C<close> method is called or the IO::Uncompress::Gunzip object is |
667 | destroyed. |
668 | |
669 | This parameter defaults to 0. |
670 | |
671 | =item -MultiStream =E<gt> 0|1 |
672 | |
673 | |
674 | |
675 | Allows multiple concatenated compressed streams to be treated as a single |
676 | compressed stream. Decompression will stop once either the end of the |
677 | file/buffer is reached, an error is encountered (premature eof, corrupt |
678 | compressed data) or the end of a stream is not immediately followed by the |
679 | start of another stream. |
680 | |
681 | This parameter defaults to 0. |
682 | |
683 | |
684 | |
685 | =item -Prime =E<gt> $string |
686 | |
687 | This option will uncompress the contents of C<$string> before processing the |
688 | input file/buffer. |
689 | |
690 | This option can be useful when the compressed data is embedded in another |
691 | file/data structure and it is not possible to work out where the compressed |
1a6a8453 |
692 | data begins without having to read the first few bytes. If this is the |
693 | case, the uncompression can be I<primed> with these bytes using this |
694 | option. |
642e522c |
695 | |
696 | =item -Transparent =E<gt> 0|1 |
697 | |
698 | If this option is set and the input file or buffer is not compressed data, |
699 | the module will allow reading of it anyway. |
700 | |
701 | This option defaults to 1. |
702 | |
703 | =item -BlockSize =E<gt> $num |
704 | |
1a6a8453 |
705 | When reading the compressed input data, IO::Uncompress::Gunzip will read it in |
706 | blocks of C<$num> bytes. |
642e522c |
707 | |
708 | This option defaults to 4096. |
709 | |
710 | =item -InputLength =E<gt> $size |
711 | |
1a6a8453 |
712 | When present this option will limit the number of compressed bytes read |
713 | from the input file/buffer to C<$size>. This option can be used in the |
714 | situation where there is useful data directly after the compressed data |
715 | stream and you know beforehand the exact length of the compressed data |
716 | stream. |
642e522c |
717 | |
1a6a8453 |
718 | This option is mostly used when reading from a filehandle, in which case |
719 | the file pointer will be left pointing to the first byte directly after the |
642e522c |
720 | compressed data stream. |
721 | |
722 | |
723 | |
724 | This option defaults to off. |
725 | |
726 | =item -Append =E<gt> 0|1 |
727 | |
728 | This option controls what the C<read> method does with uncompressed data. |
729 | |
1a6a8453 |
730 | If set to 1, all uncompressed data will be appended to the output parameter |
731 | of the C<read> method. |
642e522c |
732 | |
1a6a8453 |
733 | If set to 0, the contents of the output parameter of the C<read> method |
734 | will be overwritten by the uncompressed data. |
642e522c |
735 | |
736 | Defaults to 0. |
737 | |
738 | =item -Strict =E<gt> 0|1 |
739 | |
740 | |
741 | |
742 | This option controls whether the extra checks defined below are used when |
1a6a8453 |
743 | carrying out the decompression. When Strict is on, the extra tests are |
744 | carried out, when Strict is off they are not. |
642e522c |
745 | |
746 | The default for this option is off. |
747 | |
748 | |
749 | |
750 | |
751 | |
752 | |
753 | |
754 | |
755 | |
756 | =over 5 |
757 | |
758 | =item 1 |
759 | |
760 | If the FHCRC bit is set in the gzip FLG header byte, the CRC16 bytes in the |
761 | header must match the crc16 value of the gzip header actually read. |
762 | |
763 | =item 2 |
764 | |
765 | If the gzip header contains a name field (FNAME) it consists solely of ISO |
766 | 8859-1 characters. |
767 | |
768 | =item 3 |
769 | |
1a6a8453 |
770 | If the gzip header contains a comment field (FCOMMENT) it consists solely |
771 | of ISO 8859-1 characters plus line-feed. |
642e522c |
772 | |
773 | =item 4 |
774 | |
775 | If the gzip FEXTRA header field is present it must conform to the sub-field |
a02d0f6f |
776 | structure as defined in RFC 1952. |
642e522c |
777 | |
778 | =item 5 |
779 | |
780 | The CRC32 and ISIZE trailer fields must be present. |
781 | |
782 | =item 6 |
783 | |
784 | The value of the CRC32 field read must match the crc32 value of the |
785 | uncompressed data actually contained in the gzip file. |
786 | |
787 | =item 7 |
788 | |
1a6a8453 |
789 | The value of the ISIZE fields read must match the length of the |
790 | uncompressed data actually read from the file. |
642e522c |
791 | |
792 | =back |
793 | |
794 | |
795 | |
796 | |
797 | |
798 | |
799 | =item -ParseExtra =E<gt> 0|1 |
800 | |
801 | If the gzip FEXTRA header field is present and this option is set, it will |
802 | force the module to check that it conforms to the sub-field structure as |
a02d0f6f |
803 | defined in RFC 1952. |
642e522c |
804 | |
805 | If the C<Strict> is on it will automatically enable this option. |
806 | |
807 | Defaults to 0. |
808 | |
809 | |
810 | |
a02d0f6f |
811 | |
812 | |
642e522c |
813 | =back |
814 | |
815 | =head2 Examples |
816 | |
817 | TODO |
818 | |
819 | =head1 Methods |
820 | |
821 | =head2 read |
822 | |
823 | Usage is |
824 | |
825 | $status = $z->read($buffer) |
826 | |
827 | Reads a block of compressed data (the size the the compressed block is |
828 | determined by the C<Buffer> option in the constructor), uncompresses it and |
1a6a8453 |
829 | writes any uncompressed data into C<$buffer>. If the C<Append> parameter is |
830 | set in the constructor, the uncompressed data will be appended to the |
831 | C<$buffer> parameter. Otherwise C<$buffer> will be overwritten. |
642e522c |
832 | |
1a6a8453 |
833 | Returns the number of uncompressed bytes written to C<$buffer>, zero if eof |
834 | or a negative number on error. |
642e522c |
835 | |
836 | =head2 read |
837 | |
838 | Usage is |
839 | |
840 | $status = $z->read($buffer, $length) |
841 | $status = $z->read($buffer, $length, $offset) |
842 | |
843 | $status = read($z, $buffer, $length) |
844 | $status = read($z, $buffer, $length, $offset) |
845 | |
846 | Attempt to read C<$length> bytes of uncompressed data into C<$buffer>. |
847 | |
1a6a8453 |
848 | The main difference between this form of the C<read> method and the |
849 | previous one, is that this one will attempt to return I<exactly> C<$length> |
850 | bytes. The only circumstances that this function will not is if end-of-file |
851 | or an IO error is encountered. |
642e522c |
852 | |
1a6a8453 |
853 | Returns the number of uncompressed bytes written to C<$buffer>, zero if eof |
854 | or a negative number on error. |
642e522c |
855 | |
856 | |
857 | =head2 getline |
858 | |
859 | Usage is |
860 | |
861 | $line = $z->getline() |
862 | $line = <$z> |
863 | |
864 | Reads a single line. |
865 | |
866 | This method fully supports the use of of the variable C<$/> |
867 | (or C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to |
868 | determine what constitutes an end of line. Both paragraph mode and file |
869 | slurp mode are supported. |
870 | |
871 | |
872 | =head2 getc |
873 | |
874 | Usage is |
875 | |
876 | $char = $z->getc() |
877 | |
878 | Read a single character. |
879 | |
880 | =head2 ungetc |
881 | |
882 | Usage is |
883 | |
884 | $char = $z->ungetc($string) |
885 | |
886 | |
a02d0f6f |
887 | |
642e522c |
888 | =head2 inflateSync |
889 | |
890 | Usage is |
891 | |
892 | $status = $z->inflateSync() |
893 | |
894 | TODO |
895 | |
a02d0f6f |
896 | |
642e522c |
897 | =head2 getHeaderInfo |
898 | |
899 | Usage is |
900 | |
1a6a8453 |
901 | $hdr = $z->getHeaderInfo(); |
902 | @hdrs = $z->getHeaderInfo(); |
642e522c |
903 | |
1a6a8453 |
904 | This method returns either a hash reference (in scalar context) or a list |
905 | or hash references (in array context) that contains information about each |
906 | of the header fields in the compressed data stream(s). |
642e522c |
907 | |
908 | |
909 | |
1a6a8453 |
910 | =over 5 |
642e522c |
911 | |
1a6a8453 |
912 | =item Name |
642e522c |
913 | |
1a6a8453 |
914 | The contents of the Name header field, if present. If no name is |
915 | present, the value will be undef. Note this is different from a zero length |
916 | name, which will return an empty string. |
642e522c |
917 | |
918 | =item Comment |
919 | |
1a6a8453 |
920 | The contents of the Comment header field, if present. If no comment is |
921 | present, the value will be undef. Note this is different from a zero length |
922 | comment, which will return an empty string. |
642e522c |
923 | |
924 | =back |
925 | |
926 | |
927 | |
928 | |
929 | =head2 tell |
930 | |
931 | Usage is |
932 | |
933 | $z->tell() |
934 | tell $z |
935 | |
936 | Returns the uncompressed file offset. |
937 | |
938 | =head2 eof |
939 | |
940 | Usage is |
941 | |
942 | $z->eof(); |
943 | eof($z); |
944 | |
945 | |
946 | |
947 | Returns true if the end of the compressed input stream has been reached. |
948 | |
949 | |
950 | |
951 | =head2 seek |
952 | |
953 | $z->seek($position, $whence); |
954 | seek($z, $position, $whence); |
955 | |
956 | |
957 | |
958 | |
959 | Provides a sub-set of the C<seek> functionality, with the restriction |
960 | that it is only legal to seek forward in the input file/buffer. |
961 | It is a fatal error to attempt to seek backward. |
962 | |
963 | |
964 | |
965 | The C<$whence> parameter takes one the usual values, namely SEEK_SET, |
966 | SEEK_CUR or SEEK_END. |
967 | |
968 | Returns 1 on success, 0 on failure. |
969 | |
970 | =head2 binmode |
971 | |
972 | Usage is |
973 | |
974 | $z->binmode |
975 | binmode $z ; |
976 | |
977 | This is a noop provided for completeness. |
978 | |
a02d0f6f |
979 | =head2 opened |
980 | |
981 | $z->opened() |
982 | |
983 | Returns true if the object currently refers to a opened file/buffer. |
984 | |
985 | =head2 autoflush |
986 | |
987 | my $prev = $z->autoflush() |
988 | my $prev = $z->autoflush(EXPR) |
989 | |
990 | If the C<$z> object is associated with a file or a filehandle, this method |
991 | returns the current autoflush setting for the underlying filehandle. If |
992 | C<EXPR> is present, and is non-zero, it will enable flushing after every |
993 | write/print operation. |
994 | |
995 | If C<$z> is associated with a buffer, this method has no effect and always |
996 | returns C<undef>. |
997 | |
998 | B<Note> that the special variable C<$|> B<cannot> be used to set or |
999 | retrieve the autoflush setting. |
1000 | |
1001 | =head2 input_line_number |
1002 | |
1003 | $z->input_line_number() |
1004 | $z->input_line_number(EXPR) |
1005 | |
1006 | |
1007 | |
1008 | Returns the current uncompressed line number. If C<EXPR> is present it has |
1009 | the effect of setting the line number. Note that setting the line number |
1010 | does not change the current position within the file/buffer being read. |
1011 | |
1012 | The contents of C<$/> are used to to determine what constitutes a line |
1013 | terminator. |
1014 | |
1015 | |
1016 | |
642e522c |
1017 | =head2 fileno |
1018 | |
1019 | $z->fileno() |
1020 | fileno($z) |
1021 | |
a02d0f6f |
1022 | If the C<$z> object is associated with a file or a filehandle, this method |
1023 | will return the underlying file descriptor. |
642e522c |
1024 | |
1025 | If the C<$z> object is is associated with a buffer, this method will |
1026 | return undef. |
1027 | |
1028 | =head2 close |
1029 | |
1030 | $z->close() ; |
1031 | close $z ; |
1032 | |
1033 | |
1034 | |
1035 | Closes the output file/buffer. |
1036 | |
1037 | |
1038 | |
1039 | For most versions of Perl this method will be automatically invoked if |
1040 | the IO::Uncompress::Gunzip object is destroyed (either explicitly or by the |
1041 | variable with the reference to the object going out of scope). The |
1042 | exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In |
1043 | these cases, the C<close> method will be called automatically, but |
1044 | not until global destruction of all live objects when the program is |
1045 | terminating. |
1046 | |
1047 | Therefore, if you want your scripts to be able to run on all versions |
1048 | of Perl, you should call C<close> explicitly and not rely on automatic |
1049 | closing. |
1050 | |
1051 | Returns true on success, otherwise 0. |
1052 | |
1053 | If the C<AutoClose> option has been enabled when the IO::Uncompress::Gunzip |
1054 | object was created, and the object is associated with a file, the |
1055 | underlying file will also be closed. |
1056 | |
1057 | |
1058 | |
1059 | |
1060 | =head1 Importing |
1061 | |
1062 | No symbolic constants are required by this IO::Uncompress::Gunzip at present. |
1063 | |
1064 | =over 5 |
1065 | |
1066 | =item :all |
1067 | |
1068 | Imports C<gunzip> and C<$GunzipError>. |
1069 | Same as doing this |
1070 | |
1071 | use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; |
1072 | |
1073 | =back |
1074 | |
1075 | =head1 EXAMPLES |
1076 | |
1077 | |
1078 | |
1079 | |
1080 | =head1 SEE ALSO |
1081 | |
a02d0f6f |
1082 | L<Compress::Zlib>, L<IO::Compress::Gzip>, 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::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress> |
642e522c |
1083 | |
1084 | L<Compress::Zlib::FAQ|Compress::Zlib::FAQ> |
1085 | |
a02d0f6f |
1086 | L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>, |
1087 | L<Archive::Tar|Archive::Tar>, |
642e522c |
1088 | L<IO::Zlib|IO::Zlib> |
1089 | |
a02d0f6f |
1090 | |
642e522c |
1091 | For RFC 1950, 1951 and 1952 see |
1092 | F<http://www.faqs.org/rfcs/rfc1950.html>, |
1093 | F<http://www.faqs.org/rfcs/rfc1951.html> and |
1094 | F<http://www.faqs.org/rfcs/rfc1952.html> |
1095 | |
a02d0f6f |
1096 | The I<zlib> compression library was written by Jean-loup Gailly |
1097 | F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>. |
1098 | |
1099 | The primary site for the I<zlib> compression library is |
1100 | F<http://www.zlib.org>. |
1101 | |
1102 | The primary site for gzip is F<http://www.gzip.org>. |
1103 | |
1104 | |
1105 | |
1106 | |
642e522c |
1107 | =head1 AUTHOR |
1108 | |
cb7abd7f |
1109 | This module was written by Paul Marquess, F<pmqs@cpan.org>. |
642e522c |
1110 | |
642e522c |
1111 | |
642e522c |
1112 | |
1113 | =head1 MODIFICATION HISTORY |
1114 | |
1115 | See the Changes file. |
1116 | |
1117 | =head1 COPYRIGHT AND LICENSE |
642e522c |
1118 | |
1a6a8453 |
1119 | Copyright (c) 2005-2006 Paul Marquess. All rights reserved. |
a02d0f6f |
1120 | |
642e522c |
1121 | This program is free software; you can redistribute it and/or |
1122 | modify it under the same terms as Perl itself. |
1123 | |