Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

The documentation doesn't seem hard to follow, it just doesn't seem to specify, that I can see, a few things. First being, how do you set the name of the zip file? It shows how to read an already made zip file and it give this next line as an example, thing is it doesn't explain if this is how you assign the name of the zip file itself. It just gives you the code.
my $zip = Archive::Zip->new('somefile.zip');
I don't want to keep a zip file of the files, I'd rather have it temporary and forgotten at the time it loads. I know I can unlink the zip file afterwards, but it looks like it might have what I'm looking for. This is hard for me to understand, will this create a temporary zip file that will disappear when the script ends?
Archive::Zip::tempName( [$tmpdir] ) Create a unique name for a temp file, in the given directory or in the + first one of: /tmp $ENV{TMPDIR} $ENV{TEMP} . Archive::Zip::tempFile( [$tmpdir] ) Create a uniquely named temp file. It will be returned open for read/w +rite. If $tmpdir is given, it is used as the name of a directory to c +reate the file in. If not given, creates the file in the first direct +ory found in this list: /tmp $ENV{TMPDIR} $ENV{TEMP}
Basically all I want to do is when they go to my script, it makes a zip file for a given $filename and pushes it as a download box so they can download it. So not only do I need to create a zip file and remove it (or not ever store it) but at the same time push it to the user to download.

Can someone show me an example of how I would do any part of this? Compression isn't needed, just the fact it's a zip file will work. This is what I've got so far.

my $member = "/home/name/www/images/sunset.png"; my $zip = Archive::Zip->new('newfile.zip'); $zip->addMember( $member );

Replies are listed 'Best First'.
Re: Examples of Archive::Zip
by BUU (Prior) on Apr 06, 2004 at 08:20 UTC
    Acccording to the docs, the following code should do what you want (untested of course):
    my $zip = Archive::Zip->new(); $zip->addFile($file); $zip->writeToFileHandle(*STDOUT);
    I didn't see an option to get it to just give you the zip file in a scalar or something, so you'll have to either write your own FakeFileHandleAccumulator or get one off cpan (IO::ALL looks like it might do it easily), but for just sending the output of your zip file via a cgi script, write to file handle should do what you need.

      Since s/he is planning to output this to the browser, you'd think the writeToFileHandle() method will suffice (to STDOUT). If s/he does wind up needing it in a scalar (for some perverse reason, I can think of no good reason for needing this), s/he could use something like this to get the contents of $zip into $scalar:

      # recent perl's: open( my $fh, '>', \my $scalar ) or die "open failed: $!"; $zip->writeToFileHandle($fh); # recent & odler perl's: use IO::Scalar; # or: # use IO::String (apparently more recent than IO::Scalar) my $fh = IO::Scalar->new(\my $scalar); $zip->writeToFileHandle($fh);
Re: Examples of Archive::Zip
by Anonymous Monk on Apr 06, 2004 at 08:16 UTC

    You just posted this (more or less) same question 2 days ago (I am assuming that you are the author of temporary file zipping). If you noted the example I provided, you have the answer to your question. The script I provided is the backbone of what you need. It creates a new in-memory zip file, sends it to the user for download and then exits, without saving the zip file anywhere.

      Thank you for YOUR reply! I tried using this and it kept erroring out. I ended up having to use CGI qw/:standard/ instead of Simple, but those little changes shouldn't be causing the problem.

      I uploaded foobar.txt in the same folder using an absolute path and at run time I get PK=8†0 foobar.txtadadasddsC™ PK=8†0C™ ¶foobar.txtPK8=. This proves two things though. It proves it can find the foobar.txt and is goofing around with it and also proves it can read it "adadasdds" is the entirety of the text file.

      I get this text message to browser, it doesn't die or anything and no popup window exists. What should I do?

      #!/usr/bin/perl use warnings; use CGI::Carp 'fatalsToBrowser'; $|++; use strict; use CGI qw/:standard/; use Archive::Zip; my $fileToUpload = 'foobar.txt'; my $zip = Archive::Zip->new(); open( my $fh, '<', $fileToUpload ) or die "open failed: $!"; binmode( $fh ); # for win32 my $contents = do { local $/; <$fh> }; close( $fh ); $zip->addString($contents, $fileToUpload); print header( -type => 'application/zip', -attachment => "$fileToUpload.zip" ); binmode( STDOUT ); # once again, for win32 $zip->writeToFileHandle(\*STDOUT);