CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



tester_V
Novice

Nov 23, 2017, 2:19 PM

Post #1 of 7 (1378 views)
How to Zip files and directories? Can't Post

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

Post #2 of 7 (1369 views)
Re: [tester_V] How to Zip files and directories? [In reply to] Can't Post

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

Post #3 of 7 (1367 views)
Re: [Laurent_R] How to Zip files and directories? [In reply to] Can't Post

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


tester_V
Novice

Nov 24, 2017, 12:15 PM

Post #4 of 7 (1359 views)
Re: [tester_V] How to Zip files and directories? [In reply to] Can't Post

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

Post #5 of 7 (1358 views)
Re: [tester_V] How to Zip files and directories? [In reply to] Can't Post

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

Post #6 of 7 (1355 views)
Re: [FishMonger] How to Zip files and directories? [In reply to] Can't Post

Thank you Fish!

Tester_V


tester_V
Novice

Nov 24, 2017, 1:09 PM

Post #7 of 7 (1352 views)
Re: [tester_V] How to Zip files and directories? [In reply to] Can't Post

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



      
    

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives