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

Hi Monks!
I have this code that generates files from a larger file, all I need to do is zip all the files generated, how could I do that? I am trying like on my code here, but it's not working, any help?

#!/perl/bin/perl -w use strict; use warnings; use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); use File::Find; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); # @chunks could be: file1.txt file2.txt file3.txt or more files or jus +t one my $htdocs_out; open $htdocs_out, '>', "$arch_path/$dir_archive_name/$outname" + or die $!; foreach my $chunk_file(@chunks){ my $chunk_zip = Archive::Zip->new(); my $chunk_zipped; $chunk_zipped = $chunk_zip->addFile( $chunk_file ); print $htdocs_out $chunk_zipped; } close $htdocs_out or die $!;

Thanks!

Replies are listed 'Best First'.
Re: Zipping Files
by samtregar (Abbot) on Jun 26, 2006 at 20:17 UTC
    Check your error logs. I'm quite certain you'll find errors there since you're using strict and not defining variables before you use them. (Please keep using strict! Sure, you could make the errors go away by droping it, but that won't help you solve your problem.)

    If, after examining the errors you're generating, post again and include the errors. We can explain them to you, but only after we've seen them!

    -sam

      I am sorry, but had only missed one line, but here it is:
      #!/perl/bin/perl -w use strict; use warnings; use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); use File::Find; use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); # @chunks could be: file1.txt file2.txt file3.txt or more files or jus +t one my ($arch_path, $dir_archive_name, $outname, my @chunks); my $htdocs_out; open $htdocs_out, '>', "$arch_path/$dir_archive_name/$outname" +or die $!; foreach my $chunk_file(@chunks){ my $chunk_zip = Archive::Zip->new(); my $chunk_zipped; $chunk_zipped = $chunk_zip->addFile( $chunk_file ); print $htdocs_out $chunk_zipped; } close $htdocs_out or die $!;
      Thanks again!
        I don't think addFile() is returning what you think it is. I think you need to use one of the write*() methods in Archive::Zip instead of trying to print out each chunk yourself.

        If that's not it, why don't you tell us what exactly the problem is. Do you get an error message? A corrupt file? Something else?

        -sam

Re: Zipping Files
by radiantmatrix (Parson) on Jun 27, 2006 at 17:14 UTC

    Here, without all the extraneous modules, is simple example code. You're not using the Archive::Zip module properly.

    #!/usr/bin/perl use strict; use warnings; use File::Spec::Functions qw'catdir catfile'; use Archive::ZIP qw':ERROR_CODES :CONSTANTS'; # here's an example @chunks array my @chunks = qw(file1.txt file2.txt file3.txt); #example output file name: output.zip in HomeDir my $zip_file = catfile($ENV{HOME},'output.zip'); my $zip = Archive::Zip->new(); foreach my $file (@chunks) { my $member = $zip->addFile($file); #add fil +e $member->desiredCompressionMethod( COMPRESSION_DEFLATED ); #compres +s file } #write ZIP file to disk my $status = $zip->writeToFileNamed($zip_file); die "Unable to write to file '$zip_file' if $status != AZ_OK; #error c +heck

    Note that I never open my own file handle, the module takes care of all that for me.

    <radiant.matrix>
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet

      ++ for straight forward adaption of some of the functions mentioned in the "SYNOPSIS" code from the Archive::Zip docs to the OP's need.

      Note that I never open my own file handle, the module takes care of all that for me.

      Of course, if he wants to he can use writeToFileHandle (see docs linked above) … the module provides this and a full wealth of other functions to read/write/create/manipulate Zip-Archives.