Debian Codegraph @

upstream/debian-unstable/man3/IO::Uncompress::Unzip.3perl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\" ========================================================================
56 .\"
57 .IX Title "IO::Uncompress::Unzip 3perl"
58 .TH IO::Uncompress::Unzip 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 IO::Uncompress::Unzip \- Read zip files/buffers
65 .SH SYNOPSIS
66 .IX Header "SYNOPSIS"
67 .Vb 1
68 \& use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
69 \&
70 \& my $status = unzip $input => $output [,OPTS]
71 \& or die "unzip failed: $UnzipError\en";
72 \&
73 \& my $z = IO::Uncompress::Unzip\->new( $input [OPTS] )
74 \& or die "unzip failed: $UnzipError\en";
75 \&
76 \& $status = $z\->read($buffer)
77 \& $status = $z\->read($buffer, $length)
78 \& $status = $z\->read($buffer, $length, $offset)
79 \& $line = $z\->getline()
80 \& $char = $z\->getc()
81 \& $char = $z\->ungetc()
82 \& $char = $z\->opened()
83 \&
84 \& $status = $z\->inflateSync()
85 \&
86 \& $data = $z\->trailingData()
87 \& $status = $z\->nextStream()
88 \& $data = $z\->getHeaderInfo()
89 \& $z\->tell()
90 \& $z\->seek($position, $whence)
91 \& $z\->binmode()
92 \& $z\->fileno()
93 \& $z\->eof()
94 \& $z\->close()
95 \&
96 \& $UnzipError ;
97 \&
98 \& # IO::File mode
99 \&
100 \& <$z>
101 \& read($z, $buffer);
102 \& read($z, $buffer, $length);
103 \& read($z, $buffer, $length, $offset);
104 \& tell($z)
105 \& seek($z, $position, $whence)
106 \& binmode($z)
107 \& fileno($z)
108 \& eof($z)
109 \& close($z)
110 .Ve
111 .SH DESCRIPTION
112 .IX Header "DESCRIPTION"
113 This module provides a Perl interface that allows the reading of
114 zlib files/buffers.
115 .PP
116 For writing zip files/buffers, see the companion module IO::Compress::Zip.
117 .PP
118 The primary purpose of this module is to provide \fIstreaming\fR read access to
119 zip files and buffers.
120 .PP
121 At present the following compression methods are supported by IO::Uncompress::Unzip
122 .IP "Store (0)" 5
123 .IX Item "Store (0)"
124 .PD 0
125 .IP "Deflate (8)" 5
126 .IX Item "Deflate (8)"
127 .IP "Bzip2 (12)" 5
128 .IX Item "Bzip2 (12)"
129 .PD
130 To read Bzip2 content, the module \f(CW\*(C`IO::Uncompress::Bunzip2\*(C'\fR must
131 be installed.
132 .IP "Lzma (14)" 5
133 .IX Item "Lzma (14)"
134 To read LZMA content, the module \f(CW\*(C`IO::Uncompress::UnLzma\*(C'\fR must
135 be installed.
136 .IP "Xz (95)" 5
137 .IX Item "Xz (95)"
138 To read Xz content, the module \f(CW\*(C`IO::Uncompress::UnXz\*(C'\fR must
139 be installed.
140 .IP "Zstandard (93)" 5
141 .IX Item "Zstandard (93)"
142 To read Zstandard content, the module \f(CW\*(C`IO::Uncompress::UnZstd\*(C'\fR must
143 be installed.
144 .SH "Functional Interface"
145 .IX Header "Functional Interface"
146 A top-level function, \f(CW\*(C`unzip\*(C'\fR, is provided to carry out
147 "one-shot" uncompression between buffers and/or files. For finer
148 control over the uncompression process, see the "OO Interface"
149 section.
150 .PP
151 .Vb 1
152 \& use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
153 \&
154 \& unzip $input_filename_or_reference => $output_filename_or_reference [,OPTS]
155 \& or die "unzip failed: $UnzipError\en";
156 .Ve
157 .PP
158 The functional interface needs Perl5.005 or better.
159 .ie n .SS "unzip $input_filename_or_reference => $output_filename_or_reference [, OPTS]"
160 .el .SS "unzip \f(CW$input_filename_or_reference\fP => \f(CW$output_filename_or_reference\fP [, OPTS]"
161 .IX Subsection "unzip $input_filename_or_reference => $output_filename_or_reference [, OPTS]"
162 \&\f(CW\*(C`unzip\*(C'\fR expects at least two parameters,
163 \&\f(CW$input_filename_or_reference\fR and \f(CW$output_filename_or_reference\fR
164 and zero or more optional parameters (see "Optional Parameters")
165 .PP
166 \fIThe \fR\f(CI$input_filename_or_reference\fR\fI parameter\fR
167 .IX Subsection "The $input_filename_or_reference parameter"
168 .PP
169 The parameter, \f(CW$input_filename_or_reference\fR, is used to define the
170 source of the compressed data.
171 .PP
172 It can take one of the following forms:
173 .IP "A filename" 5
174 .IX Item "A filename"
175 If the \f(CW$input_filename_or_reference\fR parameter is a simple scalar, it is
176 assumed to be a filename. This file will be opened for reading and the
177 input data will be read from it.
178 .IP "A filehandle" 5
179 .IX Item "A filehandle"
180 If the \f(CW$input_filename_or_reference\fR parameter is a filehandle, the input
181 data will be read from it. The string '\-' can be used as an alias for
182 standard input.
183 .IP "A scalar reference" 5
184 .IX Item "A scalar reference"
185 If \f(CW$input_filename_or_reference\fR is a scalar reference, the input data
186 will be read from \f(CW$$input_filename_or_reference\fR.
187 .IP "An array reference" 5
188 .IX Item "An array reference"
189 If \f(CW$input_filename_or_reference\fR is an array reference, each element in
190 the array must be a filename.
191 .Sp
192 The input data will be read from each file in turn.
193 .Sp
194 The complete array will be walked to ensure that it only
195 contains valid filenames before any data is uncompressed.
196 .IP "An Input FileGlob string" 5
197 .IX Item "An Input FileGlob string"
198 If \f(CW$input_filename_or_reference\fR is a string that is delimited by the
199 characters "<" and ">" \f(CW\*(C`unzip\*(C'\fR will assume that it is an
200 \&\fIinput fileglob string\fR. The input is the list of files that match the
201 fileglob.
202 .Sp
203 See File::GlobMapper for more details.
204 .PP
205 If the \f(CW$input_filename_or_reference\fR parameter is any other type,
206 \&\f(CW\*(C`undef\*(C'\fR will be returned.
207 .PP
208 \fIThe \fR\f(CI$output_filename_or_reference\fR\fI parameter\fR
209 .IX Subsection "The $output_filename_or_reference parameter"
210 .PP
211 The parameter \f(CW$output_filename_or_reference\fR is used to control the
212 destination of the uncompressed data. This parameter can take one of
213 these forms.
214 .IP "A filename" 5
215 .IX Item "A filename"
216 If the \f(CW$output_filename_or_reference\fR parameter is a simple scalar, it is
217 assumed to be a filename. This file will be opened for writing and the
218 uncompressed data will be written to it.
219 .IP "A filehandle" 5
220 .IX Item "A filehandle"
221 If the \f(CW$output_filename_or_reference\fR parameter is a filehandle, the
222 uncompressed data will be written to it. The string '\-' can be used as
223 an alias for standard output.
224 .IP "A scalar reference" 5
225 .IX Item "A scalar reference"
226 If \f(CW$output_filename_or_reference\fR is a scalar reference, the
227 uncompressed data will be stored in \f(CW$$output_filename_or_reference\fR.
228 .IP "An Array Reference" 5
229 .IX Item "An Array Reference"
230 If \f(CW$output_filename_or_reference\fR is an array reference,
231 the uncompressed data will be pushed onto the array.
232 .IP "An Output FileGlob" 5
233 .IX Item "An Output FileGlob"
234 If \f(CW$output_filename_or_reference\fR is a string that is delimited by the
235 characters "<" and ">" \f(CW\*(C`unzip\*(C'\fR will assume that it is an
236 \&\fIoutput fileglob string\fR. The output is the list of files that match the
237 fileglob.
238 .Sp
239 When \f(CW$output_filename_or_reference\fR is an fileglob string,
240 \&\f(CW$input_filename_or_reference\fR must also be a fileglob string. Anything
241 else is an error.
242 .Sp
243 See File::GlobMapper for more details.
244 .PP
245 If the \f(CW$output_filename_or_reference\fR parameter is any other type,
246 \&\f(CW\*(C`undef\*(C'\fR will be returned.
247 .SS Notes
248 .IX Subsection "Notes"
249 When \f(CW$input_filename_or_reference\fR maps to multiple compressed
250 files/buffers and \f(CW$output_filename_or_reference\fR is
251 a single file/buffer, after uncompression \f(CW$output_filename_or_reference\fR will contain a
252 concatenation of all the uncompressed data from each of the input
253 files/buffers.
254 .SS "Optional Parameters"
255 .IX Subsection "Optional Parameters"
256 The optional parameters for the one-shot function \f(CW\*(C`unzip\*(C'\fR
257 are (for the most part) identical to those used with the OO interface defined in the
258 "Constructor Options" section. The exceptions are listed below
259 .ie n .IP """AutoClose => 0|1""" 5
260 .el .IP "\f(CWAutoClose => 0|1\fR" 5
261 .IX Item "AutoClose => 0|1"
262 This option applies to any input or output data streams to
263 \&\f(CW\*(C`unzip\*(C'\fR that are filehandles.
264 .Sp
265 If \f(CW\*(C`AutoClose\*(C'\fR is specified, and the value is true, it will result in all
266 input and/or output filehandles being closed once \f(CW\*(C`unzip\*(C'\fR has
267 completed.
268 .Sp
269 This parameter defaults to 0.
270 .ie n .IP """BinModeOut => 0|1""" 5
271 .el .IP "\f(CWBinModeOut => 0|1\fR" 5
272 .IX Item "BinModeOut => 0|1"
273 This option is now a no-op. All files will be written in binmode.
274 .ie n .IP """Append => 0|1""" 5
275 .el .IP "\f(CWAppend => 0|1\fR" 5
276 .IX Item "Append => 0|1"
277 The behaviour of this option is dependent on the type of output data
278 stream.
279 .RS 5
280 .IP \(bu 5
281 A Buffer
282 .Sp
283 If \f(CW\*(C`Append\*(C'\fR is enabled, all uncompressed data will be append to the end of
284 the output buffer. Otherwise the output buffer will be cleared before any
285 uncompressed data is written to it.
286 .IP \(bu 5
287 A Filename
288 .Sp
289 If \f(CW\*(C`Append\*(C'\fR is enabled, the file will be opened in append mode. Otherwise
290 the contents of the file, if any, will be truncated before any uncompressed
291 data is written to it.
292 .IP \(bu 5
293 A Filehandle
294 .Sp
295 If \f(CW\*(C`Append\*(C'\fR is enabled, the filehandle will be positioned to the end of
296 the file via a call to \f(CW\*(C`seek\*(C'\fR before any uncompressed data is
297 written to it. Otherwise the file pointer will not be moved.
298 .RE
299 .RS 5
300 .Sp
301 When \f(CW\*(C`Append\*(C'\fR is specified, and set to true, it will \fIappend\fR all uncompressed
302 data to the output data stream.
303 .Sp
304 So when the output is a filehandle it will carry out a seek to the eof
305 before writing any uncompressed data. If the output is a filename, it will be opened for
306 appending. If the output is a buffer, all uncompressed data will be
307 appended to the existing buffer.
308 .Sp
309 Conversely when \f(CW\*(C`Append\*(C'\fR is not specified, or it is present and is set to
310 false, it will operate as follows.
311 .Sp
312 When the output is a filename, it will truncate the contents of the file
313 before writing any uncompressed data. If the output is a filehandle
314 its position will not be changed. If the output is a buffer, it will be
315 wiped before any uncompressed data is output.
316 .Sp
317 Defaults to 0.
318 .RE
319 .ie n .IP """MultiStream => 0|1""" 5
320 .el .IP "\f(CWMultiStream => 0|1\fR" 5
321 .IX Item "MultiStream => 0|1"
322 If the input file/buffer contains multiple compressed data streams, this
323 option will uncompress the whole lot as a single data stream.
324 .Sp
325 Defaults to 0.
326 .ie n .IP """TrailingData => $scalar""" 5
327 .el .IP "\f(CWTrailingData => $scalar\fR" 5
328 .IX Item "TrailingData => $scalar"
329 Returns the data, if any, that is present immediately after the compressed
330 data stream once uncompression is complete.
331 .Sp
332 This option can be used when there is useful information immediately
333 following the compressed data stream, and you don't know the length of the
334 compressed data stream.
335 .Sp
336 If the input is a buffer, \f(CW\*(C`trailingData\*(C'\fR will return everything from the
337 end of the compressed data stream to the end of the buffer.
338 .Sp
339 If the input is a filehandle, \f(CW\*(C`trailingData\*(C'\fR will return the data that is
340 left in the filehandle input buffer once the end of the compressed data
341 stream has been reached. You can then use the filehandle to read the rest
342 of the input file.
343 .Sp
344 Don't bother using \f(CW\*(C`trailingData\*(C'\fR if the input is a filename.
345 .Sp
346 If you know the length of the compressed data stream before you start
347 uncompressing, you can avoid having to use \f(CW\*(C`trailingData\*(C'\fR by setting the
348 \&\f(CW\*(C`InputLength\*(C'\fR option.
349 .SS "OneShot Examples"
350 .IX Subsection "OneShot Examples"
351 Say you have a zip file, \f(CW\*(C`file1.zip\*(C'\fR, that only contains a
352 single member, you can read it and write the uncompressed data to the
353 file \f(CW\*(C`file1.txt\*(C'\fR like this.
354 .PP
355 .Vb 3
356 \& use strict ;
357 \& use warnings ;
358 \& use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
359 \&
360 \& my $input = "file1.zip";
361 \& my $output = "file1.txt";
362 \& unzip $input => $output
363 \& or die "unzip failed: $UnzipError\en";
364 .Ve
365 .PP
366 If you have a zip file that contains multiple members and want to read a
367 specific member from the file, say \f(CW"data1"\fR, use the \f(CW\*(C`Name\*(C'\fR option
368 .PP
369 .Vb 3
370 \& use strict ;
371 \& use warnings ;
372 \& use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
373 \&
374 \& my $input = "file1.zip";
375 \& my $output = "file1.txt";
376 \& unzip $input => $output, Name => "data1"
377 \& or die "unzip failed: $UnzipError\en";
378 .Ve
379 .PP
380 Alternatively, if you want to read the \f(CW"data1"\fR member into memory, use
381 a scalar reference for the \f(CW\*(C`output\*(C'\fR parameter.
382 .PP
383 .Vb 3
384 \& use strict ;
385 \& use warnings ;
386 \& use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
387 \&
388 \& my $input = "file1.zip";
389 \& my $output ;
390 \& unzip $input => \e$output, Name => "data1"
391 \& or die "unzip failed: $UnzipError\en";
392 \& # $output now contains the uncompressed data
393 .Ve
394 .PP
395 To read from an existing Perl filehandle, \f(CW$input\fR, and write the
396 uncompressed data to a buffer, \f(CW$buffer\fR.
397 .PP
398 .Vb 4
399 \& use strict ;
400 \& use warnings ;
401 \& use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
402 \& use IO::File ;
403 \&
404 \& my $input = IO::File\->new( "<file1.zip" )
405 \& or die "Cannot open \*(Aqfile1.zip\*(Aq: $!\en" ;
406 \& my $buffer ;
407 \& unzip $input => \e$buffer
408 \& or die "unzip failed: $UnzipError\en";
409 .Ve
410 .SH "OO Interface"
411 .IX Header "OO Interface"
412 .SS Constructor
413 .IX Subsection "Constructor"
414 The format of the constructor for IO::Uncompress::Unzip is shown below
415 .PP
416 .Vb 2
417 \& my $z = IO::Uncompress::Unzip\->new( $input [OPTS] )
418 \& or die "IO::Uncompress::Unzip failed: $UnzipError\en";
419 .Ve
420 .PP
421 The constructor takes one mandatory parameter, \f(CW$input\fR, defined below, and
422 zero or more \f(CW\*(C`OPTS\*(C'\fR, defined in "Constructor Options".
423 .PP
424 Returns an \f(CW\*(C`IO::Uncompress::Unzip\*(C'\fR object on success and undef on failure.
425 The variable \f(CW$UnzipError\fR will contain an error message on failure.
426 .PP
427 If you are running Perl 5.005 or better the object, \f(CW$z\fR, returned from
428 IO::Uncompress::Unzip can be used exactly like an IO::File filehandle.
429 This means that all normal input file operations can be carried out with
430 \&\f(CW$z\fR. For example, to read a line from a compressed file/buffer you can
431 use either of these forms
432 .PP
433 .Vb 2
434 \& $line = $z\->getline();
435 \& $line = <$z>;
436 .Ve
437 .PP
438 Below is a simple exaple of using the OO interface to read the compressed file
439 \&\f(CW\*(C`myfile.zip\*(C'\fR and write its contents to stdout.
440 .PP
441 .Vb 3
442 \& my $filename = "myfile.zip";
443 \& my $z = IO::Uncompress::Unzip\->new($filename)
444 \& or die "IO::Uncompress::Unzip failed: $UnzipError\en";
445 \&
446 \& while (<$z>) {
447 \& print $_;
448 \& }
449 \& $z\->close();
450 .Ve
451 .PP
452 See "EXAMPLES" for further examples
453 .PP
454 The mandatory parameter \f(CW$input\fR is used to determine the source of the
455 compressed data. This parameter can take one of three forms.
456 .IP "A filename" 5
457 .IX Item "A filename"
458 If the \f(CW$input\fR parameter is a scalar, it is assumed to be a filename. This
459 file will be opened for reading and the compressed data will be read from it.
460 .IP "A filehandle" 5
461 .IX Item "A filehandle"
462 If the \f(CW$input\fR parameter is a filehandle, the compressed data will be
463 read from it.
464 The string '\-' can be used as an alias for standard input.
465 .IP "A scalar reference" 5
466 .IX Item "A scalar reference"
467 If \f(CW$input\fR is a scalar reference, the compressed data will be read from
468 \&\f(CW$$input\fR.
469 .SS "Constructor Options"
470 .IX Subsection "Constructor Options"
471 The option names defined below are case insensitive and can be optionally
472 prefixed by a '\-'. So all of the following are valid
473 .PP
474 .Vb 4
475 \& \-AutoClose
476 \& \-autoclose
477 \& AUTOCLOSE
478 \& autoclose
479 .Ve
480 .PP
481 OPTS is a combination of the following options:
482 .ie n .IP """Name => ""membername""""" 5
483 .el .IP "\f(CWName => ""membername""\fR" 5
484 .IX Item "Name => ""membername"""
485 Open "membername" from the zip file for reading.
486 .ie n .IP """Efs => 0| 1""" 5
487 .el .IP "\f(CWEfs => 0| 1\fR" 5
488 .IX Item "Efs => 0| 1"
489 When this option is set to true AND the zip archive being read has
490 the "Language Encoding Flag" (EFS) set, the member name is assumed to be encoded in UTF\-8.
491 .Sp
492 If the member name in the zip archive is not valid UTF\-8 when this optionn is true,
493 the script will die with an error message.
494 .Sp
495 Note that this option only works with Perl 5.8.4 or better.
496 .Sp
497 This option defaults to \fBfalse\fR.
498 .ie n .IP """AutoClose => 0|1""" 5
499 .el .IP "\f(CWAutoClose => 0|1\fR" 5
500 .IX Item "AutoClose => 0|1"
501 This option is only valid when the \f(CW$input\fR parameter is a filehandle. If
502 specified, and the value is true, it will result in the file being closed once
503 either the \f(CW\*(C`close\*(C'\fR method is called or the IO::Uncompress::Unzip object is
504 destroyed.
505 .Sp
506 This parameter defaults to 0.
507 .ie n .IP """MultiStream => 0|1""" 5
508 .el .IP "\f(CWMultiStream => 0|1\fR" 5
509 .IX Item "MultiStream => 0|1"
510 Treats the complete zip file/buffer as a single compressed data
511 stream. When reading in multi-stream mode each member of the zip
512 file/buffer will be uncompressed in turn until the end of the file/buffer
513 is encountered.
514 .Sp
515 This parameter defaults to 0.
516 .ie n .IP """Prime => $string""" 5
517 .el .IP "\f(CWPrime => $string\fR" 5
518 .IX Item "Prime => $string"
519 This option will uncompress the contents of \f(CW$string\fR before processing the
520 input file/buffer.
521 .Sp
522 This option can be useful when the compressed data is embedded in another
523 file/data structure and it is not possible to work out where the compressed
524 data begins without having to read the first few bytes. If this is the
525 case, the uncompression can be \fIprimed\fR with these bytes using this
526 option.
527 .ie n .IP """Transparent => 0|1""" 5
528 .el .IP "\f(CWTransparent => 0|1\fR" 5
529 .IX Item "Transparent => 0|1"
530 If this option is set and the input file/buffer is not compressed data,
531 the module will allow reading of it anyway.
532 .Sp
533 In addition, if the input file/buffer does contain compressed data and
534 there is non-compressed data immediately following it, setting this option
535 will make this module treat the whole file/buffer as a single data stream.
536 .Sp
537 This option defaults to 1.
538 .ie n .IP """BlockSize => $num""" 5
539 .el .IP "\f(CWBlockSize => $num\fR" 5
540 .IX Item "BlockSize => $num"
541 When reading the compressed input data, IO::Uncompress::Unzip will read it in
542 blocks of \f(CW$num\fR bytes.
543 .Sp
544 This option defaults to 4096.
545 .ie n .IP """InputLength => $size""" 5
546 .el .IP "\f(CWInputLength => $size\fR" 5
547 .IX Item "InputLength => $size"
548 When present this option will limit the number of compressed bytes read
549 from the input file/buffer to \f(CW$size\fR. This option can be used in the
550 situation where there is useful data directly after the compressed data
551 stream and you know beforehand the exact length of the compressed data
552 stream.
553 .Sp
554 This option is mostly used when reading from a filehandle, in which case
555 the file pointer will be left pointing to the first byte directly after the
556 compressed data stream.
557 .Sp
558 This option defaults to off.
559 .ie n .IP """Append => 0|1""" 5
560 .el .IP "\f(CWAppend => 0|1\fR" 5
561 .IX Item "Append => 0|1"
562 This option controls what the \f(CW\*(C`read\*(C'\fR method does with uncompressed data.
563 .Sp
564 If set to 1, all uncompressed data will be appended to the output parameter
565 of the \f(CW\*(C`read\*(C'\fR method.
566 .Sp
567 If set to 0, the contents of the output parameter of the \f(CW\*(C`read\*(C'\fR method
568 will be overwritten by the uncompressed data.
569 .Sp
570 Defaults to 0.
571 .ie n .IP """Strict => 0|1""" 5
572 .el .IP "\f(CWStrict => 0|1\fR" 5
573 .IX Item "Strict => 0|1"
574 This option controls whether the extra checks defined below are used when
575 carrying out the decompression. When Strict is on, the extra tests are
576 carried out, when Strict is off they are not.
577 .Sp
578 The default for this option is off.
579 .SH Methods
580 .IX Header "Methods"
581 .SS read
582 .IX Subsection "read"
583 Usage is
584 .PP
585 .Vb 1
586 \& $status = $z\->read($buffer)
587 .Ve
588 .PP
589 Reads a block of compressed data (the size of the compressed block is
590 determined by the \f(CW\*(C`Buffer\*(C'\fR option in the constructor), uncompresses it and
591 writes any uncompressed data into \f(CW$buffer\fR. If the \f(CW\*(C`Append\*(C'\fR parameter is
592 set in the constructor, the uncompressed data will be appended to the
593 \&\f(CW$buffer\fR parameter. Otherwise \f(CW$buffer\fR will be overwritten.
594 .PP
595 Returns the number of uncompressed bytes written to \f(CW$buffer\fR, zero if eof
596 or a negative number on error.
597 .SS read
598 .IX Subsection "read"
599 Usage is
600 .PP
601 .Vb 2
602 \& $status = $z\->read($buffer, $length)
603 \& $status = $z\->read($buffer, $length, $offset)
604 \&
605 \& $status = read($z, $buffer, $length)
606 \& $status = read($z, $buffer, $length, $offset)
607 .Ve
608 .PP
609 Attempt to read \f(CW$length\fR bytes of uncompressed data into \f(CW$buffer\fR.
610 .PP
611 The main difference between this form of the \f(CW\*(C`read\*(C'\fR method and the
612 previous one, is that this one will attempt to return \fIexactly\fR \f(CW$length\fR
613 bytes. The only circumstances that this function will not is if end-of-file
614 or an IO error is encountered.
615 .PP
616 Returns the number of uncompressed bytes written to \f(CW$buffer\fR, zero if eof
617 or a negative number on error.
618 .SS getline
619 .IX Subsection "getline"
620 Usage is
621 .PP
622 .Vb 2
623 \& $line = $z\->getline()
624 \& $line = <$z>
625 .Ve
626 .PP
627 Reads a single line.
628 .PP
629 This method fully supports the use of the variable \f(CW$/\fR (or
630 \&\f(CW$INPUT_RECORD_SEPARATOR\fR or \f(CW$RS\fR when \f(CW\*(C`English\*(C'\fR is in use) to
631 determine what constitutes an end of line. Paragraph mode, record mode and
632 file slurp mode are all supported.
633 .SS getc
634 .IX Subsection "getc"
635 Usage is
636 .PP
637 .Vb 1
638 \& $char = $z\->getc()
639 .Ve
640 .PP
641 Read a single character.
642 .SS ungetc
643 .IX Subsection "ungetc"
644 Usage is
645 .PP
646 .Vb 1
647 \& $char = $z\->ungetc($string)
648 .Ve
649 .SS inflateSync
650 .IX Subsection "inflateSync"
651 Usage is
652 .PP
653 .Vb 1
654 \& $status = $z\->inflateSync()
655 .Ve
656 .PP
657 TODO
658 .SS getHeaderInfo
659 .IX Subsection "getHeaderInfo"
660 Usage is
661 .PP
662 .Vb 2
663 \& $hdr = $z\->getHeaderInfo();
664 \& @hdrs = $z\->getHeaderInfo();
665 .Ve
666 .PP
667 This method returns either a hash reference (in scalar context) or a list
668 or hash references (in array context) that contains information about each
669 of the header fields in the compressed data stream(s).
670 .SS tell
671 .IX Subsection "tell"
672 Usage is
673 .PP
674 .Vb 2
675 \& $z\->tell()
676 \& tell $z
677 .Ve
678 .PP
679 Returns the uncompressed file offset.
680 .SS eof
681 .IX Subsection "eof"
682 Usage is
683 .PP
684 .Vb 2
685 \& $z\->eof();
686 \& eof($z);
687 .Ve
688 .PP
689 Returns true if the end of the compressed input stream has been reached.
690 .SS seek
691 .IX Subsection "seek"
692 .Vb 2
693 \& $z\->seek($position, $whence);
694 \& seek($z, $position, $whence);
695 .Ve
696 .PP
697 Provides a sub-set of the \f(CW\*(C`seek\*(C'\fR functionality, with the restriction
698 that it is only legal to seek forward in the input file/buffer.
699 It is a fatal error to attempt to seek backward.
700 .PP
701 Note that the implementation of \f(CW\*(C`seek\*(C'\fR in this module does not provide
702 true random access to a compressed file/buffer. It works by uncompressing
703 data from the current offset in the file/buffer until it reaches the
704 uncompressed offset specified in the parameters to \f(CW\*(C`seek\*(C'\fR. For very small
705 files this may be acceptable behaviour. For large files it may cause an
706 unacceptable delay.
707 .PP
708 The \f(CW$whence\fR parameter takes one the usual values, namely SEEK_SET,
709 SEEK_CUR or SEEK_END.
710 .PP
711 Returns 1 on success, 0 on failure.
712 .SS binmode
713 .IX Subsection "binmode"
714 Usage is
715 .PP
716 .Vb 2
717 \& $z\->binmode
718 \& binmode $z ;
719 .Ve
720 .PP
721 This is a noop provided for completeness.
722 .SS opened
723 .IX Subsection "opened"
724 .Vb 1
725 \& $z\->opened()
726 .Ve
727 .PP
728 Returns true if the object currently refers to a opened file/buffer.
729 .SS autoflush
730 .IX Subsection "autoflush"
731 .Vb 2
732 \& my $prev = $z\->autoflush()
733 \& my $prev = $z\->autoflush(EXPR)
734 .Ve
735 .PP
736 If the \f(CW$z\fR object is associated with a file or a filehandle, this method
737 returns the current autoflush setting for the underlying filehandle. If
738 \&\f(CW\*(C`EXPR\*(C'\fR is present, and is non-zero, it will enable flushing after every
739 write/print operation.
740 .PP
741 If \f(CW$z\fR is associated with a buffer, this method has no effect and always
742 returns \f(CW\*(C`undef\*(C'\fR.
743 .PP
744 \&\fBNote\fR that the special variable \f(CW$|\fR \fBcannot\fR be used to set or
745 retrieve the autoflush setting.
746 .SS input_line_number
747 .IX Subsection "input_line_number"
748 .Vb 2
749 \& $z\->input_line_number()
750 \& $z\->input_line_number(EXPR)
751 .Ve
752 .PP
753 Returns the current uncompressed line number. If \f(CW\*(C`EXPR\*(C'\fR is present it has
754 the effect of setting the line number. Note that setting the line number
755 does not change the current position within the file/buffer being read.
756 .PP
757 The contents of \f(CW$/\fR are used to determine what constitutes a line
758 terminator.
759 .SS fileno
760 .IX Subsection "fileno"
761 .Vb 2
762 \& $z\->fileno()
763 \& fileno($z)
764 .Ve
765 .PP
766 If the \f(CW$z\fR object is associated with a file or a filehandle, \f(CW\*(C`fileno\*(C'\fR
767 will return the underlying file descriptor. Once the \f(CW\*(C`close\*(C'\fR method is
768 called \f(CW\*(C`fileno\*(C'\fR will return \f(CW\*(C`undef\*(C'\fR.
769 .PP
770 If the \f(CW$z\fR object is associated with a buffer, this method will return
771 \&\f(CW\*(C`undef\*(C'\fR.
772 .SS close
773 .IX Subsection "close"
774 .Vb 2
775 \& $z\->close() ;
776 \& close $z ;
777 .Ve
778 .PP
779 Closes the output file/buffer.
780 .PP
781 For most versions of Perl this method will be automatically invoked if
782 the IO::Uncompress::Unzip object is destroyed (either explicitly or by the
783 variable with the reference to the object going out of scope). The
784 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
785 these cases, the \f(CW\*(C`close\*(C'\fR method will be called automatically, but
786 not until global destruction of all live objects when the program is
787 terminating.
788 .PP
789 Therefore, if you want your scripts to be able to run on all versions
790 of Perl, you should call \f(CW\*(C`close\*(C'\fR explicitly and not rely on automatic
791 closing.
792 .PP
793 Returns true on success, otherwise 0.
794 .PP
795 If the \f(CW\*(C`AutoClose\*(C'\fR option has been enabled when the IO::Uncompress::Unzip
796 object was created, and the object is associated with a file, the
797 underlying file will also be closed.
798 .SS nextStream
799 .IX Subsection "nextStream"
800 Usage is
801 .PP
802 .Vb 1
803 \& my $status = $z\->nextStream();
804 .Ve
805 .PP
806 Skips to the next compressed data stream in the input file/buffer. If a new
807 compressed data stream is found, the eof marker will be cleared and \f(CW$.\fR
808 will be reset to 0.
809 .PP
810 If trailing data is present immediately after the zip archive and the
811 \&\f(CW\*(C`Transparent\*(C'\fR option is enabled, this method will consider that trailing
812 data to be another member of the zip archive.
813 .PP
814 Returns 1 if a new stream was found, 0 if none was found, and \-1 if an
815 error was encountered.
816 .SS trailingData
817 .IX Subsection "trailingData"
818 Usage is
819 .PP
820 .Vb 1
821 \& my $data = $z\->trailingData();
822 .Ve
823 .PP
824 Returns the data, if any, that is present immediately after the compressed
825 data stream once uncompression is complete. It only makes sense to call
826 this method once the end of the compressed data stream has been
827 encountered.
828 .PP
829 This option can be used when there is useful information immediately
830 following the compressed data stream, and you don't know the length of the
831 compressed data stream.
832 .PP
833 If the input is a buffer, \f(CW\*(C`trailingData\*(C'\fR will return everything from the
834 end of the compressed data stream to the end of the buffer.
835 .PP
836 If the input is a filehandle, \f(CW\*(C`trailingData\*(C'\fR will return the data that is
837 left in the filehandle input buffer once the end of the compressed data
838 stream has been reached. You can then use the filehandle to read the rest
839 of the input file.
840 .PP
841 Don't bother using \f(CW\*(C`trailingData\*(C'\fR if the input is a filename.
842 .PP
843 If you know the length of the compressed data stream before you start
844 uncompressing, you can avoid having to use \f(CW\*(C`trailingData\*(C'\fR by setting the
845 \&\f(CW\*(C`InputLength\*(C'\fR option in the constructor.
846 .SH Importing
847 .IX Header "Importing"
848 No symbolic constants are required by IO::Uncompress::Unzip at present.
849 .IP :all 5
850 .IX Item ":all"
851 Imports \f(CW\*(C`unzip\*(C'\fR and \f(CW$UnzipError\fR.
852 Same as doing this
853 .Sp
854 .Vb 1
855 \& use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
856 .Ve
857 .SH EXAMPLES
858 .IX Header "EXAMPLES"
859 .SS "Simple Read"
860 .IX Subsection "Simple Read"
861 Say you have a zip file, \f(CW\*(C`file1.zip\*(C'\fR, that only contains a
862 single member, you can read it and write the uncompressed data to the
863 file \f(CW\*(C`file1.txt\*(C'\fR like this.
864 .PP
865 .Vb 3
866 \& use strict ;
867 \& use warnings ;
868 \& use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
869 \&
870 \& my $filename = "file1.zip";
871 \& my $z = IO::Uncompress::Unzip\->new($filename)
872 \& or die "IO::Uncompress::Unzip failed: $UnzipError\en";
873 \& open my $out, ">", "file1.txt";
874 \&
875 \& while (<$z>) {
876 \& print $out $_;
877 \& }
878 \& $z\->close();
879 .Ve
880 .PP
881 If you have a zip file that contains multiple members and want to read a
882 specific member from the file, say \f(CW"data1"\fR, use the \f(CW\*(C`Name\*(C'\fR option when
883 constructing the
884 .PP
885 .Vb 3
886 \& use strict ;
887 \& use warnings ;
888 \& use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
889 \&
890 \& my $filename = "file1.zip";
891 \& my $z = IO::Uncompress::Unzip\->new($filename, Name => "data1")
892 \& or die "IO::Uncompress::Unzip failed: $UnzipError\en";
893 .Ve
894 .SS "Walking through a zip file"
895 .IX Subsection "Walking through a zip file"
896 The code below can be used to traverse a zip file, one compressed data
897 stream at a time.
898 .PP
899 .Vb 1
900 \& use IO::Uncompress::Unzip qw($UnzipError);
901 \&
902 \& my $zipfile = "somefile.zip";
903 \& my $u = IO::Uncompress::Unzip\->new( $zipfile )
904 \& or die "Cannot open $zipfile: $UnzipError";
905 \&
906 \& my $status;
907 \& for ($status = 1; $status > 0; $status = $u\->nextStream())
908 \& {
909 \&
910 \& my $name = $u\->getHeaderInfo()\->{Name};
911 \& warn "Processing member $name\en" ;
912 \&
913 \& my $buff;
914 \& while (($status = $u\->read($buff)) > 0) {
915 \& # Do something here
916 \& }
917 \&
918 \& last if $status < 0;
919 \& }
920 \&
921 \& die "Error processing $zipfile: $!\en"
922 \& if $status < 0 ;
923 .Ve
924 .PP
925 Each individual compressed data stream is read until the logical
926 end-of-file is reached. Then \f(CW\*(C`nextStream\*(C'\fR is called. This will skip to the
927 start of the next compressed data stream and clear the end-of-file flag.
928 .PP
929 It is also worth noting that \f(CW\*(C`nextStream\*(C'\fR can be called at any time \-\- you
930 don't have to wait until you have exhausted a compressed data stream before
931 skipping to the next one.
932 .SS "Unzipping a complete zip file to disk"
933 .IX Subsection "Unzipping a complete zip file to disk"
934 Daniel S. Sterling has written a script that uses \f(CW\*(C`IO::Uncompress::UnZip\*(C'\fR
935 to read a zip file and unzip its contents to disk.
936 .PP
937 The script is available from <https://gist.github.com/eqhmcow/5389877>
938 .SS "Working with Net::FTP"
939 .IX Subsection "Working with Net::FTP"
940 See IO::Compress::FAQ
941 .SH SUPPORT
942 .IX Header "SUPPORT"
943 General feedback/questions/bug reports should be sent to
944 <https://github.com/pmqs/IO\-Compress/issues> (preferred) or
945 <https://rt.cpan.org/Public/Dist/Display.html?Name=IO\-Compress>.
946 .SH "SEE ALSO"
947 .IX Header "SEE ALSO"
948 Compress::Zlib, IO::Compress::Gzip, IO::Uncompress::Gunzip, IO::Compress::Deflate, IO::Uncompress::Inflate, IO::Compress::RawDeflate, IO::Uncompress::RawInflate, IO::Compress::Bzip2, IO::Uncompress::Bunzip2, IO::Compress::Lzma, IO::Uncompress::UnLzma, IO::Compress::Xz, IO::Uncompress::UnXz, IO::Compress::Lzip, IO::Uncompress::UnLzip, IO::Compress::Lzop, IO::Uncompress::UnLzop, IO::Compress::Lzf, IO::Uncompress::UnLzf, IO::Compress::Zstd, IO::Uncompress::UnZstd, IO::Uncompress::AnyInflate, IO::Uncompress::AnyUncompress
949 .PP
950 IO::Compress::FAQ
951 .PP
952 File::GlobMapper, Archive::Zip,
953 Archive::Tar,
954 IO::Zlib
955 .PP
956 For RFC 1950, 1951 and 1952 see
957 <https://datatracker.ietf.org/doc/html/rfc1950>,
958 <https://datatracker.ietf.org/doc/html/rfc1951> and
959 <https://datatracker.ietf.org/doc/html/rfc1952>
960 .PP
961 The \fIzlib\fR compression library was written by Jean-loup Gailly
962 \&\f(CW\*(C`[email protected]\*(C'\fR and Mark Adler \f(CW\*(C`[email protected]\*(C'\fR.
963 .PP
964 The primary site for the \fIzlib\fR compression library is
965 <http://www.zlib.org>.
966 .PP
967 The primary site for the \fIzlib-ng\fR compression library is
968 <https://github.com/zlib\-ng/zlib\-ng>.
969 .PP
970 The primary site for gzip is <http://www.gzip.org>.
971 .SH AUTHOR
972 .IX Header "AUTHOR"
973 This module was written by Paul Marquess, \f(CW\*(C`[email protected]\*(C'\fR.
974 .SH "MODIFICATION HISTORY"
975 .IX Header "MODIFICATION HISTORY"
976 See the Changes file.
977 .SH "COPYRIGHT AND LICENSE"
978 .IX Header "COPYRIGHT AND LICENSE"
979 Copyright (c) 2005\-2024 Paul Marquess. All rights reserved.
980 .PP
981 This program is free software; you can redistribute it and/or
982 modify it under the same terms as Perl itself.