Arsenii
Arsenii

Reputation: 774

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

I have a code as following:

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";
}

Pretty much easy!

The question is: is it possible to rewrite $file = 'myFile.txt' to put there a string, which contains some data (i.e.: "123455677889") and this data must be associated with some filename (say: "myFile.txt") which does NOT exist on the server. The other words:

  1. we have (obtain or generate) some $data = "12344566789abcselkjlkj";
  2. then we associate this data in some way with new file, which does not exist yet and must NOT exist ever on the server (even 1 second as a _temp).
  3. The last step - we put this "virtual" file into the new archive and even then do not write on the disk, but sending as an output (CGI) to the user right from the webpage.

Every help is appreciated! Thank you!

Upvotes: 2

Views: 389

Answers (1)

Chankey Pathak
Chankey Pathak

Reputation: 21666

addString does exactly what you want.

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

Upvotes: 2

Related Questions