1IO::Uncompress::Gunzip(U3s)er Contributed Perl DocumentatIiOo:n:Uncompress::Gunzip(3)
2
3
4
6 IO::Uncompress::Gunzip - Read RFC 1952 files/buffers
7
9 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;
10
11 my $status = gunzip $input => $output [,OPTS]
12 or die "gunzip failed: $GunzipError\n";
13
14 my $z = IO::Uncompress::Gunzip->new( $input [OPTS] )
15 or die "gunzip failed: $GunzipError\n";
16
17 $status = $z->read($buffer)
18 $status = $z->read($buffer, $length)
19 $status = $z->read($buffer, $length, $offset)
20 $line = $z->getline()
21 $char = $z->getc()
22 $char = $z->ungetc()
23 $char = $z->opened()
24
25 $status = $z->inflateSync()
26
27 $data = $z->trailingData()
28 $status = $z->nextStream()
29 $data = $z->getHeaderInfo()
30 $z->tell()
31 $z->seek($position, $whence)
32 $z->binmode()
33 $z->fileno()
34 $z->eof()
35 $z->close()
36
37 $GunzipError ;
38
39 # IO::File mode
40
41 <$z>
42 read($z, $buffer);
43 read($z, $buffer, $length);
44 read($z, $buffer, $length, $offset);
45 tell($z)
46 seek($z, $position, $whence)
47 binmode($z)
48 fileno($z)
49 eof($z)
50 close($z)
51
53 This module provides a Perl interface that allows the reading of
54 files/buffers that conform to RFC 1952.
55
56 For writing RFC 1952 files/buffers, see the companion module
57 IO::Compress::Gzip.
58
60 A top-level function, "gunzip", is provided to carry out "one-shot"
61 uncompression between buffers and/or files. For finer control over the
62 uncompression process, see the "OO Interface" section.
63
64 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;
65
66 gunzip $input_filename_or_reference => $output_filename_or_reference [,OPTS]
67 or die "gunzip failed: $GunzipError\n";
68
69 The functional interface needs Perl5.005 or better.
70
71 gunzip $input_filename_or_reference => $output_filename_or_reference [,
72 OPTS]
73 "gunzip" expects at least two parameters, $input_filename_or_reference
74 and $output_filename_or_reference and zero or more optional parameters
75 (see "Optional Parameters")
76
77 The $input_filename_or_reference parameter
78
79 The parameter, $input_filename_or_reference, is used to define the
80 source of the compressed data.
81
82 It can take one of the following forms:
83
84 A filename
85 If the $input_filename_or_reference parameter is a simple scalar,
86 it is assumed to be a filename. This file will be opened for
87 reading and the input data will be read from it.
88
89 A filehandle
90 If the $input_filename_or_reference parameter is a filehandle, the
91 input data will be read from it. The string '-' can be used as an
92 alias for standard input.
93
94 A scalar reference
95 If $input_filename_or_reference is a scalar reference, the input
96 data will be read from $$input_filename_or_reference.
97
98 An array reference
99 If $input_filename_or_reference is an array reference, each
100 element in the array must be a filename.
101
102 The input data will be read from each file in turn.
103
104 The complete array will be walked to ensure that it only contains
105 valid filenames before any data is uncompressed.
106
107 An Input FileGlob string
108 If $input_filename_or_reference is a string that is delimited by
109 the characters "<" and ">" "gunzip" will assume that it is an
110 input fileglob string. The input is the list of files that match
111 the fileglob.
112
113 See File::GlobMapper for more details.
114
115 If the $input_filename_or_reference parameter is any other type,
116 "undef" will be returned.
117
118 The $output_filename_or_reference parameter
119
120 The parameter $output_filename_or_reference is used to control the
121 destination of the uncompressed data. This parameter can take one of
122 these forms.
123
124 A filename
125 If the $output_filename_or_reference parameter is a simple scalar,
126 it is assumed to be a filename. This file will be opened for
127 writing and the uncompressed data will be written to it.
128
129 A filehandle
130 If the $output_filename_or_reference parameter is a filehandle,
131 the uncompressed data will be written to it. The string '-' can
132 be used as an alias for standard output.
133
134 A scalar reference
135 If $output_filename_or_reference is a scalar reference, the
136 uncompressed data will be stored in
137 $$output_filename_or_reference.
138
139 An Array Reference
140 If $output_filename_or_reference is an array reference, the
141 uncompressed data will be pushed onto the array.
142
143 An Output FileGlob
144 If $output_filename_or_reference is a string that is delimited by
145 the characters "<" and ">" "gunzip" will assume that it is an
146 output fileglob string. The output is the list of files that match
147 the fileglob.
148
149 When $output_filename_or_reference is an fileglob string,
150 $input_filename_or_reference must also be a fileglob string.
151 Anything else is an error.
152
153 See File::GlobMapper for more details.
154
155 If the $output_filename_or_reference parameter is any other type,
156 "undef" will be returned.
157
158 Notes
159 When $input_filename_or_reference maps to multiple compressed
160 files/buffers and $output_filename_or_reference is a single
161 file/buffer, after uncompression $output_filename_or_reference will
162 contain a concatenation of all the uncompressed data from each of the
163 input files/buffers.
164
165 Optional Parameters
166 The optional parameters for the one-shot function "gunzip" are (for the
167 most part) identical to those used with the OO interface defined in the
168 "Constructor Options" section. The exceptions are listed below
169
170 "AutoClose => 0|1"
171 This option applies to any input or output data streams to
172 "gunzip" that are filehandles.
173
174 If "AutoClose" is specified, and the value is true, it will result
175 in all input and/or output filehandles being closed once "gunzip"
176 has completed.
177
178 This parameter defaults to 0.
179
180 "BinModeOut => 0|1"
181 This option is now a no-op. All files will be written in binmode.
182
183 "Append => 0|1"
184 The behaviour of this option is dependent on the type of output
185 data stream.
186
187 • A Buffer
188
189 If "Append" is enabled, all uncompressed data will be append
190 to the end of the output buffer. Otherwise the output buffer
191 will be cleared before any uncompressed data is written to
192 it.
193
194 • A Filename
195
196 If "Append" is enabled, the file will be opened in append
197 mode. Otherwise the contents of the file, if any, will be
198 truncated before any uncompressed data is written to it.
199
200 • A Filehandle
201
202 If "Append" is enabled, the filehandle will be positioned to
203 the end of the file via a call to "seek" before any
204 uncompressed data is written to it. Otherwise the file
205 pointer will not be moved.
206
207 When "Append" is specified, and set to true, it will append all
208 uncompressed data to the output data stream.
209
210 So when the output is a filehandle it will carry out a seek to the
211 eof before writing any uncompressed data. If the output is a
212 filename, it will be opened for appending. If the output is a
213 buffer, all uncompressed data will be appended to the existing
214 buffer.
215
216 Conversely when "Append" is not specified, or it is present and is
217 set to false, it will operate as follows.
218
219 When the output is a filename, it will truncate the contents of
220 the file before writing any uncompressed data. If the output is a
221 filehandle its position will not be changed. If the output is a
222 buffer, it will be wiped before any uncompressed data is output.
223
224 Defaults to 0.
225
226 "MultiStream => 0|1"
227 If the input file/buffer contains multiple compressed data
228 streams, this option will uncompress the whole lot as a single
229 data stream.
230
231 Defaults to 0.
232
233 "TrailingData => $scalar"
234 Returns the data, if any, that is present immediately after the
235 compressed data stream once uncompression is complete.
236
237 This option can be used when there is useful information
238 immediately following the compressed data stream, and you don't
239 know the length of the compressed data stream.
240
241 If the input is a buffer, "trailingData" will return everything
242 from the end of the compressed data stream to the end of the
243 buffer.
244
245 If the input is a filehandle, "trailingData" will return the data
246 that is left in the filehandle input buffer once the end of the
247 compressed data stream has been reached. You can then use the
248 filehandle to read the rest of the input file.
249
250 Don't bother using "trailingData" if the input is a filename.
251
252 If you know the length of the compressed data stream before you
253 start uncompressing, you can avoid having to use "trailingData" by
254 setting the "InputLength" option.
255
256 OneShot Examples
257 To read the contents of the file "file1.txt.gz" and write the
258 uncompressed data to the file "file1.txt".
259
260 use strict ;
261 use warnings ;
262 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;
263
264 my $input = "file1.txt.gz";
265 my $output = "file1.txt";
266 gunzip $input => $output
267 or die "gunzip failed: $GunzipError\n";
268
269 To read from an existing Perl filehandle, $input, and write the
270 uncompressed data to a buffer, $buffer.
271
272 use strict ;
273 use warnings ;
274 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;
275 use IO::File ;
276
277 my $input = IO::File->new( "<file1.txt.gz" )
278 or die "Cannot open 'file1.txt.gz': $!\n" ;
279 my $buffer ;
280 gunzip $input => \$buffer
281 or die "gunzip failed: $GunzipError\n";
282
283 To uncompress all files in the directory "/my/home" that match
284 "*.txt.gz" and store the compressed data in the same directory
285
286 use strict ;
287 use warnings ;
288 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;
289
290 gunzip '</my/home/*.txt.gz>' => '</my/home/#1.txt>'
291 or die "gunzip failed: $GunzipError\n";
292
293 and if you want to compress each file one at a time, this will do the
294 trick
295
296 use strict ;
297 use warnings ;
298 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;
299
300 for my $input ( glob "/my/home/*.txt.gz" )
301 {
302 my $output = $input;
303 $output =~ s/.gz// ;
304 gunzip $input => $output
305 or die "Error compressing '$input': $GunzipError\n";
306 }
307
309 Constructor
310 The format of the constructor for IO::Uncompress::Gunzip is shown below
311
312 my $z = IO::Uncompress::Gunzip->new( $input [OPTS] )
313 or die "IO::Uncompress::Gunzip failed: $GunzipError\n";
314
315 The constructor takes one mandatory parameter, $input, defined below,
316 and zero or more "OPTS", defined in "Constructor Options".
317
318 Returns an "IO::Uncompress::Gunzip" object on success and undef on
319 failure. The variable $GunzipError will contain an error message on
320 failure.
321
322 If you are running Perl 5.005 or better the object, $z, returned from
323 IO::Uncompress::Gunzip can be used exactly like an IO::File filehandle.
324 This means that all normal input file operations can be carried out
325 with $z. For example, to read a line from a compressed file/buffer you
326 can use either of these forms
327
328 $line = $z->getline();
329 $line = <$z>;
330
331 Below is a simple exaple of using the OO interface to read the
332 compressed file "myfile.gz" and write its contents to stdout.
333
334 my $filename = "myfile.gz";
335 my $z = IO::Uncompress::Gunzip->new($filename)
336 or die "IO::Uncompress::Gunzip failed: $GunzipError\n";
337
338 while (<$z>) {
339 print $_;
340 }
341 $z->close();
342
343 See "EXAMPLES" for further examples
344
345 The mandatory parameter $input is used to determine the source of the
346 compressed data. This parameter can take one of three forms.
347
348 A filename
349 If the $input parameter is a scalar, it is assumed to be a
350 filename. This file will be opened for reading and the compressed
351 data will be read from it.
352
353 A filehandle
354 If the $input parameter is a filehandle, the compressed data will
355 be read from it. The string '-' can be used as an alias for
356 standard input.
357
358 A scalar reference
359 If $input is a scalar reference, the compressed data will be read
360 from $$input.
361
362 Constructor Options
363 The option names defined below are case insensitive and can be
364 optionally prefixed by a '-'. So all of the following are valid
365
366 -AutoClose
367 -autoclose
368 AUTOCLOSE
369 autoclose
370
371 OPTS is a combination of the following options:
372
373 "AutoClose => 0|1"
374 This option is only valid when the $input parameter is a
375 filehandle. If specified, and the value is true, it will result in
376 the file being closed once either the "close" method is called or
377 the IO::Uncompress::Gunzip object is destroyed.
378
379 This parameter defaults to 0.
380
381 "MultiStream => 0|1"
382 Allows multiple concatenated compressed streams to be treated as a
383 single compressed stream. Decompression will stop once either the
384 end of the file/buffer is reached, an error is encountered
385 (premature eof, corrupt compressed data) or the end of a stream is
386 not immediately followed by the start of another stream.
387
388 This parameter defaults to 0.
389
390 "Prime => $string"
391 This option will uncompress the contents of $string before
392 processing the input file/buffer.
393
394 This option can be useful when the compressed data is embedded in
395 another file/data structure and it is not possible to work out
396 where the compressed data begins without having to read the first
397 few bytes. If this is the case, the uncompression can be primed
398 with these bytes using this option.
399
400 "Transparent => 0|1"
401 If this option is set and the input file/buffer is not compressed
402 data, the module will allow reading of it anyway.
403
404 In addition, if the input file/buffer does contain compressed data
405 and there is non-compressed data immediately following it, setting
406 this option will make this module treat the whole file/buffer as a
407 single data stream.
408
409 This option defaults to 1.
410
411 "BlockSize => $num"
412 When reading the compressed input data, IO::Uncompress::Gunzip
413 will read it in blocks of $num bytes.
414
415 This option defaults to 4096.
416
417 "InputLength => $size"
418 When present this option will limit the number of compressed bytes
419 read from the input file/buffer to $size. This option can be used
420 in the situation where there is useful data directly after the
421 compressed data stream and you know beforehand the exact length of
422 the compressed data stream.
423
424 This option is mostly used when reading from a filehandle, in
425 which case the file pointer will be left pointing to the first
426 byte directly after the compressed data stream.
427
428 This option defaults to off.
429
430 "Append => 0|1"
431 This option controls what the "read" method does with uncompressed
432 data.
433
434 If set to 1, all uncompressed data will be appended to the output
435 parameter of the "read" method.
436
437 If set to 0, the contents of the output parameter of the "read"
438 method will be overwritten by the uncompressed data.
439
440 Defaults to 0.
441
442 "Strict => 0|1"
443 This option controls whether the extra checks defined below are
444 used when carrying out the decompression. When Strict is on, the
445 extra tests are carried out, when Strict is off they are not.
446
447 The default for this option is off.
448
449 1. If the FHCRC bit is set in the gzip FLG header byte, the
450 CRC16 bytes in the header must match the crc16 value of the
451 gzip header actually read.
452
453 2. If the gzip header contains a name field (FNAME) it consists
454 solely of ISO 8859-1 characters.
455
456 3. If the gzip header contains a comment field (FCOMMENT) it
457 consists solely of ISO 8859-1 characters plus line-feed.
458
459 4. If the gzip FEXTRA header field is present it must conform to
460 the sub-field structure as defined in RFC 1952.
461
462 5. The CRC32 and ISIZE trailer fields must be present.
463
464 6. The value of the CRC32 field read must match the crc32 value
465 of the uncompressed data actually contained in the gzip file.
466
467 7. The value of the ISIZE fields read must match the length of
468 the uncompressed data actually read from the file.
469
470 "ParseExtra => 0|1" If the gzip FEXTRA header field is present and this
471 option is set, it will force the module to check that it conforms to
472 the sub-field structure as defined in RFC 1952.
473 If the "Strict" is on it will automatically enable this option.
474
475 Defaults to 0.
476
478 read
479 Usage is
480
481 $status = $z->read($buffer)
482
483 Reads a block of compressed data (the size of the compressed block is
484 determined by the "Buffer" option in the constructor), uncompresses it
485 and writes any uncompressed data into $buffer. If the "Append"
486 parameter is set in the constructor, the uncompressed data will be
487 appended to the $buffer parameter. Otherwise $buffer will be
488 overwritten.
489
490 Returns the number of uncompressed bytes written to $buffer, zero if
491 eof or a negative number on error.
492
493 read
494 Usage is
495
496 $status = $z->read($buffer, $length)
497 $status = $z->read($buffer, $length, $offset)
498
499 $status = read($z, $buffer, $length)
500 $status = read($z, $buffer, $length, $offset)
501
502 Attempt to read $length bytes of uncompressed data into $buffer.
503
504 The main difference between this form of the "read" method and the
505 previous one, is that this one will attempt to return exactly $length
506 bytes. The only circumstances that this function will not is if end-of-
507 file or an IO error is encountered.
508
509 Returns the number of uncompressed bytes written to $buffer, zero if
510 eof or a negative number on error.
511
512 getline
513 Usage is
514
515 $line = $z->getline()
516 $line = <$z>
517
518 Reads a single line.
519
520 This method fully supports the use of the variable $/ (or
521 $INPUT_RECORD_SEPARATOR or $RS when "English" is in use) to determine
522 what constitutes an end of line. Paragraph mode, record mode and file
523 slurp mode are all supported.
524
525 getc
526 Usage is
527
528 $char = $z->getc()
529
530 Read a single character.
531
532 ungetc
533 Usage is
534
535 $char = $z->ungetc($string)
536
537 inflateSync
538 Usage is
539
540 $status = $z->inflateSync()
541
542 TODO
543
544 getHeaderInfo
545 Usage is
546
547 $hdr = $z->getHeaderInfo();
548 @hdrs = $z->getHeaderInfo();
549
550 This method returns either a hash reference (in scalar context) or a
551 list or hash references (in array context) that contains information
552 about each of the header fields in the compressed data stream(s).
553
554 Name The contents of the Name header field, if present. If no name is
555 present, the value will be undef. Note this is different from a
556 zero length name, which will return an empty string.
557
558 Comment
559 The contents of the Comment header field, if present. If no
560 comment is present, the value will be undef. Note this is
561 different from a zero length comment, which will return an empty
562 string.
563
564 tell
565 Usage is
566
567 $z->tell()
568 tell $z
569
570 Returns the uncompressed file offset.
571
572 eof
573 Usage is
574
575 $z->eof();
576 eof($z);
577
578 Returns true if the end of the compressed input stream has been
579 reached.
580
581 seek
582 $z->seek($position, $whence);
583 seek($z, $position, $whence);
584
585 Provides a sub-set of the "seek" functionality, with the restriction
586 that it is only legal to seek forward in the input file/buffer. It is
587 a fatal error to attempt to seek backward.
588
589 Note that the implementation of "seek" in this module does not provide
590 true random access to a compressed file/buffer. It works by
591 uncompressing data from the current offset in the file/buffer until it
592 reaches the uncompressed offset specified in the parameters to "seek".
593 For very small files this may be acceptable behaviour. For large files
594 it may cause an unacceptable delay.
595
596 The $whence parameter takes one the usual values, namely SEEK_SET,
597 SEEK_CUR or SEEK_END.
598
599 Returns 1 on success, 0 on failure.
600
601 binmode
602 Usage is
603
604 $z->binmode
605 binmode $z ;
606
607 This is a noop provided for completeness.
608
609 opened
610 $z->opened()
611
612 Returns true if the object currently refers to a opened file/buffer.
613
614 autoflush
615 my $prev = $z->autoflush()
616 my $prev = $z->autoflush(EXPR)
617
618 If the $z object is associated with a file or a filehandle, this method
619 returns the current autoflush setting for the underlying filehandle. If
620 "EXPR" is present, and is non-zero, it will enable flushing after every
621 write/print operation.
622
623 If $z is associated with a buffer, this method has no effect and always
624 returns "undef".
625
626 Note that the special variable $| cannot be used to set or retrieve the
627 autoflush setting.
628
629 input_line_number
630 $z->input_line_number()
631 $z->input_line_number(EXPR)
632
633 Returns the current uncompressed line number. If "EXPR" is present it
634 has the effect of setting the line number. Note that setting the line
635 number does not change the current position within the file/buffer
636 being read.
637
638 The contents of $/ are used to determine what constitutes a line
639 terminator.
640
641 fileno
642 $z->fileno()
643 fileno($z)
644
645 If the $z object is associated with a file or a filehandle, "fileno"
646 will return the underlying file descriptor. Once the "close" method is
647 called "fileno" will return "undef".
648
649 If the $z object is associated with a buffer, this method will return
650 "undef".
651
652 close
653 $z->close() ;
654 close $z ;
655
656 Closes the output file/buffer.
657
658 For most versions of Perl this method will be automatically invoked if
659 the IO::Uncompress::Gunzip object is destroyed (either explicitly or by
660 the variable with the reference to the object going out of scope). The
661 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In these
662 cases, the "close" method will be called automatically, but not until
663 global destruction of all live objects when the program is terminating.
664
665 Therefore, if you want your scripts to be able to run on all versions
666 of Perl, you should call "close" explicitly and not rely on automatic
667 closing.
668
669 Returns true on success, otherwise 0.
670
671 If the "AutoClose" option has been enabled when the
672 IO::Uncompress::Gunzip object was created, and the object is associated
673 with a file, the underlying file will also be closed.
674
675 nextStream
676 Usage is
677
678 my $status = $z->nextStream();
679
680 Skips to the next compressed data stream in the input file/buffer. If a
681 new compressed data stream is found, the eof marker will be cleared and
682 $. will be reset to 0.
683
684 Returns 1 if a new stream was found, 0 if none was found, and -1 if an
685 error was encountered.
686
687 trailingData
688 Usage is
689
690 my $data = $z->trailingData();
691
692 Returns the data, if any, that is present immediately after the
693 compressed data stream once uncompression is complete. It only makes
694 sense to call this method once the end of the compressed data stream
695 has been encountered.
696
697 This option can be used when there is useful information immediately
698 following the compressed data stream, and you don't know the length of
699 the compressed data stream.
700
701 If the input is a buffer, "trailingData" will return everything from
702 the end of the compressed data stream to the end of the buffer.
703
704 If the input is a filehandle, "trailingData" will return the data that
705 is left in the filehandle input buffer once the end of the compressed
706 data stream has been reached. You can then use the filehandle to read
707 the rest of the input file.
708
709 Don't bother using "trailingData" if the input is a filename.
710
711 If you know the length of the compressed data stream before you start
712 uncompressing, you can avoid having to use "trailingData" by setting
713 the "InputLength" option in the constructor.
714
716 No symbolic constants are required by IO::Uncompress::Gunzip at
717 present.
718
719 :all Imports "gunzip" and $GunzipError. Same as doing this
720
721 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;
722
724 Working with Net::FTP
725 See IO::Compress::FAQ
726
728 General feedback/questions/bug reports should be sent to
729 <https://github.com/pmqs/IO-Compress/issues> (preferred) or
730 <https://rt.cpan.org/Public/Dist/Display.html?Name=IO-Compress>.
731
733 Compress::Zlib, IO::Compress::Gzip, IO::Compress::Deflate,
734 IO::Uncompress::Inflate, IO::Compress::RawDeflate,
735 IO::Uncompress::RawInflate, IO::Compress::Bzip2,
736 IO::Uncompress::Bunzip2, IO::Compress::Lzma, IO::Uncompress::UnLzma,
737 IO::Compress::Xz, IO::Uncompress::UnXz, IO::Compress::Lzip,
738 IO::Uncompress::UnLzip, IO::Compress::Lzop, IO::Uncompress::UnLzop,
739 IO::Compress::Lzf, IO::Uncompress::UnLzf, IO::Compress::Zstd,
740 IO::Uncompress::UnZstd, IO::Uncompress::AnyInflate,
741 IO::Uncompress::AnyUncompress
742
743 IO::Compress::FAQ
744
745 File::GlobMapper, Archive::Zip, Archive::Tar, IO::Zlib
746
747 For RFC 1950, 1951 and 1952 see
748 <https://datatracker.ietf.org/doc/html/rfc1950>,
749 <https://datatracker.ietf.org/doc/html/rfc1951> and
750 <https://datatracker.ietf.org/doc/html/rfc1952>
751
752 The zlib compression library was written by Jean-loup Gailly
753 "gzip@prep.ai.mit.edu" and Mark Adler "madler@alumni.caltech.edu".
754
755 The primary site for the zlib compression library is
756 <http://www.zlib.org>.
757
758 The primary site for the zlib-ng compression library is
759 <https://github.com/zlib-ng/zlib-ng>.
760
761 The primary site for gzip is <http://www.gzip.org>.
762
764 This module was written by Paul Marquess, "pmqs@cpan.org".
765
767 See the Changes file.
768
770 Copyright (c) 2005-2023 Paul Marquess. All rights reserved.
771
772 This program is free software; you can redistribute it and/or modify it
773 under the same terms as Perl itself.
774
775
776
777perl v5.38.0 2023-07-26 IO::Uncompress::Gunzip(3)