Solved

Unzip only files from zip file.. junk the paths

Posted on 2008-06-17
7
599 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
  • 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now