1Archive::Zip::StreamedUUnszeirp(C3o)ntributed Perl DocumAerncthaitvieo:n:Zip::StreamedUnzip(3)
2
3
4
6 Archive::Zip::StreamedUnzip - Read Zip Archives in streaming mode
7
9 use Archive::Zip::StreamedUnzip qw($StreamedUnzipError) ;
10
11 my $z = new Archive::Zip::StreamedUnzip "my.zip"
12 or die "Cannot open zip file: $StreamedUnzipError\n" ;
13
14
15 # Iterate through a zip archive
16 while (my $member = $z->next)
17 {
18 print $member->name() . "\n" ;
19 }
20
21 # Archive::Zip::StreamedUnzip::Member
22
23 my $name = $member->name();
24 my $content = $member->content();
25 my $comment = $member->comment();
26
27 # open a filehandle to read from a zip member
28 $fh = $member->open("mydata1.txt");
29
30 # read blocks of data
31 read($fh, $buffer, 1234) ;
32
33 # or a line at a time
34 $line = <$fh> ;
35
36 close $fh;
37
38 $z->close();
39
41 Archive::Zip::StreamedUnzip is a module that allows reading of Zip
42 archives in streaming mode. This is useful if you are processing a zip
43 coming directly off a socket without having to read the complete file
44 into memory and/or store it on disk. Similarly it can be handy when
45 woking with a pipelined command.
46
47 Working with a streamed zip file does have limitations, so most of the
48 time Archive::Zip::SimpleUnzip and/or Archive::Zip are a better choice
49 of module for reading file files.
50
51 For writing Zip archives, there is a companion module, called
52 Archive::Zip::SimpleZip, that can create Zip archives.
53
54 Features
55 • Read zip archive from a file, a filehandle or from an in-memory
56 buffer.
57
58 • Perl Filehandle interface for reading a zip member.
59
60 • Supports deflate, store, bzip2, Zstandard (Zstd), Xz and lzma
61 compression.
62
63 • Supports Zip64, so can read archves larger than 4Gig and/or have
64 greater than 64K members.
65
66 Constructor
67 $z = new Archive::Zip::StreamedUnzip "myzipfile.zip" [, OPTIONS] ;
68 $z = new Archive::Zip::StreamedUnzip \$buffer [, OPTIONS] ;
69 $z = new Archive::Zip::StreamedUnzip $filehandle [, OPTIONS] ;
70
71 The constructor takes one mandatory parameter along with zero or more
72 optional parameters.
73
74 The mandatory parameter controls where the zip archive is read from.
75 This can be any one of the following
76
77 • Input from a Filename
78
79 When StreamedUnzip is passed a string, it will read the zip
80 archive from the filename stored in the string.
81
82 • Input from a String
83
84 When StreamedUnzip is passed a string reference, like "\$buffer",
85 it will read the in-memory zip archive from that string.
86
87 • Input from a Filehandle
88
89 When StreamedUnzip is passed a filehandle, it will read the zip
90 archive from that filehandle. Note the filehandle must be
91 seekable.
92
93 See "Options" for a list of the optional parameters that can be
94 specified when calling the constructor.
95
96 Options
97 None yet.
98
99 Methods
100 $z->next()
101 Returns the next member from the zip archive as a
102 Archive::Zip::StreamedUnzip::Member object. See
103 "Archive::Zip::StreamedUnzip::Member"
104
105 Standard usage is
106
107 use Archive::Zip::StreamedUnzip qw($StreamedUnzipError) ;
108
109 my $match = "hello";
110 my $zipfile = "my.zip";
111
112 my $z = new Archive::Zip::StreamedUnzip $zipfile
113 or die "Cannot open zip file: $StreamedUnzipError\n" ;
114
115 while (my $member = $z->next())
116 {
117 my $name = $member->name();
118 my $fh = $member->open();
119 while (<$fh>)
120 {
121 my $offset =
122 print "$name, line $.\n" if /$match/;
123 }
124 }
125
126 $z->close()
127 Closes the zip file.
128
130 The "next" method returns a member object of type
131 "Archive::Zip::StreamedUnzip::Member" that has the following methods.
132
133 $string = $m->name()
134 Returns the name of the member.
135
136 $data = $m->content()
137 Returns the uncompressed content.
138
139 $fh = $m->open()
140 Returns a filehandle that can be used to read the uncompressed
141 content.
142
144 Iterate through a Zip file
145 use Archive::Zip::StreamedUnzip qw($StreamedUnzipError) ;
146
147 my $zipfile = "my.zip";
148 my $z = new Archive::Zip::StreamedUnzip $zipfile
149 or die "Cannot open zip file: $StreamedUnzipError\n" ;
150
151 while (my $member = $z->next())
152 {
153 print "$member->name()\n";
154 }
155
156 Filehandle interface
157 Here is a simple grep, that walks through a zip file and prints
158 matching strings.
159
160 use Archive::Zip::StreamedUnzip qw($StreamedUnzipError) ;
161
162 my $match = "hello";
163 my $zipfile = "my.zip";
164
165 my $z = new Archive::Zip::StreamedUnzip $zipfile
166 or die "Cannot open zip file: $StreamedUnzipError\n" ;
167
168 while (my $member = $z->next())
169 {
170 my $name = $member->name();
171 my $fh = $member->open();
172 while (<$fh>)
173 {
174 my $offset =
175 print "$name, line $.\n" if /$match/;
176 }
177 }
178
179 Nested Zip
180 Here is a script that will list the contents of a zip file along with
181 any zip files that are embedded in it. In fact it will work with any
182 level of nesting.
183
184 sub walk
185 {
186 my $unzip = shift ;
187 my $depth = shift // 1;
188
189 while (my $member = $unzip->next())
190 {
191 my $name = $unzip->name();
192 print " " x $depth . "$name\n" ;
193
194 if ($name =~ /\.zip$/i)
195 {
196 my $fh = $member->open();
197 my $newunzip = new Archive::Zip::StreamedUnzip $fh;
198 walk($newunzip, $depth + 1);
199 }
200 }
201 }
202
203 my $unzip = new Archive::Zip::StreamedUnzip $zipfile
204 or die "Cannot open '$zipfile': $StreamedUnzipError";
205
206 print "$zipfile\n" ;
207 walk($unzip) ;
208
210 The intention is to be interoperable with zip archives created by other
211 programs, like pkzip or WinZip, but the majority of testing carried out
212 used the Info-Zip zip/unzip <http://www.info-zip.org/> programs running
213 on Linux.
214
215 This doesn't necessarily mean that there is no interoperability with
216 other zip programs like pkzip and WinZip - it just means that I haven't
217 tested them. Please report any issues you find.
218
219 Compression Methods Supported
220 The following compression methods are supported
221
222 deflate (8)
223 This is the most common compression used in zip archives.
224
225 store (0)
226 This is used when no compression has been carried out.
227
228 bzip2 (12)
229 Only if the "IO-Compress-Bzip2" module is available.
230
231 lzma (14)
232 Only if the "IO-Compress-Lzma" module is available.
233
234 Xz (95)
235 To read Xz content, the module "IO::Uncompress::UnXz" must be
236 installed.
237
238 Zstandard (93)
239 To read Zstandard content, the module "IO::Uncompress::UnZstd"
240 must be installed.
241
242 Zip64 Support
243 This modules supports Zip64, so it can read archves larger than 4Gig
244 and/or have greater than 64K members.
245
246 Limitations
247 The following features are not currently supported.
248
249 • Compression methods not listed in "Compression Methods Supported"
250
251 • Multi-Volume Archives
252
253 • Encrypted Archives
254
256 General feedback/questions/bug reports should be sent to
257 <https://github.com/pmqs/Archive-Zip-SimpleZip/issues> (preferred) or
258 <https://rt.cpan.org/Public/Dist/Display.html?Name=Archive-Zip-SimpleZip>.
259
261 Archive::Zip::SimpleUnzip, Archive::Zip::SimpleZip, Archive::Zip,
262 IO::Compress::Zip, IO::Uncompress::UnZip
263
265 This module was written by Paul Marquess, pmqs@cpan.org.
266
268 See the Changes file.
269
271 Copyright (c) 2019-2021 Paul Marquess. All rights reserved.
272
273 This program is free software; you can redistribute it and/or modify it
274 under the same terms as Perl itself.
275
276
277
278perl v5.34.0 2022-01-20 Archive::Zip::StreamedUnzip(3)