Более безопасный способ - использовать Archive::zip
, вот мой рабочий код, он должен дать вам представление о том, как это сделать.Модуль Archive::zip
также имеет некоторую информацию.
sub unpack_zipfiles
{
my $zipfile = shift; # Name of uploaded zip file
my $zip = Archive::Zip->new();
my $status = $zip->read($zipfile);
if ($status == AZ_OK )
{
my @members = $zip->memberNames();
my $n = 1;
foreach my $f (@members)
{
my @f2 = split(/\//,$f);
# Part 1 - get a filename, and save the file
my $zf = getNextFile($f2[$#f2]);
my $unzipped = qq{$unpack_dir/$zf};
$zip->extractMemberWithoutPaths($f,$unzipped);
# Part 2 - check if it's new or the same, and adjust the filename if necessary
$zf = check_or_revert($unpack_dir,$zf,$f2[$#f2]);
$n++;
push @msgs,qq{Unzipped file: $zf};
# Add file to list of unpacked file (for diagnostic purposes)
push @{$data{unpacked}},$unzipped;
# Unzipped files are plonked back in the file queue, because there is a loop that deals with them.
push @{$data{fileq}},$unzipped;
}
}
else
{
die "Error: $zipfile is not a valid zip file, Zip status=$status";
}
}