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