Вот пример использования perl (с модулем IO :: Compress :: Zip) для отправки сжатого файла на лету, когда @deceze указывает на
use IO::Compress::Zip qw(:all);
my @files = ('example.gif', 'example1.png'); # here are some files
my $path = "/home/projects/"; # files location
# here is the header
print "Content-Type: application/zip\n"; # we are going to compress to zip and send it
print "Content-Disposition: attachment; filename=\"zip.zip\"\r\n\r\n"; # zip.zip for example is where we are going to zip data
my $zip = new IO::Compress::Zip;
foreach my $file (@files) {
$zip->newStream(Name => $file, Method => ZIP_CM_STORE); # storing files in zip
open(FILE, "<", "$path/$file");
binmode FILE; # reading file in binary mode
my ($buffer, $data, $n);
while (($n = read FILE,$data, 1024) != 0) { # reading data from file to the end
$zip->print($data); # print the data in binary
}
close(FILE);
}
$zip->close;
Как вы видите в сценарии, даже если вы добавляете имя файла zip в заголовок, это не имеет значения, потому что мы архивируем файлы и печатаем их в двоичном режиме сразу, поэтому нет необходимости архивировать данные и сохранить их, а затем отправить их клиенту, вы можете напрямую сжать файлы и распечатать их без сохранения.