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
Solved

Unzip only files from zip file.. junk the paths

Posted on 2008-06-17
7
606 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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to strip .csv from file name 9 83
Perl output collect 7 67
batch script for automated email 12 120
Matching a random pattern with one common character 2 73
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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

840 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