Archive::Zip::FAQ(3)
πΈ NAME πΈ
okay bestie, this is Archive::Zip::FAQ(3) from User Contributed Perl Documentation β Archive::Zip::FAQ β Answers to a few frequently asked questions about Archive::Zip. π
π DESCRIPTION π
so real talk: I keep answering the same questions over and over again. I assume that this is because my documentation is deficient, rather than that people don't read the documentation. π
so this FAQ is an attempt to cut down on the number of personal answers I have to give. at least I can now say βYou did read the FAQ, right?β. π«Ά
the questions are not in any particular order. the answers assume the current version of Archive::Zip; some of the answers depend on newly added/fixed functionality. β¨
π Install problems on RedHat 8 or 9 with Perl 5.8.0 π
Q: Archive::Zip won't install on my RedHat 9 system! It's broke! π
A: this has become something of a FAQ. basically, RedHat broke some versions of Perl by setting LANG to UTF8. they apparently have a fixed version out as an update. π΅βπ«
you might try running CPAN or creating your Makefile after exporting the LANG environment variable as
"LANG=C"
<https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=87682>
π¦ Why is my zip file so big? π¦
Q: my zip file is actually bigger than what I stored in it! why? π
A: some things to make sure of:
- make sure that you are requesting
COMPRESSION_DEFLATEDif you are storing strings:$member->desiredCompressionMethod( COMPRESSION_DEFLATED );π₯ - don't make lots of little files if you can help it. since zip computes the compression tables for each member, small members without much entropy won't compress well. instead, if you've got lots of repeated strings in your data, try to combine them into one big member. π§©
- make sure that you are requesting
COMPRESSION_STOREDif you are storing things that are already compressed. if you're storing a.zip,.jpg,.mp3, or other compressed file in a zip, then don't compress them again. they'll get bigger. π
π§ Sample code? π§
Q: can you send me code to do (whatever)? π
A: have you looked in the examples/ directory yet? it contains:
examples/calcSizes.plβ How to find out how big a Zip file will be before writing itexamples/copy.plβ Copies one Zip file to anotherexamples/extract.plβ extract file(s) from a Zipexamples/mailZip.plβ make and mail a zip fileexamples/mfh.plβ demo for use of MockFileHandleexamples/readScalar.plβ shows how to use IO::Scalar as the source of a Zip readexamples/selfex.plβ a brief example of a self-extracting Zipexamples/unzipAll.plβ uses Archive::Zip::Tree to unzip an entire Zipexamples/updateZip.plβ shows how to read/modify/write a Zipexamples/updateTree.plβ shows how to update a Zip in placeexamples/writeScalar.plβ shows how to use IO::Scalar as the destination of a Zip writeexamples/writeScalar2.plβ shows how to use IO::String as the destination of a Zip writeexamples/zip.plβ Constructs a Zip fileexamples/zipcheck.plβ One way to check a Zip file for validityexamples/zipinfo.plβ Prints out information about a Zip archive fileexamples/zipGrep.plβ Searches for text in Zip filesexamples/ziptest.plβ Lists a Zip file and checks member CRCsexamples/ziprecent.plβ Puts recent files into a zipfileexamples/ziptest.plβ Another way to check a Zip file for validity
π Can't Read/modify/write same Zip file π
Q: why can't I open a Zip file, add a member, and write it back? I get an error message when I try. π
A: because Archive::Zip doesn't (and can't, generally) read file contents into memory, the original Zip file is required to stay around until the writing of the new file is completed. π‘
the best way to do this is to write the Zip to a temporary file and then rename the temporary file to have the old name (possibly after deleting the old one). π«Ά
Archive::Zip v1.02 added the archive methods overwrite() and overwriteAs() to do this simply and carefully. β¨
see examples/updateZip.pl for an example of this technique. π¦
β° File creation time not set β°
Q: upon extracting files, I see that their modification (and access) times are set to the time in the Zip archive. However, their creation time is not set to the same time. why? π€
A: mostly because Perl doesn't give cross-platform access to creation time. indeed, many systems (like Unix) don't support such a concept. however, if yours does, you can easily set it. get the modification time from the member using lastModTime(). π°οΈ
π« Can't use Archive::Zip on gzip files π«
Q: can I use Archive::Zip to extract Unix gzip files? π₯Ί
A: No. π
there is a distinction between Unix gzip files, and Zip archives that also can use the gzip compression. π
depending on the format of the gzip file, you can use Compress::Raw::Zlib, or Archive::Tar to decompress it (and de-archive it in the case of Tar files). π§°
you can unzip PKZIP/WinZip/etc/ archives using Archive::Zip (that's what it's for) as long as any compressed members are compressed using Deflate compression. π
ποΈ Add a directory/tree to a Zip ποΈ
Q: how can I add a directory (or tree) full of files to a Zip? π³
A: you can use the Archive::Zip::addTree*() methods:
use Archive::Zip;
my $zip = Archive::Zip->new();
# add all readable files and directories below . as xyz/*
$zip->addTree( '.', 'xyz' );
# add all readable plain files below /abc as def/*
$zip->addTree( '/abc', 'def', sub { -f && -r } );
# add all .c files below /tmp as stuff/*
$zip->addTreeMatching( '/tmp', 'stuff', '\.c$' );
# add all .o files below /tmp as stuff/* if they aren't writable
$zip->addTreeMatching( '/tmp', 'stuff', '\.o$', sub { ! -w } );
# add all .so files below /tmp that are smaller than 200 bytes as stuff/*
$zip->addTreeMatching( '/tmp', 'stuff', '\.o$', sub { -s < 200 } );
# and write them into a file
$zip->writeToFileNamed('xxx.zip');
π€ Extract a directory/tree π€
Q: how can I extract some (or all) files from a Zip into a different directory? π
A: you can use the Archive::Zip::extractTree() method: ??? ||
# now extract the same files into /tmpx
$zip->extractTree( 'stuff', '/tmpx' );
π Update a directory/tree π
Q: how can I update a Zip from a directory tree, adding or replacing only the newer files? π±
A: you can use the Archive::Zip::updateTree() method that was added in version 1.09. π
β±οΈ Zip times might be off by 1 second β±οΈ
Q: it bothers me greatly that my file times are wrong by one second about half the time. why don't you do something about it? π€
A: get over it. this is a result of the Zip format storing times in DOS format, which has a resolution of only two seconds. π
π Zip times don't include time zone information π
Q: my file times don't respect time zones. what gives? π€¨
A: if this is important to you, please submit patches to read the various Extra Fields that encode times with time zones. I'm just using the DOS Date/Time, which doesn't have a time zone. π°οΈ
πͺ How do I make a self-extracting Zip πͺ
Q: I want to make a self-extracting Zip file. can I do this? π₯Ί
A: Yes. you can write a self-extracting archive stub (that is, a version of unzip) to the output filehandle that you pass to writeToFileHandle(). see examples/selfex.pl for how to write a self-extracting archive. β¨
however, you should understand that this will only work on one kind of platform (the one for which the stub was compiled). β οΈ
ποΈ How can I deal with Zips with prepended garbage (i.e. from Sircam) ποΈ
Q: how can I tell if a Zip has been damaged by adding garbage to the beginning or inside the file? π§
A: I added code for this for the Amavis virus scanner. you can query archives for their 'eocdOffset' property, which should be 0: π
if ($zip->eocdOffset > 0)
{ warn($zip->eocdOffset . " bytes of garbage at beginning or within Zip") }
when members are extracted, this offset will be used to adjust the start of the member if necessary. π
π Can't extract Shrunk files π
Q: I'm trying to extract a file out of a Zip produced by PKZIP, and keep getting this error message:
error: Unsupported compression combination: read 6, write 0
A: you can't uncompress this archive member. Archive::Zip only supports uncompressed members, and compressed members that are compressed using the compression supported by Compress::Raw::Zlib. that means only Deflated and Stored members. π§
your file is compressed using the Shrink format, which is not supported by Compress::Raw::Zlib. π«
you could, perhaps, use a command-line UnZip program (like the Info-Zip one) to extract this. π»
π Can't do decryption π
Q: how do I decrypt encrypted Zip members? π€«
A: with some other program or library. Archive::Zip doesn't support decryption, and probably never will (unless you write it). π
π©Ί How to test file integrity? π©Ί
Q: how can Archive::Zip test the validity of a Zip file? π§ͺ
A: if you try to decompress the file, the gzip streams will report errors if you have garbage. most of the time. π¬
if you try to open the file and a central directory structure can't be found, an error will be reported. π¨
when a file is being read, if we can't find a proper PK.. signature in the right places we report a format error. π§
if there is added garbage at the beginning of a Zip file (as inserted by some viruses), you can find out about it, but Archive::Zip will ignore it, and you can still use the archive. when it gets written back out the added stuff will be gone. π«§
there are two ready-to-use utilities in the examples directory that can be used to test file integrity, or that you can use as examples for your own code: π οΈ
examples/zipcheck.plshows how to use an attempted extraction to test a file.examples/ziptest.plshows how to test CRCs in a file.
π― Duplicate files in Zip? π―
Q: Archive::Zip let me put the same file in my Zip twice! why don't you prevent this? π΅
A: as far as I can tell, this is not disallowed by the Zip spec. if you think it's a bad idea, check for it yourself: π΅οΈ
$zip->addFile($someFile, $someName) unless $zip->memberNamed($someName);
I can even imagine cases where this might be useful (for instance, multiple versions of files). π
π File ownership/permissions/ACLS/etc π
Q: why doesn't Archive::Zip deal with file ownership, ACLs, etc.? π€·
A: there is no standard way to represent these in the Zip file format. if you want to send me code to properly handle the various extra fields that have been used to represent these through the years, I'll look at it. π
π§© I can't compile but ActiveState only has an old version of Archive::Zip π§©
Q: So I've only installed modules using ActiveState's PPM program and repository, but they have a much older version of Archive::Zip than is in CPAN. Will you send me a newer PPM? π₯Ί
A: Probably not, unless I get lots of extra time. But there's literally no reason you can't install the version from CPAN. Archive::Zip is pure Perl, so all you need is NMAKE, which you can get for free from Microsoft. See the FAQ in the ActiveState documentation for details on how to install CPAN modules. β¨
πΌοΈ My JPEGs (or MP3's) don't compress when I put them into Zips! π΅
Q: How come my JPEGs and MP3's don't compress much when I put them into Zips? π
A: Because they're already compressed, bestie. That's the whole tea. π
πͺ Under Windows, things lock up/get damaged π
Q: I'm using Windows. When I try to use Archive::Zip, my machine locks up/makes funny sounds/displays a BSOD/corrupts data. How can I fix this?
A: First, try the newest version of Compress::Raw::Zlib. I know of Windows-related problems prior to v1.14 of that library. π«Ά
π¦ Zip contents in a scalar π¦
Q: I want to read a Zip file from (or write one to) a scalar variable instead of a file. How can I do this?
A: Use IO::String and the readFromFileHandle() and writeToFileHandle() methods. See examples/readScalar.pl and examples/writeScalar.pl. π
π Reading from streams π
Q: How do I read from a stream (like for the Info-Zip funzip program)?
A: This is not currently supported, though writing to a stream is. π
Okay, that's the whole FAQ, bestie. This page is serving Archive::Zip::FAQ(3) energy from perl v5.42.0, dated 2020-03-13. Now go unzip something iconic. ππβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Archive-Zip 1.68-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms