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