Op 21-10-16 om 14:13 schreef Vincent Lequertier:
This code creates a zip file which contains several files, all named
'test'. The thing I'm trying to achieve is to have all the output of
commands being appended to a 'test' file, into the zip.
How can I do this properly ? I mean without having to create a temp file
with a filehandle with all the output and adding the file to the zip
after the for loop.
A zip-file is no compressed filesystem, so I think there is no way to do
what you want properly.
You could do something like this, but in the else block you will have
all of your data temporarily uncompressed in memory:
if (! $zip->memberNamed( 'test' )) {
$zip->addString($config, 'test', COMPRESSION_DEFLATED);
}
else {
$zip->contents( 'test', $zip->contents( 'test' ) . $config );
}
If it's too much data to fit in memory you have to go with the temp file
option.
Just a thought though: perhaps a zip archive to compress just one file
is a bit overkill.
Maybe IO::Compress::Gzip, which let's you compress strings in this way:
gzip \$input \$output
is better suited, or could at least be used in stead of the temp file.
Greetings,
Eric de Hont