in reply to Re: Creating a .zip file using perl
in thread Creating a .zip file using perl

Running the below code is giving me an exception,can anyone pls advise what am I doing wrong?

use strict; use warnings; use File::Find; use Archive::Zip qw( :ERROR_CODES ); my @array; my ($zipfile,$filesStr,$output); my %seen; find(sub { push @array, "$File::Find::name" if -f && /\.lib(?:\.\d+)?$/ && + !$seen{$File::Find::name}++; }, "."); $filesStr = join(" ",@core_libs); $zipfile = "files.zip"; $output = `zip $zipfile $filesStr`; print "Progress:\n$output\n\nYour files are in [$zipfile].";

Replies are listed 'Best First'.
Re^3: Creating a .zip file using perl
by Corion (Patriarch) on May 05, 2011 at 06:46 UTC

    Why do you expect us to tell you what you're doing wrong if you're not even telling us what "exception" you get?

    Also, you were advised to use Archive::Zip. Why do you shell out to the zip program instead?

Re^3: Creating a .zip file using perl
by wind (Priest) on May 05, 2011 at 06:53 UTC

    The following code will use Archive::Zip to create a zip with all the .lib files from a specified directory.

    This was pulled pretty much directly from the examples in the documentation

    use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); use File::Spec; use strict; use warnings; my $zipfile = shift or die "What's the zipfile?"; my $dir = shift or die "Where's the dir?"; my $zip = Archive::Zip->new(); opendir my $dh, $dir or die $!; while (readdir $dh) { next if !/\.lib$/; $zip->addFile(File::Spec->catfile($dir, $_), $_); } closedir $dh; # Save the Zip file unless ( $zip->writeToFileNamed($zipfile) == AZ_OK ) { die 'write error'; }

      Hi Wind - I get the following error when running the code,any idea what is wrong?

      >perl c:\perl_scripts\zip.pl test C:\dir Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14. Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z +ip.pl line 14.

        Well, since line 14 is:

        next if !/\.lib$/;

        I would surmise that $_ is not being initialized properly.

        Did you change the code in some way that might lead to this? Like by assigning the return value of the readdir call to a variable?

Re^3: Creating a .zip file using perl
by pmqs (Friar) on May 05, 2011 at 08:46 UTC
    Here is an alternative.
    use IO::Compress::Zip qw(:all); zip [ glob("*.lib") ] => "my.zip" or die "Cannot create zip file: $ZipError" ;