File Coverage

blib/lib/IO/Uncompress/Gunzip.pm
Criterion Covered Total %
statement 106 115 92.1
branch 63 72 87.5
condition 14 15 93.3
subroutine 17 18 94.4
pod 2 8 25.0
total 202 228 88.6


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