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

Hi
I'm zipping a file using the archive-zip module
use Archive::Zip qw(:CONSTANTS :ERROR_CODES);
and it zips the entire path of the item i'm zipping in with the zip, so that when I expand my zip the target is buried under a bunch of folders representing the file path.

Is there a flag or command to tell the lib that I only want it to save the file in the zip archive, without the directory structure above it?
I'm calling it with:
zipname = C:\Program Files\xampp\htdocs\test\1.zip
memberName is C:\Program Files\xampp\htdocs\test\1.dat

I'm trying to execute the code from a cgi-bin, not in the same local directory as the files.
sub zipit(){ my ($zipName,$memberName)=@_; my $zip = Archive::Zip->new(); $zip->addFile( $memberName ); my $zipstatus = $zip->writeToFileNamed($zipName); }
It gives me back a zip with a folder heirchy staring at Program Files. At the bottom of the heirchy the file is there by itself and it's fine, but I need to ditch the heirchy above it.

Many thanks. Totally stumped.

Replies are listed 'Best First'.
Re: create zip without path struct
by ecuguru (Monk) on Jan 17, 2006 at 23:19 UTC
    For windows, passing it a relative location rather than an absolute location seems to have worked
    my $relPath = '..\\htdocs\\temp\\'; #To get from PerlScript to FileDirectory
      1. Maybe you knew, maybe you didn't: in Perl you can use / as the directory separator even under Windows. That way you won't have to quote all the time which is annoying, error prone, and degrades readability;
      2. not strictly related to Perl or Archive::Zip, but I would regard a relative path like the above as risky. How 'bout ./htdoc/temp/ instead?
Re: create zip without path struct
by idsfa (Vicar) on Jan 18, 2006 at 15:51 UTC

    Use the optional second argument for the Archive::Zip::addFile method:

    use File::Basename; sub zipit(){ my ($zipName,$memberName)=@_; my $zip = Archive::Zip->new(); $zip->addFile( $memberName, basename($memberName) ); my $zipstatus = $zip->writeToFileNamed($zipName); }

    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon