使用 Perl 同时为其创建 ZIP 存档和文件

Creating ZIP archive and files for it the same time with Perl

我有一个代码如下:

my $file = 'myFile.txt';
my $zip = Archive::Zip->new();
my $compressed = $zip->addFile($file, "newFilename");

if ($zip->writeToFileNamed('dir/test.zip') != AZ_OK)
{
    print "ERROR";
}
else
{
    print "DONE";
}

相当简单!

问题是:是否可以重写 $file = 'myFile.txt' 以在其中放置一个字符串,其中包含一些数据(即:“123455677889”)并且该数据必须与某个文件名相关联(例如:"myFile.txt") 在服务器上不存在。换言之:

  1. 我们有(获得或生成)一些$data = "12344566789abcselkjlkj";
  2. 然后我们以某种方式将此数据与新文件相关联,该文件尚不存在并且不得在服务器上存在(即使是 1 秒作为 _temp)。
  3. 最后一步 - 我们将这个 "virtual" 文件放入新存档中,即使这样也不写入磁盘,而是作为输出发送 (CGI)直接从网页发送给用户。

感谢您的帮助! 谢谢!

addString 做你想做的。

# Create a file in the archive from a string.
my $string_member = $zip->addString('12344566789abcselkjlkj', 'myFile.txt');
$string_member->desiredCompressionMethod(COMPRESSION_DEFLATED);