andr3w77 has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I am fairly new to perl and its fair to say I am not a developer by profession. I am having an issue and can not fanthom how to resolve or workaround...

on my Linux box I have zip files and want to find out the contents. for example;

[myuser@dev2 ~]# file /tmp/test.zip /tmp/test.zip: Zip archive data, at least v2.0 to extract [myuser@dev2 ~]# unzip -l /tmp/test.zip Archive: /tmp/test.zip Length Date Time Name -------- ---- ---- ---- 19855954 08-24-10 17:06 RGB_DSC08815.jpg 11230988 08-24-10 17:05 RGB_fly43337.jpg 13285985 08-24-10 17:04 RGB_fly48151.jpg 10024136 08-24-10 17:04 RGB_fly48168.jpg 9236336 08-24-10 17:04 RGB_fly54598.jpg 7750448 08-24-10 17:03 RGB_fly57173.jpg 8870833 08-24-10 17:03 RGB_MHowell142.jpg 10178959 08-24-10 17:06 RGB_MHowell181.jpg 1151658 08-24-10 17:16 RGB_Ducky_HARD.eps 14741504 09-07-10 17:08 RGB_TEST.indd -------- ------- 106326801 10 files

but when I use the perl code below I get the error.

[myuser@dev2 ~]# ./showZipContents.pl RGB_DSC08815.jpg RGB_fly43337.jpg Error processing /tmp/test.zip:

Here i my code so far.

#!/usr/bin/perl -w use strict; use warnings; use IO::Uncompress::Unzip qw(unzip $UnzipError); my $zipfile = "/tmp/test.zip"; my $u = new IO::Uncompress::Unzip $zipfile or die "Cannot open $zipfile: $UnzipError"; my $status; for ($status = 1; ! $u->eof(); $status = $u->nextStream()) { my $filename = $u->getHeaderInfo()->{Name}; print "$filename\n" ; } die "Error processing $zipfile: $!\n" if $status < 0 ;

Replies are listed 'Best First'.
Re: problems using IO::Uncompress::Unzip to get name of zip contents.
by andr3w77 (Novice) on Dec 21, 2010 at 12:48 UTC
    Thanks to a suggestion by Bart I have now met my requirements. The new code is
    #!/usr/bin/perl -w use strict; use warnings; use Archive::Zip qw( :ERROR_CODES ); my $zipfile = "/tmp/test.zip"; my $zip = Archive::Zip->new(); unless ( $zip->read( "$zipfile" ) == AZ_OK ) { die 'read error'; } my @members = $zip->memberNames(); my $fileNames = join(', ', @members); print"fileNames=$fileNames";
Re: problems using IO::Uncompress::Unzip to get name of zip contents.
by jethro (Monsignor) on Dec 21, 2010 at 12:07 UTC

    Hi, tested your script with a few testfiles and it worked, expect for one case where uncompressed files were stored in the zip file. That usually happens when you try to zip a zipfile or something else that can't be compressed further.

    If you do 'unzip -v' instead of 'unzip -l' you should see whether files are compressed or not inside your zipfile

    I experimented with some of the optional parameters, but that didn't help. The documentation didn't really help either, some of the jargon is never defined, for example what a stream is in that context. Probably you have to know the unzip format to know that.

    Anyway, it seems likely this is a bug in the module

      I believe they are not compressed.
      [user@dev2 ~]# unzip -v /tmp/test.zip Archive: /tmp/test.zip Length Method Size Ratio Date Time CRC-32 Name -------- ------ ------- ----- ---- ---- ------ ---- 19855954 Stored 19855954 0% 08-24-10 17:06 0fcdfbd0 RGB_DSC08815 +.jpg 11230988 Stored 11230988 0% 08-24-10 17:05 67a55892 RGB_fly43337 +.jpg 13285985 Stored 13285985 0% 08-24-10 17:04 99f07708 RGB_fly48151 +.jpg 10024136 Stored 10024136 0% 08-24-10 17:04 830c539f RGB_fly48168 +.jpg 9236336 Stored 9236336 0% 08-24-10 17:04 7e2c3508 RGB_fly54598 +.jpg 7750448 Stored 7750448 0% 08-24-10 17:03 63612579 RGB_fly57173 +.jpg 8870833 Stored 8870833 0% 08-24-10 17:03 d592d805 RGB_MHowell1 +42.jpg 10178959 Stored 10178959 0% 08-24-10 17:06 ee859aa4 RGB_MHowell1 +81.jpg 1151658 Stored 1151658 0% 08-24-10 17:16 9d05c82e RGB_Ducky_HA +RD.eps 14741504 Stored 14741504 0% 09-07-10 17:08 f6bf0574 RGB_TEST.ind +d -------- ------- --- ------- 106326801 106326801 0% 10 files