As documented, use the second parameter on the addFile() method to force a different name, which (not documented) can include a name with no path.
I leave it as an exercise for the reader to figure out how to determine the base filename without the path for any given file.
Sample attached.
#!/usr/bin/perl
use Archive::Zip;
use strict;
my $TRUE = 1;
my $FALSE = 0;
foreach my $addtyp (qw /yespath nopath/)
{
# Hard-code test values
my $os_nam = $^O; # Operating system name
my $unisep = '/'; # *nix directory separato
+r
my $winsep = "\\"; # Windows directory sepa
+rator
my $subdnm = "sub1"; # Subdirectory name
my $shtfnm = 'test.dat'; # Filename with no path
my $zipfnm = "test-$addtyp.zip"; # ZIP filename
my $zipobj = ''; # ZIP object pointer
my $newflg = $TRUE; # Was this a new ZIP fi
+le?
# Set up test environment
my $dirsep = $unisep; # Assum *nix until proven
+ otherwise
if ($os_nam =~ /windows|win\d+/i)
{
# Appears to be Windows
$dirsep = $winsep;
}
unlink $zipfnm; # Do not care if nonext
+ant
mkdir $subdnm; # Do not care if extant
my $addfnm = "$subdnm$dirsep$shtfnm"; # Filename with path to a
+dd
open ADDFIL, ">$addfnm";
print ADDFIL "This is a test.\n";
close ADDFIL;
# Perform test
$zipobj = Archive::Zip->new();
if ($addtyp =~ /yespath/)
{
my $addret = $zipobj->addFile($addfnm);
}
else
{
my $addret = $zipobj->addFile($addfnm, $shtfnm);
}
my $wriret = $zipobj->writeToFileNamed($zipfnm);
}
exit;
|