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