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

Hello fellow monks,

I am trying to add files to the newest zip file in a directory. But if the newest zip file contains files more than 3 months long, I create a new zip file and store my files in the new zip file.

So this is what I am doing now to find the newest zip file in the directory:

#finding the newest .zip file in the directory my $zipFile = reduce {(stat $a)[10] > (stat $b)[10] ? $a : $b} glob Fi +le::Spec->catfile($dir, '*.zip');

Could you please tell me how I can open up a zip file and check if the difference between the oldest file and the newest file in there is equal to or less than 3 months old?

Thank you.

Replies are listed 'Best First'.
Re: Checking file dates in a Zip file
by Yendor (Pilgrim) on Dec 02, 2004 at 17:59 UTC

    It looks like you can use Archive::Zip to open up a Zip file.

    use Archive::Zip; my $zip = Archive::Zip->new("yourfile.zip");

    Once you have the Zip file opened, use the members() method to get a list of files contained in the Zip archive, and loop over them:

    my @members = $zip->members(); foreach my $member (@members) { # ... }

    Where the # ... is is where the interesting bits of your code need to go. In order to check out the last modified time of the $fh you have, call $member->lastModTime().

    So let's put this together...

    #!/usr/bin/perl -T use strict; use warnings; use Archive::Zip; my $zip = Archive::Zip->new("file.zip"); my $members = $zip->members(); my $firstdate; my $lastdate; # No need to fill these in with "never", because I hav +e had dates before. ;-) foreach my $member (@members) { my $filedate = $member->lastModTime(); if (($filedate < $firstdate) || (!defined($firstdate)) { $firstdate = $filedate; } if ((!defined($lastdate) || ($filedate > $lastdate)) { $lastdate = $filedate; } } $fh->close(); # Calculate the difference between the first and last # file dates... my $datediff = $lastdate - $firstdate; # Check if the time difference between the first and last # files is more than 3 months... if ($datediff > (60 * 60 * 24 * 30 * 3)) { # You need a new file }

    Please note that this code is completely untested, but it should at least be a reasonable start...

      hehe...that is funny :)

      Thank you...that did work.