1Archive::Zip::FAQ(3) User Contributed Perl Documentation Archive::Zip::FAQ(3)
2
3
4
6 Archive::Zip::FAQ - Answers to a few frequently asked questions about
7 Archive::Zip
8
10 It seems that I keep answering the same questions over and over again.
11 I assume that this is because my documentation is deficient, rather
12 than that people don't read the documentation.
13
14 So this FAQ is an attempt to cut down on the number of personal answers
15 I have to give. At least I can now say "You did read the FAQ, right?".
16
17 The questions are not in any particular order. The answers assume the
18 current version of Archive::Zip; some of the answers depend on newly
19 added/fixed functionality.
20
22 Q: Archive::Zip won't install on my RedHat 9 system! It's broke!
23
24 A: This has become something of a FAQ. Basically, RedHat broke some
25 versions of Perl by setting LANG to UTF8. They apparently have a fixed
26 version out as an update.
27
28 You might try running CPAN or creating your Makefile after exporting
29 the LANG environment variable as
30
31 "LANG=C"
32
33 <https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=87682>
34
36 Q: My zip file is actually bigger than what I stored in it! Why?
37
38 A: Some things to make sure of:
39
40 Make sure that you are requesting COMPRESSION_DEFLATED if you are
41 storing strings.
42 $member->desiredCompressionMethod( COMPRESSION_DEFLATED );
43
44 Don't make lots of little files if you can help it.
45 Since zip computes the compression tables for each member, small
46 members without much entropy won't compress well. Instead, if
47 you've got lots of repeated strings in your data, try to combine
48 them into one big member.
49
50 Make sure that you are requesting COMPRESSION_STORED if you are storing
51 things that are already compressed.
52 If you're storing a .zip, .jpg, .mp3, or other compressed file in a
53 zip, then don't compress them again. They'll get bigger.
54
56 Q: Can you send me code to do (whatever)?
57
58 A: Have you looked in the "examples/" directory yet? It contains:
59
60 examples/calcSizes.pl -- How to find out how big a Zip file will be
61 before writing it
62 examples/copy.pl -- Copies one Zip file to another
63 examples/extract.pl -- extract file(s) from a Zip
64 examples/mailZip.pl -- make and mail a zip file
65 examples/mfh.pl -- demo for use of MockFileHandle
66 examples/readScalar.pl -- shows how to use IO::Scalar as the source
67 of a Zip read
68 examples/selfex.pl -- a brief example of a self-extracting Zip
69 examples/unzipAll.pl -- uses Archive::Zip::Tree to unzip an entire
70 Zip
71 examples/updateZip.pl -- shows how to read/modify/write a Zip
72 examples/updateTree.pl -- shows how to update a Zip in place
73 examples/writeScalar.pl -- shows how to use IO::Scalar as the
74 destination of a Zip write
75 examples/writeScalar2.pl -- shows how to use IO::String as the
76 destination of a Zip write
77 examples/zip.pl -- Constructs a Zip file
78 examples/zipcheck.pl -- One way to check a Zip file for validity
79 examples/zipinfo.pl -- Prints out information about a Zip archive
80 file
81 examples/zipGrep.pl -- Searches for text in Zip files
82 examples/ziptest.pl -- Lists a Zip file and checks member CRCs
83 examples/ziprecent.pl -- Puts recent files into a zipfile
84 examples/ziptest.pl -- Another way to check a Zip file for
85 validity
86
88 Q: Why can't I open a Zip file, add a member, and write it back? I get
89 an error message when I try.
90
91 A: Because Archive::Zip doesn't (and can't, generally) read file
92 contents into memory, the original Zip file is required to stay around
93 until the writing of the new file is completed.
94
95 The best way to do this is to write the Zip to a temporary file and
96 then rename the temporary file to have the old name (possibly after
97 deleting the old one).
98
99 Archive::Zip v1.02 added the archive methods "overwrite()" and
100 "overwriteAs()" to do this simply and carefully.
101
102 See "examples/updateZip.pl" for an example of this technique.
103
105 Q: Upon extracting files, I see that their modification (and access)
106 times are set to the time in the Zip archive. However, their creation
107 time is not set to the same time. Why?
108
109 A: Mostly because Perl doesn't give cross-platform access to creation
110 time. Indeed, many systems (like Unix) don't support such a concept.
111 However, if yours does, you can easily set it. Get the modification
112 time from the member using "lastModTime()".
113
115 Q: Can I use Archive::Zip to extract Unix gzip files?
116
117 A: No.
118
119 There is a distinction between Unix gzip files, and Zip archives that
120 also can use the gzip compression.
121
122 Depending on the format of the gzip file, you can use
123 Compress::Raw::Zlib, or Archive::Tar to decompress it (and de-archive
124 it in the case of Tar files).
125
126 You can unzip PKZIP/WinZip/etc/ archives using Archive::Zip (that's
127 what it's for) as long as any compressed members are compressed using
128 Deflate compression.
129
131 Q: How can I add a directory (or tree) full of files to a Zip?
132
133 A: You can use the Archive::Zip::addTree*() methods:
134
135 use Archive::Zip;
136 my $zip = Archive::Zip->new();
137 # add all readable files and directories below . as xyz/*
138 $zip->addTree( '.', 'xyz' );
139 # add all readable plain files below /abc as def/*
140 $zip->addTree( '/abc', 'def', sub { -f && -r } );
141 # add all .c files below /tmp as stuff/*
142 $zip->addTreeMatching( '/tmp', 'stuff', '\.c$' );
143 # add all .o files below /tmp as stuff/* if they aren't writable
144 $zip->addTreeMatching( '/tmp', 'stuff', '\.o$', sub { ! -w } );
145 # add all .so files below /tmp that are smaller than 200 bytes as stuff/*
146 $zip->addTreeMatching( '/tmp', 'stuff', '\.o$', sub { -s < 200 } );
147 # and write them into a file
148 $zip->writeToFileNamed('xxx.zip');
149
151 Q: How can I extract some (or all) files from a Zip into a different
152 directory?
153
154 A: You can use the Archive::Zip::extractTree() method: ??? ||
155
156 # now extract the same files into /tmpx
157 $zip->extractTree( 'stuff', '/tmpx' );
158
160 Q: How can I update a Zip from a directory tree, adding or replacing
161 only the newer files?
162
163 A: You can use the Archive::Zip::updateTree() method that was added in
164 version 1.09.
165
167 Q: It bothers me greatly that my file times are wrong by one second
168 about half the time. Why don't you do something about it?
169
170 A: Get over it. This is a result of the Zip format storing times in DOS
171 format, which has a resolution of only two seconds.
172
174 Q: My file times don't respect time zones. What gives?
175
176 A: If this is important to you, please submit patches to read the
177 various Extra Fields that encode times with time zones. I'm just using
178 the DOS Date/Time, which doesn't have a time zone.
179
181 Q: I want to make a self-extracting Zip file. Can I do this?
182
183 A: Yes. You can write a self-extracting archive stub (that is, a
184 version of unzip) to the output filehandle that you pass to
185 writeToFileHandle(). See examples/selfex.pl for how to write a self-
186 extracting archive.
187
188 However, you should understand that this will only work on one kind of
189 platform (the one for which the stub was compiled).
190
192 Q: How can I tell if a Zip has been damaged by adding garbage to the
193 beginning or inside the file?
194
195 A: I added code for this for the Amavis virus scanner. You can query
196 archives for their 'eocdOffset' property, which should be 0:
197
198 if ($zip->eocdOffset > 0)
199 { warn($zip->eocdOffset . " bytes of garbage at beginning or within Zip") }
200
201 When members are extracted, this offset will be used to adjust the
202 start of the member if necessary.
203
205 Q: I'm trying to extract a file out of a Zip produced by PKZIP, and
206 keep getting this error message:
207
208 error: Unsupported compression combination: read 6, write 0
209
210 A: You can't uncompress this archive member. Archive::Zip only supports
211 uncompressed members, and compressed members that are compressed using
212 the compression supported by Compress::Raw::Zlib. That means only
213 Deflated and Stored members.
214
215 Your file is compressed using the Shrink format, which is not supported
216 by Compress::Raw::Zlib.
217
218 You could, perhaps, use a command-line UnZip program (like the Info-Zip
219 one) to extract this.
220
222 Q: How do I decrypt encrypted Zip members?
223
224 A: With some other program or library. Archive::Zip doesn't support
225 decryption, and probably never will (unless you write it).
226
228 Q: How can Archive::Zip can test the validity of a Zip file?
229
230 A: If you try to decompress the file, the gzip streams will report
231 errors if you have garbage. Most of the time.
232
233 If you try to open the file and a central directory structure can't be
234 found, an error will be reported.
235
236 When a file is being read, if we can't find a proper PK.. signature in
237 the right places we report a format error.
238
239 If there is added garbage at the beginning of a Zip file (as inserted
240 by some viruses), you can find out about it, but Archive::Zip will
241 ignore it, and you can still use the archive. When it gets written back
242 out the added stuff will be gone.
243
244 There are two ready-to-use utilities in the examples directory that can
245 be used to test file integrity, or that you can use as examples for
246 your own code:
247
248 examples/zipcheck.pl shows how to use an attempted extraction to test a
249 file.
250 examples/ziptest.pl shows how to test CRCs in a file.
251
253 Q: Archive::Zip let me put the same file in my Zip twice! Why don't you
254 prevent this?
255
256 A: As far as I can tell, this is not disallowed by the Zip spec. If you
257 think it's a bad idea, check for it yourself:
258
259 $zip->addFile($someFile, $someName) unless $zip->memberNamed($someName);
260
261 I can even imagine cases where this might be useful (for instance,
262 multiple versions of files).
263
265 Q: Why doesn't Archive::Zip deal with file ownership, ACLs, etc.?
266
267 A: There is no standard way to represent these in the Zip file format.
268 If you want to send me code to properly handle the various extra fields
269 that have been used to represent these through the years, I'll look at
270 it.
271
273 Q: I've only installed modules using ActiveState's PPM program and
274 repository. But they have a much older version of Archive::Zip than is
275 in CPAN. Will you send me a newer PPM?
276
277 A: Probably not, unless I get lots of extra time. But there's no reason
278 you can't install the version from CPAN. Archive::Zip is pure Perl, so
279 all you need is NMAKE, which you can get for free from Microsoft (see
280 the FAQ in the ActiveState documentation for details on how to install
281 CPAN modules).
282
284 Q: How come my JPEGs and MP3's don't compress much when I put them into
285 Zips?
286
287 A: Because they're already compressed.
288
290 Q: I'm using Windows. When I try to use Archive::Zip, my machine locks
291 up/makes funny sounds/displays a BSOD/corrupts data. How can I fix
292 this?
293
294 A: First, try the newest version of Compress::Raw::Zlib. I know of
295 Windows-related problems prior to v1.14 of that library.
296
298 Q: I want to read a Zip file from (or write one to) a scalar variable
299 instead of a file. How can I do this?
300
301 A: Use "IO::String" and the "readFromFileHandle()" and
302 "writeToFileHandle()" methods. See "examples/readScalar.pl" and
303 "examples/writeScalar.pl".
304
306 Q: How do I read from a stream (like for the Info-Zip "funzip"
307 program)?
308
309 A: This is not currently supported, though writing to a stream is.
310
311
312
313perl v5.32.0 2020-07-28 Archive::Zip::FAQ(3)