perldatezipunzipperl-core

Perl : IO::Uncompress::Unzip : getting last modified date of file inside zip


I am currently working with the core module IO::Uncompress::Unzip to read the content of a zip file in Perl. I previously used Archive::Zip, which is a great module, and didn't give me that many headaches, but it's not bundled as a core module.

I would like to get the last modified time of the files that were zipped in my zip file. For example, I have the following file : "test.zip" which contains "1.txt", "2.txt", "3.txt". The zip was, for instance, last modified today, whereas the txt files were modified a week ago. How can I get the last modified date of each compressed file in my zip ? I could do this very easily with Archive::Zip, but not with this module..

Here's the code I'm using :

use strict;
use warnings;
use IO::Uncompress::Unzip qw($UnzipError);

my $zipfile  = 'test.zip';
my $u = new IO::Uncompress::Unzip $zipfile
   or die "Cannot open $zipfile: $UnzipError";
for (my $status = 1; $status > 0; $status = $u->nextStream) {
    my $name = $u->getHeaderInfo->{Name};
    # my $date = ctime(stat($u)->mtime); -> This doesn't work, even though $u is considered as a filehandle in the IO::Uncompress::Unzip documentation.. 
    warn "Processing member $name\n" ;

    while(<$u>) {
        print "Line $. -> $_";
    }
}

Any ideas or workarounds to solve my problem ?

Thanks !


Solution

  • You can try $u->getHeaderInfo->{Time}

    Check all attributes with

    use Data::Dumper;
    print Dumper scalar $u->getHeaderInfo;