📄 archive::zip::faq.3
字号:
.IX Header "Can't Read/modify/write same Zip file"\&\fBQ:\fR Why can't I open a Zip file, add a member, and write it back? I get anerror message when I try..PP\&\fBA:\fR 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 newfile is completed..PPThe best way to do this is to write the Zip to a temporary file and thenrename the temporary file to have the old name (possibly after deleting theold one)..PPArchive::Zip v1.02 added the archive methods \f(CW\*(C`overwrite()\*(C'\fR and\&\f(CW\*(C`overwriteAs()\*(C'\fR to do this simply and carefully..PPSee \f(CW\*(C`examples/updateZip.pl\*(C'\fR for an example of this technique..SH "File creation time not set".IX Header "File creation time not set"\&\fBQ:\fR Upon extracting files, I see that their modification (and access) times areset to the time in the Zip archive. However, their creation time is not set tothe same time. Why?.PP\&\fBA:\fR Mostly because Perl doesn't give cross-platform access to \fIcreation time\fR.Indeed, many systems (like Unix) don't support such a concept.However, if yours does, you can easily set it. Get the modification time fromthe member using \f(CW\*(C`lastModTime()\*(C'\fR..SH "Can't use Archive::Zip on gzip files".IX Header "Can't use Archive::Zip on gzip files"\&\fBQ:\fR Can I use Archive::Zip to extract Unix gzip files?.PP\&\fBA:\fR No..PPThere is a distinction between Unix gzip files, and Zip archives that also can use the gzip compression..PPDepending on the format of the gzip file, you can use Compress::Zlib, orArchive::Tar to decompress it (and de-archive it in the case of Tar files)..PPYou 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..SH "Add a directory/tree to a Zip".IX Header "Add a directory/tree to a Zip"\&\fBQ:\fR How can I add a directory (or tree) full of files to a Zip?.PP\&\fBA:\fR You can use the Archive::Zip::addTree*() methods:.PP.Vb 10\& use Archive::Zip;\& my $zip = Archive::Zip\->new();\& # add all readable files and directories below . as xyz/*\& $zip\->addTree( \*(Aq.\*(Aq, \*(Aqxyz\*(Aq ); \& # add all readable plain files below /abc as def/*\& $zip\->addTree( \*(Aq/abc\*(Aq, \*(Aqdef\*(Aq, sub { \-f && \-r } ); \& # add all .c files below /tmp as stuff/*\& $zip\->addTreeMatching( \*(Aq/tmp\*(Aq, \*(Aqstuff\*(Aq, \*(Aq\e.c$\*(Aq );\& # add all .o files below /tmp as stuff/* if they aren\*(Aqt writable\& $zip\->addTreeMatching( \*(Aq/tmp\*(Aq, \*(Aqstuff\*(Aq, \*(Aq\e.o$\*(Aq, sub { ! \-w } );\& # add all .so files below /tmp that are smaller than 200 bytes as stuff/*\& $zip\->addTreeMatching( \*(Aq/tmp\*(Aq, \*(Aqstuff\*(Aq, \*(Aq\e.o$\*(Aq, sub { \-s < 200 } );\& # and write them into a file\& $zip\->writeToFileNamed(\*(Aqxxx.zip\*(Aq);.Ve.SH "Extract a directory/tree".IX Header "Extract a directory/tree"\&\fBQ:\fR How can I extract some (or all) files from a Zip into a differentdirectory?.PP\&\fBA:\fR You can use the \fIArchive::Zip::extractTree()\fR method:??? ||.PP.Vb 2\& # now extract the same files into /tmpx\& $zip\->extractTree( \*(Aqstuff\*(Aq, \*(Aq/tmpx\*(Aq );.Ve.SH "Update a directory/tree".IX Header "Update a directory/tree"\&\fBQ:\fR How can I update a Zip from a directory tree, adding or replacing onlythe newer files?.PP\&\fBA:\fR You can use the \fIArchive::Zip::updateTree()\fR method that was added in version 1.09..SH "Zip times might be off by 1 second".IX Header "Zip times might be off by 1 second"\&\fBQ:\fR It bothers me greatly that my file times are wrong by one second about halfthe time. Why don't you do something about it?.PP\&\fBA:\fR Get over it. This is a result of the Zip format storing times in \s-1DOS\s0format, which has a resolution of only two seconds..SH "Zip times don't include time zone information".IX Header "Zip times don't include time zone information"\&\fBQ:\fR My file times don't respect time zones. What gives?.PP\&\fBA:\fR If this is important to you, please submit patches to read the variousExtra Fields that encode times with time zones. I'm just using the \s-1DOS\s0Date/Time, which doesn't have a time zone..SH "How do I make a self-extracting Zip".IX Header "How do I make a self-extracting Zip"\&\fBQ:\fR I want to make a self-extracting Zip file. Can I do this?.PP\&\fBA:\fR Yes. You can write a self-extracting archive stub (that is, a version ofunzip) to the output filehandle that you pass to \fIwriteToFileHandle()\fR. Seeexamples/selfex.pl for how to write a self-extracting archive..PPHowever, you should understand that this will only work on one kind ofplatform (the one for which the stub was compiled)..SH "How can I deal with Zips with prepended garbage (i.e. from Sircam)".IX Header "How can I deal with Zips with prepended garbage (i.e. from Sircam)"\&\fBQ:\fR How can I tell if a Zip has been damaged by adding garbage to thebeginning or inside the file?.PP\&\fBA:\fR I added code for this for the Amavis virus scanner. You can query archivesfor their 'eocdOffset' property, which should be 0:.PP.Vb 2\& if ($zip\->eocdOffset > 0)\& { warn($zip\->eocdOffset . " bytes of garbage at beginning or within Zip") }.Ve.PPWhen members are extracted, this offset will be used to adjust the start ofthe member if necessary..SH "Can't extract Shrunk files".IX Header "Can't extract Shrunk files"\&\fBQ:\fR I'm trying to extract a file out of a Zip produced by \s-1PKZIP\s0, and keepgetting this error message:.PP.Vb 1\& error: Unsupported compression combination: read 6, write 0.Ve.PP\&\fBA:\fR You can't uncompress this archive member. Archive::Zip only supports uncompressedmembers, and compressed members that are compressed using the compressionsupported by Compress::Zlib. That means only Deflated and Stored members..PPYour file is compressed using the Shrink format, which isn't supported byCompress::Zlib..PPYou could, perhaps, use a command-line UnZip program (like the Info-Zipone) to extract this..SH "Can't do decryption".IX Header "Can't do decryption"\&\fBQ:\fR How do I decrypt encrypted Zip members?.PP\&\fBA:\fR With some other program or library. Archive::Zip doesn't support decryption,and probably never will (unless \fIyou\fR write it)..SH "How to test file integrity?".IX Header "How to test file integrity?"\&\fBQ:\fR How can Archive::Zip can test the validity of a Zip file?.PP\&\fBA:\fR If you try to decompress the file, the gzip streams will report errors if you have garbage. Most of the time..PPIf you try to open the file and a central directory structure can't be found, an error will be reported..PPWhen a file is being read, if we can't find a proper \s-1PK\s0.. signature in the right places we report a format error..PPIf 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..PPThere are two ready-to-use utilities in the examples directory that canbe used to test file integrity, or that you can use as examplesfor your own code:.IP "examples/zipcheck.pl shows how to use an attempted extraction to test a file." 4.IX Item "examples/zipcheck.pl shows how to use an attempted extraction to test a file.".PD 0.IP "examples/ziptest.pl shows how to test CRCs in a file." 4.IX Item "examples/ziptest.pl shows how to test CRCs in a file.".PD.SH "Duplicate files in Zip?".IX Header "Duplicate files in Zip?"\&\fBQ:\fR Archive::Zip let me put the same file in my Zip twice! Why don't you prevent this?.PP\&\fBA:\fR As far as I can tell, this is not disallowed by the Zip spec. If youthink it's a bad idea, check for it yourself:.PP.Vb 1\& $zip\->addFile($someFile, $someName) unless $zip\->memberNamed($someName);.Ve.PPI can even imagine cases where this might be useful (for instance, multipleversions of files)..SH "File ownership/permissions/ACLS/etc".IX Header "File ownership/permissions/ACLS/etc"\&\fBQ:\fR Why doesn't Archive::Zip deal with file ownership, ACLs, etc.?.PP\&\fBA:\fR There is no standard way to represent these in the Zip file format. Ifyou want to send me code to properly handle the various extra fields thathave been used to represent these through the years, I'll look at it..SH "I can't compile but ActiveState only has an old version of Archive::Zip".IX Header "I can't compile but ActiveState only has an old version of Archive::Zip"\&\fBQ:\fR I've only installed modules using ActiveState's \s-1PPM\s0 program andrepository. But they have a much older version of Archive::Zip than is in \s-1CPAN\s0. Willyou send me a newer \s-1PPM\s0?.PP\&\fBA:\fR Probably not, unless I get lots of extra time. But there's no reason youcan't install the version from \s-1CPAN\s0. Archive::Zip is pure Perl, so all you need is\&\s-1NMAKE\s0, which you can get for free from Microsoft (see the \s-1FAQ\s0 in theActiveState documentation for details on how to install \s-1CPAN\s0 modules)..SH "My JPEGs (or MP3's) don't compress when I put them into Zips!".IX Header "My JPEGs (or MP3's) don't compress when I put them into Zips!"\&\fBQ:\fR How come my JPEGs and \s-1MP3\s0's don't compress much when I put them into Zips?.PP\&\fBA:\fR Because they're already compressed..SH "Under Windows, things lock up/get damaged".IX Header "Under Windows, things lock up/get damaged"\&\fBQ:\fR I'm using Windows. When I try to use Archive::Zip, my machine locks up/makesfunny sounds/displays a BSOD/corrupts data. How can I fix this?.PP\&\fBA:\fR First, try the newest version of Compress::Zlib. I know ofWindows-related problems prior to v1.14 of that library..PPIf that doesn't get rid of the problem, fix your computer or get rid ofWindows..SH "Zip contents in a scalar".IX Header "Zip contents in a scalar"\&\fBQ:\fR I want to read a Zip file from (or write one to) a scalar variable insteadof a file. How can I do this?.PP\&\fBA:\fR Use \f(CW\*(C`IO::String\*(C'\fR and the \f(CW\*(C`readFromFileHandle()\*(C'\fR and\&\f(CW\*(C`writeToFileHandle()\*(C'\fR methods.See \f(CW\*(C`examples/readScalar.pl\*(C'\fR and \f(CW\*(C`examples/writeScalar.pl\*(C'\fR..SH "Reading from streams".IX Header "Reading from streams"\&\fBQ:\fR How do I read from a stream (like for the Info-Zip \f(CW\*(C`funzip\*(C'\fR program)?.PP\&\fBA:\fR This isn't currently supported, though writing to a stream is.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -