Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract a single file from a ZIP archive using Perl's Archive::Zip?

Tags:

zip

perl

I have a zip file X and I'd like do extract a single file, located in x/x/x/file.txt. How do I do this using Archive::Zip and Perl?

like image 455
Lexicon Avatar asked Sep 28 '10 13:09

Lexicon


People also ask

How do I extract just one zip file?

Open File Explorer and find the zipped folder. To unzip the entire folder, right-click to select Extract All, and then follow the instructions. To unzip a single file or folder, double-click the zipped folder to open it. Then, drag or copy the item from the zipped folder to a new location.

Why can't I extract a zip file?

Zip files may refuse to open if they are not properly downloaded. Also, incomplete downloads occur when files get stuck due to issues like bad internet connection, inconsistency in network connection, all of which can cause transfer errors, affect your Zip files, and make them unable to open.


2 Answers

You can use the extractMember method:

extractMember( $memberOrName [, $extractedName ] )

Extract the given member, or match its name and extract it. Returns undef if member doesn't exist in this Zip. If optional second arg is given, use it as the name of the extracted member. Otherwise, the internal filename of the member is used as the name of the extracted file or directory. If you pass $extractedName, it should be in the local file system's format. All necessary directories will be created. Returns AZ_OK on success.

like image 133
codaddict Avatar answered Oct 12 '22 23:10

codaddict


See Archive::Zip::FAQ, "extract file(s) from a Zip". The current version of the example file is online at http://cpansearch.perl.org/src/ADAMK/Archive-Zip-1.30/examples/extract.pl.

like image 33
daxim Avatar answered Oct 12 '22 22:10

daxim