Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

how to extract zip files to specified location

by Anonymous Monk
on Dec 06, 2007 at 10:42 UTC ( [id://655354]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

my $zip = Archive::Zip->new(); if($zip->read($file) == AZ_OK) { my @members = $zip->memberNames(); foreach my $element(@members) { $zip->extractMember($element,); } }else { die "failed to open $file"; }
this code wroking and extrcting file where i run
but how can i extract the files to specified location.
regards,
Jhon

Replies are listed 'Best First'.
Re: how to extract zip files to specified location
by Corion (Patriarch) on Dec 06, 2007 at 10:47 UTC

    Maybe you can help us improve the documentation and propose a change that would make it clearer how the ->extractMember method accepts an optional second parameter which is the name under which the extracted file is saved.

      Hold the sarcasm, I think he's actually asking how to extract a file to a specific directory, and not just with a different name. So the solution is not that trivial.

      I think indeed either extractMember or extractMemberWithoutPaths, where you first construct the full path for the new file, from the old member name.

      Actually, I think Archive::Zip could use an edit, where you could pass a path to a directory (either an existing directory or a path that ends in a slash/backslash/other directory separator), which would then serve as a root dir for the extraction. It currently doesn't do that, the optional path must include the file name.

      So, the OP needs to add some code constructing the new path name out of the member name, and use that as a second parameter to extractMember.

      You can retrieve the internal path for a member by using the snippet

      $name = $member->fileName();

      Do ZIP archives use absolute or relative paths? If it's relative path, it'll use the current directory as the root dir. In that case it's easy: just chdir to the directory that you want to use as the root for the decompressed archive.

Re: how to extract zip files to specified location
by Punitha (Priest) on Dec 06, 2007 at 12:47 UTC

    Hi try this,

    use strict; use Archive::Zip; my $destinationDirectory = 'C:\test'; #####Specify your destination di +rectry to unzip the file my $zip = Archive::Zip->new('C:\00000007.zip'); #####This is the sourc +e zip file path foreach my $member ($zip-> members){ my $extractName = $member->fileName; $member->extractToFileNamed("$destinationDirectory\\$extractName") +; }

    Punitha

Re: how to extract zip files to specified location
by graff (Chancellor) on Dec 06, 2007 at 13:39 UTC
    If your task is concerned only with extracting content from zip files (as opposed to creating them), you should look at Archive::Extract (as suggested in the Archive::Zip manual). The "extract" method of that other module accepts a "to => $target_path" parameter to control where the extracted data is stored.
Re: how to extract zip files to specified location
by ~~David~~ (Hermit) on Dec 06, 2007 at 23:48 UTC
    This way is easy:
    my $endpath = 'C:\blah\blah\; foreach (@members){ $zip->extractMember( $_, "$endpath\\$_"); }
    ~~David~~

      This works for me even when archive contains subdirs (e.g. META-INF/manifest.xml)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://655354]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 22:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found