Solved

Unzip only files from zip file.. junk the paths

Posted on 2008-06-17
7
604 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PERL export multiple query results to a JSON file 1 167
Check file date before getting folder path 1 74
How to get all the API from website? 11 92
perl syntax 3 4
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

803 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