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

Hi there Monks!
I have a few questions related to what I am trying to accomplish here, here I go!
My code works with a few issues, first if there is a way when zipping files to not include the directories where I am reading these files from into the .zip file been created. The second issue is when I am adding the log.txt file to the zip file previously created it is duplicating the files from the directories I am reading from.
Here is the code:
#!/usr/bin/perl -w # use strict; use CGI qw(:standard); use File::Basename; use File::stat; use File::Slurp qw(read_dir); use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); print header(); # zip stuff::: my $zip = Archive::Zip->new(); # new instance #Archive::Zip::setChunkSize( 2000 ); #my $chunkSize = Archive::Zip::chunkSize(); my ($file_path, $file_name, $zipped, $flag,$k_size); # vars my @files; # get the pdf files from here::: my @directories = qw( dir_first dir_sec); # set sizes::: my $min_size = 1024; # bytes => 1K my $max_size = 10485760; # bytes =>10MB # go in the directories::: my $c; for my $d (@directories) { $c++; push @files, grep { -f && -s _ >= $min_size && -s _ <= $max_size } + read_dir( $d, prefix => 1 ); } #get file sizes just in case::: foreach my $files(@files) { my $size = (stat("$files"))[7] / 1024; # size in kilobytes $k_size = sprintf ("%03d", $size); #print "\n$files = $size - $k_size\n"; # do some clean up::: $files=~s/[\r\n]+//; if($files =~/(.*?)\/([^\/]+)$/) { $file_path=$1; $file_name=$2; } # creating log file::: open (INDEXFILE, '>>log.txt'); print INDEXFILE "$file_name - $k_size\n"; close (INDEXFILE); $zipped = $zip->addDirectory($file_path); $zipped->desiredCompressionMethod( COMPRESSION_DEFLATED ); #$zipped->desiredCompressionLevel( 9 ); # 1 gives the best speed and + worst compression, and 9 gives the best compression and worst speed. foreach my $each_file (glob("$file_path/$file_name")) { $zipped = $zip->addFile($each_file); } die $flag="Failed" unless $zip->writeToFileNamed( 'test.zip' ) == A +Z_OK; } #need to add the log.txt to the previously created zip file::: $zipped = $zip->read('test.zip'); # read file content if ($zipped != AZ_OK) { die('Error in file!'); } else { $zipped = $zip->addFile('log.txt'); # add files if ($zipped = $zip->overwrite() != AZ_OK) { # overwrite archive wi +th new contents print "\n\nError adding file to current zip file\n\n!"; } else { print "\n\nAdded file to current zip file was successfully!\n\n"; } } # show status::: if($flag eq "Failed") { print "\n\nError in archive creation!\n\n"; } else { print "\n\nArchive created successfully!\n\n"; }
Thanks for the Help!

Replies are listed 'Best First'.
Re: Zipping Files Help!
by Lotus1 (Vicar) on Aug 31, 2011 at 14:03 UTC

    No offense intended but it looks like a lot of rookie mistakes here. Maybe you copied and pasted some code from examples. But it is really tough to debug something like this without understanding each part.

    For example you have a loop where for each file in @files you do another foreach loop on a glob of the individual file. This makes no sense because you already have the file name. How about making a test program that has a print statement inside the foreach loop that prints "addFile($filename)\n" so you know what the loop is doing.

    For each file in @files you call addDirectory($file_path). Either add the directory or add individual files, not both.

    For each file in @files you call writeToFileNamed(). This should be done once after you have added all the files to $zip.

    To answer your question about adding the log file: have a look at this from http://search.cpan.org/~adamk/Archive-Zip-1.30/lib/Archive/Zip.pm

    overwrite() Write back to the original zip file. See overwriteAs() above. If the zip was not ever read from a file, this generates an error.

    You need to read a zipfile into $zip before you can call $zip->overwrite().

    Try something like this:

    if (-e $zip_path) { unless ( $zip->read( $zip_path ) == AZ_OK ) { die "$zip_path read error.\n"; } print "read $zip_path\n"; } else { die "Error: $zip_path should have just been created."; } # copy those from dir to zip. foreach (@files2copy) { die "File missing: $dir$_" unless -e "$dir$_"; unless ( $zip->addFile( "$dir$_" , $_ )){ die "couldn't add $dir$_ to $zip_path."; } } unless ( $zip->overwrite() == AZ_OK ) { die "$zip_path write error"; }

    This also shows how to add files with a different name or path than the original full path. $zip->addFile( "$dir$_" , $_ ) Here I added only the relative path starting at the path to $dir. You could also just put the filename. But without paths don't repeat filenames unintentionaly. zip archives allow multiple copies of the same file and Archive::Zip complies.

Re: Zipping Files Help!
by duyet (Friar) on Aug 31, 2011 at 03:48 UTC

    Your code doesn't run!

    IO error: opening test.zip for read : No such file or directory ...

    After created a test.zip, it still doesn't run.

    format error: file is too short

    One otherthing, you don't want to open and close the log.txt within a loop!!!Move the open() and close() outside of the foreach

      This will run, just create the two directories and let me know what you think, thanks for looking!
      #!/usr/bin/perl -w # use strict; use CGI qw(:standard); use File::Basename; use File::stat; use File::Slurp qw(read_dir); use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); #print header(); # zip stuff::: my $zip = Archive::Zip->new(); # new instance #Archive::Zip::setChunkSize( 2000 ); #my $chunkSize = Archive::Zip::chunkSize(); my ($file_path, $file_name, $zipped, $flag,$k_size); $flag = ''; # vars my @files; # get the pdf files from here::: my @directories = qw( /var/www/test_1 /var/www/test_2); # set sizes::: my $min_size = 1024; # bytes => 1K my $max_size = 10485760; # bytes =>10MB # go in the directories::: my $c; for my $d (@directories) { $c++; push @files, grep { -f && -s _ >= $min_size && -s _ <= $max_size } + read_dir( $d, prefix => 1 ); } #get file sizes just in case::: # open (INDEXFILE, '>>log.txt'); foreach my $files(@files) { #my $size = (stat($files))[7] / 1024; # size in kilobytes my $size = stat($files)->size; $k_size = sprintf ("%03d", $size); #print "\n$files = $size - $k_size\n"; # do some clean up::: $files=~s/[\r\n]+//; if($files =~/(.*?)\/([^\/]+)$/) { $file_path=$1; $file_name=$2; } # creating log file::: open (INDEXFILE, '>>log.txt'); print INDEXFILE "$file_name - $k_size\n"; close (INDEXFILE); $zipped = $zip->addFile('log.txt')unless $zip->memberNamed('log.txt' +); foreach my $each_file (glob("$file_path/$file_name")) { $zipped = $zip->addFile($each_file,$file_name); } die $flag="Failed" unless $zip->writeToFileNamed( 'test.zip' ) == A +Z_OK; } #close (INDEXFILE); # show status::: if($flag eq "Failed") { print "\n\nError in archive creation!\n\n"; } else { print "\n\nArchive created successfully!\n\n"; }
      I tried removing the open() and close() outside of the foreach loop, but cause of the zip gets done before the text file the log.txt file inside of the zip file is empty, any suggestions? Thanks!
      #!/usr/bin/perl -w # use strict; use CGI qw(:standard); use File::Basename; use File::stat; use File::Slurp qw(read_dir); use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); # zip stuff::: my $zip = Archive::Zip->new(); # new instance #Archive::Zip::setChunkSize( 2000 ); #my $chunkSize = Archive::Zip::chunkSize(); my ($file_path, $file_name, $zipped, $flag,$k_size); $flag = ''; # vars my @files; # get the files from here::: my @directories = qw( /var/www/test_1 /var/www/test_2); # set sizes::: my $min_size = 1024; # bytes => 1K my $max_size = 10485760; # bytes =>10MB # go in the directories::: my $c; for my $d (@directories) { $c++; push @files, grep { -f && -s _ >= $min_size && -s _ <= $max_size } + read_dir( $d, prefix => 1 ); } #get file sizes just in case::: open (INDEXFILE, '>>log.txt'); foreach my $files(@files) { my $size = stat($files)->size / 1024; # size in kilobytes $k_size = sprintf ("%03d", $size); # do some clean up::: $files=~s/[\r\n]+//; if($files =~/(.*?)\/([^\/]+)$/) { $file_path=$1; $file_name=$2; } # creating log file::: print INDEXFILE "$file_name - $k_size Kb\n"; $zipped = $zip->addFile('log.txt') unless $zip->memberNamed('log.txt +'); $zipped = $zip->addFile($files,$file_name); } die $flag="Failed" unless $zip->writeToFileNamed( 'test.zip' ) == AZ_ +OK; close (INDEXFILE); # show status::: if($flag eq "Failed") { print "\n\nError in archive creation!\n\n"; } else { print "\n\nArchive created successfully!\n\n"; }

        This version is trying to add a copy of 'log.txt' to the zip file each time the foreach loop iterates unless the file is already there. That means it adds it the first time and never again.

        It seems like you would want to just print to log.txt inside the foreach loop. Then after the foreach loop, close 'log.txt' and then call $zip->addFile('log.txt') then call writeToFileNamed.

        What is the purpose of the variable $zipped? You don't use it anywhere.

        # do some clean up::: $files=~s/[\r\n]+//;

        Is this intended to remove newlines? Why not use chomp?

        What does the output from this program look like? Is it creating test.zip at all? Does it contain files?

        I suggest you get rid of the 'show status' section at the end. If the program dies that won't run. And there is no use having the variable $flag.