📄 archive::zip.3
字号:
\& my $status = $zip\->writeToFileNamed( \*(Aqxx.zip\*(Aq );\& die "error somewhere" if $status != AZ_OK;.Ve.SpNote that if you use the same name as an existing zip filethat you read in, you will clobber ZipFileMembers. Soinstead, write to a different file name, then delete theoriginal.If you use the \f(CW\*(C`overwrite()\*(C'\fR or \f(CW\*(C`overwriteAs()\*(C'\fR methods, you canre-write the original zip in this way.\&\f(CW$fileName\fR should be a valid file name on your system..ie n .IP "writeToFileHandle( $fileHandle\fR [, \f(CW$seekable] )" 4.el .IP "writeToFileHandle( \f(CW$fileHandle\fR [, \f(CW$seekable\fR] )" 4.IX Item "writeToFileHandle( $fileHandle [, $seekable] )"Write a zip archive to a file handle. Return \s-1AZ_OK\s0 onsuccess. The optional second arg tells whether or not to tryto seek backwards to re-write headers. If not provided, it isset if the Perl \f(CW\*(C`\-f\*(C'\fR test returns true. This could fail onsome operating systems, though..Sp.Vb 4\& my $fh = IO::File\->new( \*(AqsomeFile.zip\*(Aq, \*(Aqw\*(Aq );\& unless ( $zip\->writeToFileHandle( $fh ) != AZ_OK ) {\& # error handling\& }.Ve.SpIf you pass a file handle that is not seekable (like ifyou're writing to a pipe or a socket), pass a false secondargument:.Sp.Vb 2\& my $fh = IO::File\->new( \*(Aq| cat > somefile.zip\*(Aq, \*(Aqw\*(Aq );\& $zip\->writeToFileHandle( $fh, 0 ); # fh is not seekable.Ve.SpIf this method fails during the write of a member, thatmember and all following it will return false from\&\f(CW\*(C`wasWritten()\*(C'\fR. See \fIwriteCentralDirectory()\fR for a way todeal with this.If you want, you can write data to the file handle beforepassing it to \fIwriteToFileHandle()\fR; this could be used (forinstance) for making self-extracting archives. However, thisonly works reliably when writing to a real file (as opposedto \s-1STDOUT\s0 or some other possible non-file)..SpSee examples/selfex.pl for how to write a self-extractingarchive..ie n .IP "writeCentralDirectory( $fileHandle\fR [, \f(CW$offset ] )" 4.el .IP "writeCentralDirectory( \f(CW$fileHandle\fR [, \f(CW$offset\fR ] )" 4.IX Item "writeCentralDirectory( $fileHandle [, $offset ] )"Writes the central directory structure to the given filehandle..SpReturns \s-1AZ_OK\s0 on success. If given an \f(CW$offset\fR, willseek to that point before writing. This can be used forrecovery in cases where writeToFileHandle or writeToFileNamedreturns an \s-1IO\s0 error because of running out of space on thedestination file..SpYou can truncate the zip by seeking backwards and then writing thedirectory:.Sp.Vb 10\& my $fh = IO::File\->new( \*(AqsomeFile.zip\*(Aq, \*(Aqw\*(Aq );\& my $retval = $zip\->writeToFileHandle( $fh );\& if ( $retval == AZ_IO_ERROR ) {\& my @unwritten = grep { not $_\->wasWritten() } $zip\->members();\& if (@unwritten) {\& $zip\->removeMember( $member ) foreach my $member ( @unwritten );\& $zip\->writeCentralDirectory( $fh,\& $unwritten[0]\->writeLocalHeaderRelativeOffset());\& }\& }.Ve.ie n .IP "overwriteAs( $newName )" 4.el .IP "overwriteAs( \f(CW$newName\fR )" 4.IX Item "overwriteAs( $newName )"Write the zip to the specified file, as safely as possible.This is done by first writing to a temp file, then renamingthe original if it exists, then renaming the temp file, thendeleting the renamed original if it exists. Returns \s-1AZ_OK\s0 ifsuccessful..IP "\fIoverwrite()\fR" 4.IX Item "overwrite()"Write back to the original zip file. See \fIoverwriteAs()\fR above.If the zip was not ever read from a file, this generates anerror..ie n .IP "read( $fileName )" 4.el .IP "read( \f(CW$fileName\fR )" 4.IX Item "read( $fileName )"Read zipfile headers from a zip file, appending new members.Returns \f(CW\*(C`AZ_OK\*(C'\fR or error code..Sp.Vb 2\& my $zipFile = Archive::Zip\->new();\& my $status = $zipFile\->read( \*(Aq/some/FileName.zip\*(Aq );.Ve.ie n .IP "readFromFileHandle( $fileHandle\fR, \f(CW$filename )" 4.el .IP "readFromFileHandle( \f(CW$fileHandle\fR, \f(CW$filename\fR )" 4.IX Item "readFromFileHandle( $fileHandle, $filename )"Read zipfile headers from an already-opened file handle,appending new members. Does not close the file handle.Returns \f(CW\*(C`AZ_OK\*(C'\fR or error code. Note that this requires aseekable file handle; reading from a stream is not yetsupported..Sp.Vb 5\& my $fh = IO::File\->new( \*(Aq/some/FileName.zip\*(Aq, \*(Aqr\*(Aq );\& my $zip1 = Archive::Zip\->new();\& my $status = $zip1\->readFromFileHandle( $fh );\& my $zip2 = Archive::Zip\->new();\& $status = $zip2\->readFromFileHandle( $fh );.Ve.Sh "Zip Archive Tree operations".IX Subsection "Zip Archive Tree operations"These used to be in Archive::Zip::Tree but got moved intoArchive::Zip. They enable operation on an entire tree of members orfiles.A usage example:.PP.Vb 2\& 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);\& \& # now extract the same files into /tmpx\& $zip\->extractTree( \*(Aqstuff\*(Aq, \*(Aq/tmpx\*(Aq );.Ve.ie n .IP "$zip\fR\->addTree( \f(CW$root\fR, \f(CW$dest [,$pred] ) \*(-- Add tree of files to a zip" 4.el .IP "\f(CW$zip\fR\->addTree( \f(CW$root\fR, \f(CW$dest\fR [,$pred] ) \*(-- Add tree of files to a zip" 4.IX Item "$zip->addTree( $root, $dest [,$pred] ) Add tree of files to a zip"\&\f(CW$root\fR is the root of the tree of files and directories to beadded. It is a valid directory name on your system. \f(CW$dest\fR isthe name for the root in the zip file (undef or blank meansto use relative pathnames). It is a valid \s-1ZIP\s0 directory name(that is, it uses forward slashes (/) for separatingdirectory components). \f(CW$pred\fR is an optional subroutinereference to select files: it is passed the name of theprospective file or directory using \f(CW$_\fR, and if it returnstrue, the file or directory will be included. The default isto add all readable files and directories. For instance,using.Sp.Vb 2\& my $pred = sub { /\e.txt/ };\& $zip\->addTree( \*(Aq.\*(Aq, \*(Aq\*(Aq, $pred );.Ve.Spwill add all the .txt files in and below the currentdirectory, using relative names, and making the namesidentical in the zipfile:.Sp.Vb 4\& original name zip member name\& ./xyz xyz\& ./a/ a/\& ./a/b a/b.Ve.SpTo translate absolute to relative pathnames, just pass themin: \f(CW$zip\fR\->addTree( '/c/d', 'a' );.Sp.Vb 4\& original name zip member name\& /c/d/xyz a/xyz\& /c/d/a/ a/a/\& /c/d/a/b a/a/b.Ve.SpReturns \s-1AZ_OK\s0 on success. Note that this will not followsymbolic links to directories. Note also that this does notcheck for the validity of filenames..SpNote that you generally \fIdon't\fR want to make zip archive member namesabsolute..ie n .IP "$zip\fR\->addTreeMatching( \f(CW$root\fR, \f(CW$dest\fR, \f(CW$pattern [,$pred] )" 4.el .IP "\f(CW$zip\fR\->addTreeMatching( \f(CW$root\fR, \f(CW$dest\fR, \f(CW$pattern\fR [,$pred] )" 4.IX Item "$zip->addTreeMatching( $root, $dest, $pattern [,$pred] )"\&\f(CW$root\fR is the root of the tree of files and directories to beadded \f(CW$dest\fR is the name for the root in the zip file (undefmeans to use relative pathnames) \f(CW$pattern\fR is a (non-anchored)regular expression for filenames to match \f(CW$pred\fR is anoptional subroutine reference to select files: it is passedthe name of the prospective file or directory in \f(CW$_\fR, andif it returns true, the file or directory will be included.The default is to add all readable files and directories. Toadd all files in and below the current dirctory whose namesend in \f(CW\*(C`.pl\*(C'\fR, and make them extract into a subdirectorynamed \f(CW\*(C`xyz\*(C'\fR, do this:.Sp.Vb 1\& $zip\->addTreeMatching( \*(Aq.\*(Aq, \*(Aqxyz\*(Aq, \*(Aq\e.pl$\*(Aq ).Ve.SpTo add all \fIwritable\fR files in and below the dirctory named\&\f(CW\*(C`/abc\*(C'\fR whose names end in \f(CW\*(C`.pl\*(C'\fR, and make them extract intoa subdirectory named \f(CW\*(C`xyz\*(C'\fR, do this:.Sp.Vb 1\& $zip\->addTreeMatching( \*(Aq/abc\*(Aq, \*(Aqxyz\*(Aq, \*(Aq\e.pl$\*(Aq, sub { \-w } ).Ve.SpReturns \s-1AZ_OK\s0 on success. Note that this will not followsymbolic links to directories..ie n .IP "$zip\fR\->updateTree( \f(CW$root\fR, [ \f(CW$dest\fR, [ \f(CW$pred\fR [, \f(CW$mirror]]] );" 4.el .IP "\f(CW$zip\fR\->updateTree( \f(CW$root\fR, [ \f(CW$dest\fR, [ \f(CW$pred\fR [, \f(CW$mirror\fR]]] );" 4.IX Item "$zip->updateTree( $root, [ $dest, [ $pred [, $mirror]]] );"Update a zip file from a directory tree..Sp\&\f(CW\*(C`updateTree()\*(C'\fR takes the same arguments as \f(CW\*(C`addTree()\*(C'\fR, but firstchecks to see whether the file or directory already exists in the zipfile, and whether it has been changed..SpIf the fourth argument \f(CW$mirror\fR is true, then delete all my membersif corresponding files weren't found..SpReturns an error code or \s-1AZ_OK\s0 if all is well..ie n .IP "$zip\fR\->\fIextractTree()" 4.el .IP "\f(CW$zip\fR\->\fIextractTree()\fR" 4.IX Item "$zip->extractTree()".PD 0.ie n .IP "$zip\fR\->extractTree( \f(CW$root )" 4.el .IP "\f(CW$zip\fR\->extractTree( \f(CW$root\fR )" 4.IX Item "$zip->extractTree( $root )".ie n .IP "$zip\fR\->extractTree( \f(CW$root\fR, \f(CW$dest )" 4.el .IP "\f(CW$zip\fR\->extractTree( \f(CW$root\fR, \f(CW$dest\fR )" 4.IX Item "$zip->extractTree( $root, $dest )".ie n .IP "$zip\fR\->extractTree( \f(CW$root\fR, \f(CW$dest\fR, \f(CW$volume )" 4.el .IP "\f(CW$zip\fR\->extractTree( \f(CW$root\fR, \f(CW$dest\fR, \f(CW$volume\fR )" 4.IX Item "$zip->extractTree( $root, $dest, $volume )".PDIf you don't give any arguments at all, will extract all thefiles in the zip with their original names..SpIf you supply one argument for \f(CW$root\fR, \f(CW\*(C`extractTree\*(C'\fR will extractall the members whose names start with \f(CW$root\fR into the currentdirectory, stripping off \f(CW$root\fR first.\&\f(CW$root\fR is in Zip (Unix) format.For instance,.Sp.Vb 1\& $zip\->extractTree( \*(Aqa\*(Aq );.Ve.Spwhen applied to a zip containing the files:a/x a/b/c ax/d/e d/e will extract:.Spa/x as ./x.Spa/b/c as ./b/c.SpIf you give two arguments, \f(CW\*(C`extractTree\*(C'\fR extracts all the memberswhose names start with \f(CW$root\fR. It will translate \f(CW$root\fR into\&\f(CW$dest\fR to construct the destination file name.\&\f(CW$root\fR and \f(CW$dest\fR are in Zip (Unix) format.For instance,.Sp.Vb 1\& $zip\->extractTree( \*(Aqa\*(Aq, \*(Aqd/e\*(Aq );.Ve.Spwhen applied to a zip containing the files:a/x a/b/c ax/d/e d/e will extract:.Spa/x to d/e/x.Spa/b/c to d/e/b/c and ignore ax/d/e and d/e.SpIf you give three arguments, \f(CW\*(C`extractTree\*(C'\fR extracts all the memberswhose names start with \f(CW$root\fR. It will translate \f(CW$root\fR into\&\f(CW$dest\fR to construct the destination file name, and then it willconvert to local file system format, using \f(CW$volume\fR as the name ofthe destination volume..Sp\&\f(CW$root\fR and \f(CW$dest\fR are in Zip (Unix) format..Sp\&\f(CW$volume\fR is in local file system format..SpFor instance, under Windows,.Sp.Vb 1\& $zip\->extractTree( \*(Aqa\*(Aq, \*(Aqd/e\*(Aq, \*(Aqf:\*(Aq );.Ve.Spwhen applied to a zip containing the files:a/x a/b/c ax/d/e d/e will extract:.Spa/x to f:d/e/x.Spa/b/c to f:d/e/b/c and ignore ax/d/e and d/e.SpIf you want absolute paths (the prior example used paths relative tothe current directory on the destination volume, you can specify thesein \f(CW$dest\fR:.Sp.Vb 1\& $zip\->extractTree( \*(Aqa\*(Aq, \*(Aq/d/e\*(Aq, \*(Aqf:\*(Aq );.Ve.Spwhen applied to a zip containing the files:a/x a/b/c ax/d/e d/e will extract:.Spa/x to f:\ed\ee\ex.Spa/b/c to f:\ed\ee\eb\ec and ignore ax/d/e and d/e.SpReturns an error code or \s-1AZ_OK\s0 if everything worked \s-1OK\s0..SH "MEMBER OPERATIONS".IX Header "MEMBER OPERATIONS".Sh "Member Class Methods".IX Subsection "Member Class Methods"Several constructors allow you to construct members without addingthem to a zip archive. These work the same as the \fIaddFile()\fR,\&\fIaddDirectory()\fR, and \fIaddString()\fR zip instance methods described above,but they don't add the new members to a zip..ie n .IP "Archive::Zip::Member\->newFromString( $stringOrStringRef\fR [, \f(CW$fileName] )" 4.el .IP "Archive::Zip::Member\->newFromString( \f(CW$stringOrStringRef\fR [, \f(CW$fileName\fR] )" 4.IX Item "Archive::Zip::Member->newFromString( $stringOrStringRef [, $fileName] )"Construct a new member from the given string. Returns undefon error..Sp.Vb 2\& my $member = Archive::Zip::Member\->newFromString( \*(AqThis is a test\*(Aq,\& \*(Aqxyz.txt\*(Aq );.Ve.ie n .IP "newFromFile( $fileName )" 4.el .IP "newFromFile( \f(CW$fileName\fR )" 4.IX Item "newFromFile( $fileName )"Construct a new member from the given file. Returns undef onerror..Sp.Vb 1\& my $member = Archive::Zip::Member\->newFromFile( \*(Aqxyz.txt\*(Aq );.Ve.ie n .IP "newDirectoryNamed( $directoryName\fR [, \f(CW$zipname ] )" 4.el .IP "newDirectoryNamed( \f(CW$directoryName\fR [, \f(CW$zipname\fR ] )" 4.IX Item "newDirectoryNamed( $directoryName [, $zipname ] )"Construct a new member from the given directory.\&\f(CW$directoryName\fR must be a valid name on your file system; it doesn'thave to exist..SpIf given, \f(CW$zipname\fR will be the name of the zip member; it must be avalid Zip (Unix) name. If not given, it will be converted from\&\f(CW$directoryName\fR..SpReturns undef on error..Sp.Vb 1\& my $member = Archive::Zip::Member\->newDirectoryNamed( \*(AqCVS/\*(Aq );.Ve.Sh "Member Simple accessors".IX Subsection "Member Simple accessors"These methods get (and/or set) member attribute values..IP "\fIversionMadeBy()\fR" 4.IX Item "versionMadeBy()"Gets the field from the member header..IP "fileAttributeFormat( [$format] )" 4.IX Item "fileAttributeFormat( [$format] )"Gets or sets the field from the member header. These are
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -