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

Hi all,

I'm trying to create a zip file with Archive::Zip, this is my code:
cat /home/user/d/file_list.txt /home/user/a/a.txt /home/user/b/b.txt /home/user/c/c.txt #! /usr/bin/perl use strict; use warnings; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); my $zip = Archive::Zip->new(); open (FILE_LIST, '/home/user/d/file_list.txt'); while (<FILE_LIST>) { chomp; my $member = $zip->addFile($_); print "Add error" unless ($member); } close FILE_LIST; unless ($zip->writeToFileNamed('/home/user/d/backup.zip') == AZ_OK) { die "write error";}
but I only obtain a void zip :(

What is it wrong?

Thanks :)

Replies are listed 'Best First'.
Re: Problem with creating a zip: void file
by kennethk (Abbot) on Feb 05, 2010 at 15:03 UTC
    The immediate red flag I see is that you are not testing your open on line 8 - the common approach there would be:

    open (FILE_LIST, '/home/user/d/file_list.txt') or die "File list open fail: $!";

    to make sure your open is successful - that would certainly explain your observation. Barring that, a quick review of Archive::Zip doesn't raise any more flags. Try modifying your while loop to:

    while (<FILE_LIST>) { print; chomp; my $member = $zip->addFile($_); print "Add error" unless ($member); }

    and tell us if it actually outputs what you expect.


      First of all, thanks for your reply :)

      I have added print statement and it outputs the same of "cat" command:
      my_script.pl /home/user/a/a.txt /home/user/b/b.txt /home/user/c/c.txt
      and my zip file is still void :(

      But if I put the three txt files (a.txt, b.txt, c.txt) in the same directory of perl script, then the script works as aspected and my zip file contains the three files
        The only suggestion I can make is that in reformatting your information for posting, you have somehow removed your bug. Specifically, running the script:

        #! /usr/bin/perl use strict; use warnings; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); my $zip = Archive::Zip->new(); open (FILE_LIST, '/home/user/d/file_list.txt') or die "File list open + fail: $!"; while (<FILE_LIST>) { chomp; my $member = $zip->addFile($_); print "Add error" unless ($member); } close FILE_LIST; unless ($zip->writeToFileNamed('/home/user/d/backup.zip') == AZ_OK) { die "write error";}

        from multiple directories yielded success for me, with input file:

        /home/user/a/a.txt /home/user/b/b.txt /home/user/c/c.txt

        and where success was measured by the results of the script:

        #! /usr/bin/perl use strict; use warnings; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); my $zip = Archive::Zip->new('backup.zip'); print $zip->memberNames();

        where the only scrubbing I performed was changing user name. The only other suggestion I can make is that a review of the add_file method in Archive::Zip includes the admonition:

        NOTE that you shouldn't (generally) use absolute path names in zip member names, as this will cause problems with some zip tools as well as introduce a security hole and make the zip harder to use.