Link to home
Create AccountLog in
Avatar of Steve Tempest
Steve TempestFlag for Australia

asked on

Unzip only files from zip file.. junk the paths

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
Avatar of Suhas .
Suhas .
Flag of United States of America image

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

Regards,
suhasbharadwaj
Avatar of Adam314
Adam314

You could use the extractMemberWithoutPaths function.  What are you doing now?
Avatar of Steve Tempest

ASKER

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


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

Open in new window

Is it possible to pass the destination using this method?
also will this overwrite existing files without failing which is what the -o is doing in the system call
ASKER CERTIFIED SOLUTION
Avatar of Adam314
Adam314

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer