Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
i am new in perl & i am trying to zip a file in a directory c:\scripts\1.txt using the module Archive::zip this is my script what am i doing wrong
$_="c:\/scripts\/1.txt";
use Archive::Zip;
$zip = Archive::Zip->new($_);
the errors that i get are :format error: file is too short
at C:/Perl/site/lib/Archive/Zip.pm line 1057
Archive::Zip::Archive::_findEndOfCentralDirectory('Archive::Zip::Archive
=HASH(0x225288)', 'IO::File=GLOB(0x1c14858)') called at C:/Perl/site/lib/Archive
/Zip.pm line 966
Archive::Zip::Archive::readFromFileHandle('Archive::Zip::Archive=HASH(0x
225288)', 'IO::File=GLOB(0x1c14858)', 'c:/scripts/1111.TXT') called at C:/Perl/s
ite/lib/Archive/Zip.pm line 940
Archive::Zip::Archive::read('Archive::Zip::Archive=HASH(0x225288)', 'c:/
scripts/1111.TXT') called at C:/Perl/site/lib/Archive/Zip.pm line 523
Archive::Zip::Archive::new('Archive::Zip::Archive', 'c:/scripts/1111.TXT
') called at C:/Perl/site/lib/Archive/Zip.pm line 220
Archive::Zip::new('Archive::Zip', 'c:/scripts/1111.TXT') called at 2.pl
line 3and if i try to zip another file like news.png
i get the error
format error: can't find EOCD signature
at C:/Perl/site/lib/Archive/Zip.pm line 1090
Archive::Zip::Archive::_findEndOfCentralDirectory('Archive::Zip::Archive
=HASH(0x225288)', 'IO::File=GLOB(0x1c14858)') called at C:/Perl/site/lib/Archive
/Zip.pm line 966
Archive::Zip::Archive::readFromFileHandle('Archive::Zip::Archive=HASH(0x
225288)', 'IO::File=GLOB(0x1c14858)', 'c:/scripts/news.png') called at C:/Perl/s
ite/lib/Archive/Zip.pm line 940
Archive::Zip::Archive::read('Archive::Zip::Archive=HASH(0x225288)', 'c:/
scripts/news.png') called at C:/Perl/site/lib/Archive/Zip.pm line 523
Archive::Zip::Archive::new('Archive::Zip::Archive', 'c:/scripts/news.png
') called at C:/Perl/site/lib/Archive/Zip.pm line 220
Archive::Zip::new('Archive::Zip', 'c:/scripts/news.png') called at 2.pl
line 3

just a suggestion,
for a start, you can try change the $_="c:\...."
declaration to something likemy $filetozip = q(c:\scripts\1.txt);
and try againbtw what's the full perl code that you have written.? From your post, you have just 3 lines of code, and the last one is just initializing a new object.

here is my code!!! i run your suggestion but got an error "format error: file is too short" , all i want is to use the Archive::zip module to zip my files thats located in c:/server/logfiles/k11/2344/
code starts here..
use File::Find;
$dir1="c:/server/logfiles/";
print "$dir1\n";
find(\&wanted, $dir1);
sub wanted {
if (/^ex/) {
$w=`date /T`;
#print $w;
@tt= split /(\d\d)/,$w;
#foreach (@tt) { print "$_\n" }
#print"@tt[3]";
#print"@tt[1]";
#print"@tt[7]";
@rr=split /(\d\d)/,$_;
#print "@rr[1]\n";###03
#print "@rr[3]\n";###12
#print "@rr[5]\n";###06
if (@rr[1]!=@tt[3] or @rr[3]!=@tt[1] or @rr[5]!=@tt[7]) {
@arr=split('/',"$File::Find::dir/$_");
#use Archive::Zip;
#$zip = Archive::Zip->new($_);
system("PKZIP.EXE $_.zip $_");
use Net::FTP;
$ftp = Net::FTP->new("localhost", Debug => 0);
print $ftp->message;
$ftp->login("anonymous",'');
print $ftp->message;
if ($ftp->cwd("@arr[3]"))
{print"@arr[3] exist\n";}
else {$ftp->mkdir("@arr[3]");
$ftp->cwd("@arr[3]"); }
print $ftp->message;
$ftp->binary;
print $ftp->message;
$ftp->put("$File::Find::dir/$_.zip");
print $ftp->message;
$ftp->quit;
system("del
$_.zip");
#system("del $_");
}
}
}

I see a number of things that need to be fixed. But first, I have 2 questions.
If the files to be zipped are in "c:/server/logfiles/k11/2344/", why do you start the search from "c:/server/logfiles", which is 2 levels above the desired directory?
What is the exact format of your filenames? I know you're using a date structure something like "x30x12x05.txt", but in order to help, I'll need to know the exact format.
Due to the way Justin is parsing our post submissions, posting code in this forum is a joke. I can post a cleaned up and corrected script, but it would be best if I email it to you.

Here's an example script that uses Archive::Zip to create a zip file containing all of your logfiles. Once I know your exact filename format I can add the code to only put the desired files in the zip.
#!perl -w
use strict;
use File::Find;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );my $dir = 'c:/server/logfiles';
my $zip = Archive::Zip->new();
my $zipped;$zipped = $zip->addDirectory( $dir );
$zipped->desiredCompressionMethod( COMPRESSION_DEFLATED );
find(\&zip_file, $dir);
die 'write error' unless $zip->writeToFileNamed( 'logs.zip' ) == AZ_OK;sub zip_file {
$zipped = $zip->addFile( $File::Find::name );
}

First Answer:
I start the search from "c:/server/logfiles"
cause i dont know which folders are under
"c:/server/logfiles" there could be alot of directory in the same structure like "c:/server/logfiles/k11/2344" .
Second Answer:
the exact format of my filenames is :
ex051028.log .
i dont need to zip the file thats on the date i am running the script only the day before.
my email is
shcohen2002@yahoo.com
by the way thanks alot

There's a much easier method for calculating yesterday's date and testing for the filename. Use the strftime fuction from the POSIX module.
For example:
$filename = strftime("ex%Y%m%d.log", localtime(time-86400));
if (-e $filename) {
# zip the file
}I'll send you an updated script when I have a little more free time. BTW, are you looking for and zipping only 1 file or are there multiple files in various directories?

you make my life easier
i wanted to say but i forgot
there are multiple files in various directories and i need to zip one file seperatly as you can see in the script i am uploading each file to an ftp in a special directory.
on the script you sent me all the files were zipped to a directory.
now i broke my mind over it and i cant succeed zipping 1 losy file.
its seem that i dont quite understand the syntax .well isnt it right to zip 1 file
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
my $file = 'D:\Internet\LogFiles\1.txt';
my $zip = Archive::Zip->new();
my $zipped;
$zipped = $zip->addFile( $file );

Check your email. I sent you an updated script at the same time you were making your last post.
You can zip the files individually and ftp each one separately, but that's not the most efficient approach. I'd need to think about this, but I'd probably create 1 zip file and then after the ftp, unzip the file and all files should expand to their proper locations.

![]() |
Echo blank line from batc...
|
dos loop
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |