Re^2: Creating a .zip file using perl
by changma_ha (Sexton) on May 05, 2011 at 09:31 UTC
|
use strict;
use warnings;
use Archive::Zip;
#use Archive::Tar;
print "Starting...\n";
# Archive::Zip Synopsis (relative path to directory)
my $zip1 = Archive::Zip->new();
$zip1->addFile( 'c:/Perl/bin/ans1.txt' )
or die 'unable to add file to archive';
$zip1->writeToFileNamed('test1.zip');
# Archive::Zip Synopsis (with ALL CAPS DIRECTORY NAME)
my $zip2 = Archive::Zip->new();
$zip2->addFile('c:/Perl/bin/ans1.txt' )
or die 'unable to add file to archive';
$zip2->writeToFileNamed('tests.zip');
# Archive::Tar Synopsis (relative path to directory)
#my $tar3 = Archive::Tar->new;$tar3->add_files( 'MyArchiveFiles/file1.
+txt' )or die 'unable to add file to archive';$tar3->write('test3.tar'
+);
print "Finished successfully!";
| [reply] [d/l] |
|
This code is working fine for me, but if i use the special characters like "ÅÄÖ" in the filename(like, $zip2->addFile('c:/Perl/bin/Åns1.txt' ) then the filename within the zip file is like c:/Perl/bin/+ns1.txt. So this code is not working properly for special characters. Please post if you know to zip the file which contain "ÅÄÖ". Thanks in advance
| [reply] |
|
| [reply] |
Re^2: Creating a .zip file using perl
by Anonymous Monk on May 05, 2011 at 06:42 UTC
|
Running the below code is giving me an exception,can anyone pls advise what am I doing wrong?
use strict;
use warnings;
use File::Find;
use Archive::Zip qw( :ERROR_CODES );
my @array;
my ($zipfile,$filesStr,$output);
my %seen;
find(sub {
push @array, "$File::Find::name" if -f && /\.lib(?:\.\d+)?$/ &&
+ !$seen{$File::Find::name}++;
}, ".");
$filesStr = join(" ",@core_libs);
$zipfile = "files.zip";
$output = `zip $zipfile $filesStr`;
print "Progress:\n$output\n\nYour files are in [$zipfile].";
| [reply] [d/l] |
|
Why do you expect us to tell you what you're doing wrong if you're not even telling us what "exception" you get?
Also, you were advised to use Archive::Zip. Why do you shell out to the zip program instead?
| [reply] [d/l] |
|
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use File::Spec;
use strict;
use warnings;
my $zipfile = shift or die "What's the zipfile?";
my $dir = shift or die "Where's the dir?";
my $zip = Archive::Zip->new();
opendir my $dh, $dir or die $!;
while (readdir $dh) {
next if !/\.lib$/;
$zip->addFile(File::Spec->catfile($dir, $_), $_);
}
closedir $dh;
# Save the Zip file
unless ( $zip->writeToFileNamed($zipfile) == AZ_OK ) {
die 'write error';
}
| [reply] [d/l] |
|
>perl c:\perl_scripts\zip.pl test C:\dir
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
Use of uninitialized value in pattern match (m//) at c:\perl_scripts\z
+ip.pl line 14.
| [reply] [d/l] |
|
|
|
|
|
|
use IO::Compress::Zip qw(:all);
zip [ glob("*.lib") ] => "my.zip"
or die "Cannot create zip file: $ZipError" ;
| [reply] [d/l] |