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