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

is this possible? to find out whats in a zip file without opening it? ive heard of it, but i dont even know where to begin doing it. i cant unzip a zip file, but not this. any help? thanx in advance for any help. :)
  • Comment on Reading contents of a zip file without opening it

Replies are listed 'Best First'.
Re: Reading contents of a zip file without opening it
by dvergin (Monsignor) on Nov 01, 2001 at 05:06 UTC
    Here's a bit of code mangled for your purpose from a hunk of working code from an old project. It should run as-is or with a tweak or two.
    #!/usr/bin/perl -w use strict; use Archive::Zip; my $zfile = 'somefile.zip'; my $zipobj = Archive::Zip->new() or die "Can't create zip object\n"; if ($error = $zipobj->read($zfile) ) { die "Can't read $zfile\n"; } my @file_names = $zipobj->memberNames();
    I recommend Archive::Zip. It's pretty straightforward and capable once you get the concept of the way it returns zipped archive members as objects which you then call other methods to read or manipulate. For example (from the same project):
    my @file_objs = $zipobj->members(); for my $file_obj (@file_objs) { my $file_txt = $file_obj->contents(); # etc. etc. }
    Cheers. Hope this helps. David
      Thanks David for sharing, This works fine for me.
Re: Reading contents of a zip file without opening it
by {NULE} (Hermit) on Nov 01, 2001 at 04:08 UTC
    Hi,

    I hear PSI::FileContents is popular for doing this. Ahem, sorry.

    If you want to list the files in a zip archive perhaps just unzip -l archive.zip will do what you like. That's what man zip tells me at least. I'm sure there are dos/windows equivalents.

    If you really need to use Perl to do this check out Archive::Zip It looks like the memberNames method might return a list of the files within the archive.

    Something to keep in mind is that somewhere either of these methods has to open() the file - otherwise how would they get any information from it? (I|We) could probably be of more help if you could provide more information about what you want to do.

    {NULE}
    --
    http://www.nule.org