sanvin has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am new to the Perl and facing a problem while zipping a directory. When i run the code, i am getting an error like this " The stat preceding -1 _ wasn't an 1stat at c:/Perl/site/lib/Archive/Zip.pm line 2885." Below is the code. Please help me in this. ThanQ

#!/usr/bin/perl -w use strict; use warnings; use Archive::Zip qw(:ERROR_CODES :CONSTANTS); my $directory = 'D:\WorkArena\Data\Final build '; my $obj = Archive::Zip->new(); #Open Dir, Read it and Close it. opendir (DIR, $directory) or die $!; my @filearray = readdir(DIR); close(DIR); #Add the files from the array to $obj foreach my $file(@filearray) { $obj->addFile($file); # add files } #Write the files to zip. if ($obj->writeToFileNamed('dummy.zip') == AZ_OK) { # write to disk print "Archive created successfully!"; } else { print "Error while Zipping !"; }

Replies are listed 'Best First'.
Re: Error while Zipping a Directory
by GrandFather (Saint) on Aug 24, 2015 at 06:34 UTC

    I haven't tried reproducing your problem, but I notice that you don't filter '..' and '.' from @filearray which may cause unexpected results. Also, readdir returns entries relative to $directory which means you don't have a valid path for each entry unless $directory happens to be the current directory. Most often you will need to prefix each entry with $directory: "$directory\\$file".

    Premature optimization is the root of all job security
Re: Error while Zipping a Directory
by robby_dobby (Hermit) on Aug 24, 2015 at 06:45 UTC
    Hello sanvin,

    I notice that there's a trailing space in your directory name:

    my $directory = 'D:\WorkArena\Data\Final build ';

    As a side note, it would help if you include errors in the message itself. Probably, by overriding the default error handler(Carp::carp) or by incrementing the error level ($Carp::CarpLevel++). Have a look at all of it here.

    Good luck and have fun!

Re: Error while Zipping a Directory
by poj (Abbot) on Aug 24, 2015 at 08:13 UTC
    Try
    #!/usr/bin/perl use strict; use warnings; use Archive::Zip qw(:ERROR_CODES :CONSTANTS); my $directory = 'D:\WorkArena\Data\Final build'; my $obj = Archive::Zip->new(); $obj->addTree( $directory ); # Write the files to zip. if ($obj->writeToFileNamed('dummy.zip') == AZ_OK) { # write to disk print "Archive created successfully!"; } else { print "Error while Zipping !"; }
    poj
      ThanQ poj...It works fine. Have a good time.
Re: Error while Zipping a Directory
by Anonymous Monk on Aug 24, 2015 at 08:08 UTC

    What kind of filenames do you have?

    Try this , its based on https://metacpan.org/source/PHRED/Archive-Zip-1.49/examples/zip.pl with Path::Tiny for ease and autodie

    #!/usr/bin/perl -- ## dirzip.pl ## 2015-08-24-01:12:41 ## ## ## ## perltidy -olq -csc -csci=3 -cscl="sub : BEGIN END " -otr -opr -ce +-nibc -i=4 -pt=0 "-nsak=*" #!/usr/bin/perl -- use strict; use warnings; use Path::Tiny qw/ path /; use Archive::Zip qw(:ERROR_CODES :CONSTANTS); my $directory = 'D:\WorkArena\Data\Final build '; my $outfile = '... dummy.zip'; DirZip( $directory, $outfile ); exit( 0 ); sub DirZip { my( $indir, $outzip ) = @_; $outzip = path( $outzip )->absolute; $indir = path( $indir )->absolute; my $prefix = path( $indir )->absolute; my $zip = Archive::Zip->new(); for my $infile ( $indir->children ) { my $shortname = path( $infile )->relative( $prefix ); print "Trying to addFile( $infile , $shortname )\n"; if( $infile->is_file ) { $zip->addFile( "$infile", "$shortname" ) or warn "Can't ->addFile( $infile, $shortname ) "; } else { AZ_OK == $zip->addTree( "$infile", "$shortname" ) or warn "Can't ->addTree( $infile, $shortname ) "; } } print "Trying to ->writeToFileNamed( $outzip )\n"; my $status = $zip->writeToFileNamed( "$outzip" ); die "ERROR ->writeToFileNamed( $outzip ) , status == $status " if $status != AZ_OK; } ## end sub DirZip __END__
      ThanQ for ur reply. Have a nice time.