Subject: | Can't chdir() after reading a zip file with relative pathname |
The following code does not work:
my $file = "./foo/bar.zip";
my $zip = Archive::Zip->new;
$zip->read($file) == Archive::Zip::AZ_OK() or die "Can't read zip
file $file";
my $cwd = Cwd::cwd;
chdir($dir) or die "Can't chdir into $dir: $!";
$zip->extractTree() == Archive::Zip::AZ_OK() or die "Can't unzip
$file into $dir";
chdir($cwd) or die "Can't chdir into $cwd: $!";
The extractTree() call would print an error like this:
IO error: Can't open ./foo/bar.zip : No such file or directory
This happens because Archive::Zip doesn't keep a filehandle open in
the read() method but stores the filename and later tries to re-open
it when the current directory has changed.
You can work around it by only passing absolute filenames to read():
$zip->read(Cwd::abs_path($file));