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