Home: Perl Programming Help: Beginner:
How to Zip files and directories?



tester_V
Novice

Nov 23, 2017, 2:19 PM


Views: 10016
How to Zip files and directories?

Hi,
Happy Thanksgiving everybody!
I hope all well today and everyone will have a good meal in peace and with your family and friends.


Any way back to busyness....

I have a problem zipping files.

I'd like to Zip some files in some Dirs, not all files but particular files from a directory.

I checked Google already there are a lot of examples of something I do not need and no clear one on how to use IO::Compress::Zip

I found documents about the IO module but it is like reading Latin.
It is like they are trying to make everything complicated for some reason and I'm pretty sure it is not and can be written in human language.

Here is the code, I tried to make changes to it but it still zipps the whole directory "H:/Sixshot_logs/Unit_oDay.zip".

Thank you in advance!
Tester-V

#!/usr/local/bin/perl
use strict ;
use warnings ;
use diagnostics


use IO::Compress::Zip qw(:all);
my $unit_zippedfile = "H:/Sixshot_logs/Unit_oDay.zip" ;
my $unit_tozip = 'H:/Sixshot_logs/Unit.txt' ;

zip "H:/Sixshot_logs/Unit.txt" => "$unit_zippedfile"
or die "zip failed: $ZipError\n";


Laurent_R
Veteran / Moderator

Nov 23, 2017, 11:09 PM


Views: 10007
Re: [tester_V] How to Zip files and directories?

Hi,
I do not know about IO::Compress::Zip and don't have time now to check it, but Archive::Zip lets you add files one by one (or even a list of files) to a zip archive. So, it is just a matter of preparing a list of the files you want to zip.


tester_V
Novice

Nov 23, 2017, 11:59 PM


Views: 10005
Re: [Laurent_R] How to Zip files and directories?

I'll try that... Thank you Laurent...


tester_V
Novice

Nov 24, 2017, 12:15 PM


Views: 9997
Re: [tester_V] How to Zip files and directories?

Hellow!
Here is new code I came up with (thank you Google!),
sometime it is working but if I repoint it to diferent directory it does not. I'm trying to zip text files only and each directory contains only log files (.txt) and no sub directories.
Here is the error :

Can't call method "desiredCompressionLevel" on an undefined value at C:/Strawberry/perl/vendor/lib/Archive/Zip/Archive.pm line 256.

Here is the code:


#!/usr/bin/perl
use strict;
use warnings;
use Archive::Zip qw/ :ERROR_CODES :CONSTANTS/;


my $directory = 'C:/PScripts/Test/';

opendir(DIR,$directory) or die "Cannot open $directory $!" ;
my @flow_files = readdir(DIR);
closedir(DIR);

my$ obj = Archive::Zip->new();

foreach my $flow_files (@flow_files)
{
next if($flow_files =~/^\.$/) ;
next if($flow_files =~/^\.\.$/) ;
print $flow_files,"\n";
$obj->addFile($flow_files);
}
if ($obj->writeToFileNamed('Flow_Files.zip') != AZ_OK)
{
print "Error in archive creation!";
} else {
print "Archive created successfully!";
}

exit ;

Code



      
    


FishMonger
Veteran / Moderator

Nov 24, 2017, 12:23 PM


Views: 9996
Re: [tester_V] How to Zip files and directories?

You're making the same mistake I pointed out in your prior question "Join files in a directory code errors out".


tester_V
Novice

Nov 24, 2017, 1:00 PM


Views: 9993
Re: [FishMonger] How to Zip files and directories?

Thank you Fish!

Tester_V


tester_V
Novice

Nov 24, 2017, 1:09 PM


Views: 9990
Re: [tester_V] How to Zip files and directories?

wanted to say that when this line changed to:

if ($obj->writeToFileNamed('C:/PScripts/NN/Flow_Files.zip') != AZ_OK)

the script will save ZIP file to any directory.

Thank you all (Fish, I tip my hat!)!

#!/usr/bin/perl
use strict;
use warnings;
use Archive::Zip qw/ :ERROR_CODES :CONSTANTS/;


my $directory = 'C:/PScripts/Test/';
opendir(DIR,$directory) or die "Cannot open $directory $!" ;
my @flow_files = readdir(DIR);
closedir(DIR);

my$ obj = Archive::Zip->new()or die "Failed to create a new archive" ;

foreach my $flow_files (@flow_files)
{
my $filesand_path = "$directory$flow_files" ;

next if($flow_files =~/^\.$/) ;
next if($flow_files =~/^\.\.$/) ;

print $filesand_path,"\n";
$obj->addFile($filesand_path) or die "Fuck! Dying while adding $flow_files " ;
}
if ($obj->writeToFileNamed('C:/PScripts/NN/Flow_Files.zip') != AZ_OK)
{
print "Error in archive creation!";
} else {
print "Archive created successfully!";
}

exit ;

Code