Solved

Unzip only files from zip file.. junk the paths

Posted on 2008-06-17
7
615 Views
Last Modified: 2012-06-27
Hi,

I have some code using Archive::Zip that will extract the data from a zip file into a destination folder. I'm not able to work out how not to keep the directory structure of the zip and only extract the files.

I would like to do something simulart to

unzip -o -j /zipfile.zip

I am not able to get Archive::Zip to junk the paths in the zip file.

Thanks

Steve
0
Comment
Question by:APNFSSC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 9

Expert Comment

by:Suhas .
ID: 21801870
Hello Steve,
you can use system("unzip \-o \-j \/zipfile\.zip");

Regards,
suhasbharadwaj
0
 
LVL 39

Expert Comment

by:Adam314
ID: 21803247
You could use the extractMemberWithoutPaths function.  What are you doing now?
0
 
LVL 10

Author Comment

by:APNFSSC
ID: 21808192
Hi,

Thanks for the replies.

Here is what I have now which works thanks suhasbharadwaj.

I would prefer not to break out to a shell to perform this operation so I'm leaving this item open for 1 more day to see if extractMemberWithoutPaths is possible.

I had a go at it and could not get it working with extractMemberWithoutPaths but if someone can provide a code snippit that would be great.

Thanks

Steve
sub unzip_this_junk_paths{
        my ($zipfile, $destination, $error_path) = @_;
        my $zip = Archive::Zip->new();
        unless ( $zip->read( "$zipfile" ) == AZ_OK ) {
            move("$zipfile", "$error_path/.") and die;
    }
        system("unzip \-o \-j \-qq $zipfile -d $destination");
}

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Expert Comment

by:Adam314
ID: 21808419

use Archive::Zip;
 
my $zip = Archive::Zip->new( 'test1.zip' );
$zip->extractMemberWithoutPaths($_) foreach ($zip->members);

Open in new window

0
 
LVL 10

Author Comment

by:APNFSSC
ID: 21808441
Is it possible to pass the destination using this method?
0
 
LVL 10

Author Comment

by:APNFSSC
ID: 21808457
also will this overwrite existing files without failing which is what the -o is doing in the system call
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 125 total points
ID: 21813293
Files will be overwritten.  To change the output directory, use chdir before extracting.  Use either a relative or full path to the zip file.
chdir('zipout') or die "chdir: $!\n";
my $zip = Archive::Zip->new();
($zip->read('../test1.zip')==AZ_OK) or die "Read error\n";
$zip->extractMemberWithoutPaths($_) foreach ($zip->members);

Open in new window

0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question