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