Solved

Unzip only files from zip file.. junk the paths

Posted on 2008-06-17
7
610 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Filtering a file to table 9 104
Is it possible to get a piece of info in a line from a big text file with perl? 7 76
syslog unix file 20 80
Perl Untar File 1 78
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…
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…

751 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