Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: unzip fail using IO::Uncompress::Unzip

by keienn (Novice)
on Sep 27, 2016 at 09:01 UTC ( [id://1172731]=note: print w/replies, xml ) Need Help??


in reply to Re: unzip fail using IO::Uncompress::Unzip
in thread unzip fail using IO::Uncompress::Unzip

Thanks great monks! I have tried Marshalls construct and successfully removed the "." and ".." directories. the code now looks as below



opendir(ZIPPED, $zipdir)or die "couldn't open $zipdir: $!\n"; my @zd = grep {!-d "$zipdir/$_"}readdir(ZIPPED); opendir(UNZIPPED, $unzipdir)or die "couldn't open $unzipdir: $!\n"; my @uzd = readdir(UNZIPPED); foreach(@zd) { next unless defined $_; my $inf = \@zd; my $outf = \@uzd; #print $_,"\n"; unzip $inf => $outf or die "unzip failed: $UnzipError \n"; }

I am still unable to unzip, the error I get is as below
Uncaught exception from user code:
unzip failed: input file '730372_Atoll.zip' does not exist
That is the first file in the folder.
And Marshall, the 'my @uzd; my $outf = $uzd@uzd' was intended to be an assignment of the unzipped files to an array,
but i revised as shown in the code above.
So monks, what should i improve on? Thanks in advance!

Replies are listed 'Best First'.
Re^3: unzip fail using IO::Uncompress::Unzip
by Marshall (Canon) on Sep 29, 2016 at 19:37 UTC
    "unzip failed: input file '730372_Atoll.zip' does not exist"
    That looks like a directory problem. Remember that readdir only returns the file name, not the a complete path to the file. You need "$zipdir/730372_Atoll.zip" otherwise the .zip file is expected in the current directory wherever the Perl script is running. You could add the full file path instead of just the file names from readdir into @zd array by using map:
    my @zd = grep {!-d}map{"$zipdir/$_"}readdir(ZIPPED);

    I looked at IO::Uncompress::Unzip I suspect that you are using the wrong tool. If you unzip $inref => $outref and my $inref = \@zd;, that is fine. However, your assignment of filenames to the output arrary ref is meaningless. see Notes:

    When $input_filename_or_reference maps to multiple compressed files/buffers and $output_filename_or_reference is a single file/buffer, after uncompression $output_filename_or_reference will contain a concatenation of all the uncompressed data from each of the input files/buffers.
    You will just get a bunch of concatenated junk in @uzd.

    Try reading Archive::Extract and look at Re: Archive::Zip for unzipping to directory?

    Try writing some code that just extracts one .zip file using a hard coded name and puts the contents where you want it, then work on making a loop with varying dir contents.

Re^3: unzip fail using IO::Uncompress::Unzip
by Anonymous Monk on Sep 27, 2016 at 09:35 UTC

    readdir is raw eggs, what you want is cake

    #!/usr/bin/perl -- use strict; use warnings; ## Main( @ARGV ); Main( "zipdir", "unzipdir" ); exit( 0 ); sub Main { my( $zipdir, $unzipdir ) = @_; my @zipped = GetZipFiles( $zipdir ); my @unzipped = GetZipFiles( $unzipdir ); if( @zipped == @unzipped ){ MatchBanana( \@zipped, \@unzipped ); } else { die "zipped number not equal to unzipped number, bye!\n"; } } sub MatchBanana { my( $zip, $unzip ) = @_; for my $ix ( 0 .. $#{$zip} ){ my $inf = $zip->[$ix]; my $outf = $unzip->[$ix]; unzip $inf, $outf or die "unzip failed: $UnzipError \n"; } } sub GetZipFiles { use Path::Tiny qw/ path /; my( $zipdir ) = @_; my @zipfiles = path( $zipdir )->children( qr/\.zip$/i ); return @zipfiles; }

    Even this program has problems, the zipfiles arent sorted , whos to say the infile really go with the outfiles

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1172731]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-24 21:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found