Reputation: 3262
I tried to zip some CSV files which are about 5 MB with Perl. Below is my zip code.
The files are zipped but when I open them with the Windows unzip utility I found that the last lines of the CSV files are missing. What could be the problem here? I tried to change the chuncksize
and the desiredCompressionLevel
but this didn't help.
sub zip_util{
my $directory = shift;
$zip = Archive::Zip->new();
$zip->setChunkSize(65536);
# Add a file from disk
my $file1=File::Spec->catfile($directory, 'file.csv');
my $file2=File::Spec->catfile($directory, 'file2.csv');
my $file3=File::Spec->catfile($directory, 'fil3.csv');
$zip->addFile($file1,'file1.csv')->desiredCompressionLevel( 6 );
$zip->addFile($file2,'file2.csv')->desiredCompressionLevel( 6 );
$zip->addFile($fil3,'file3.csv')->desiredCompressionLevel( 6 );
# Save the Zip file
my $zipped_file=File::Spec->catfile($directory,'files.zip');
unless ( $zip->writeToFileNamed($zipped_file) == AZ_OK ) {
print LOG ": Zip Creation error\n";
}
Upvotes: 1
Views: 629
Reputation: 11869
I've checked it with warnings and strictures and I've found certain problems.
$zip
doesn't use my
(I don't know if it's intentional, but use strict
really helps with globals like this).$zip->addFile($fil3,'file3.csv')
. $fil3
variable definitely doesn't exist. If anything, this variable is $file3
.I've done quick script which I've used during testing.
use strict;
use warnings;
use Archive::Zip qw( :ERROR_CODES );
use File::Spec;
sub zip_util {
my ($directory) = @_;
my $zip = Archive::Zip->new();
$zip->setChunkSize(65536);
# Add a file from disk
my $file1 = File::Spec->catfile( $directory, 'file.csv' );
my $file2 = File::Spec->catfile( $directory, 'file2.csv' );
my $file3 = File::Spec->catfile( $directory, 'file3.csv' );
$zip->addFile( $file1, 'file1.csv' )->desiredCompressionLevel(6);
$zip->addFile( $file2, 'file2.csv' )->desiredCompressionLevel(6);
$zip->addFile( $file3, 'file3.csv' )->desiredCompressionLevel(6);
# Save the Zip file
my $zipped_file = File::Spec->catfile( $directory, 'files.zip' );
if ( $zip->writeToFileNamed($zipped_file) != AZ_OK ) {
print STDERR "Zip Creation error\n";
}
}
zip_util '.';
The problem is that it was working. So, I've done this script to make some sort of 5MB files:
use strict;
use warnings;
use 5.010;
for ( 1 .. 524_288 ) {
my $number = $_ % 10;
say "$number-------";
}
Both files in ZIP and original "CSV" had this same size and content. So, it's probably the second issue which I've mentioned - use of $fil3
variable or something with your files (which you sadly didn't uploaded, so I cannot look into those).
Upvotes: 1