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

Is it possible to set a temporary zip file for a text or an application file so it kind of compresses itself right before it downloads?

I won't use modules that don't come with the Perl package itself, so that might make it hard but the zip file should only be valid during the download of the file and discarded afterwards. I don't need two coppies of each thing on my server /the file and the zip file/, so when there's no reason to save the zip at all.

Is this possible?

Replies are listed 'Best First'.
Re: temporary file zipping
by Zaxo (Archbishop) on Apr 04, 2004 at 05:06 UTC
    Is it possible to set a temporary zip file for a text or an application file so it kind of compresses itself right before it downloads?

    Yes, many ways. It is possible to get httpd to serve files in compressed form for transmission. That is up to the client. You can call zip and unzip with system. Multiple layers of compression are usually a waste of time.

    I won't use modules that don't come with the Perl package itself, so that might make it hard...

    Indeed. Trade some excess hubris for a little laziness and obtain Archive::Zip. Compress::Zlib is also usable, but more complicated for winders zip files.

    With perl 5.8, you could write a PerlIO::via::Zip layer and be a minor demigod.

    If you don't want to keep a temporary file, the easiest thing is to unlink it when you are done with it.

    After Compline,
    Zaxo

Re: temporary file zipping
by demerphq (Chancellor) on Apr 04, 2004 at 07:51 UTC

    I won't use modules that don't come with the Perl package itself,

    Thats a policy that removes a big reason for using Perl, and will severly disadvantage you in later releases when "slim perl" becomes the rollout mechansim. When this happens it will mean that as many of the modules currently bundled with perl will be removed and used from CPAN or from secondary follow up bundles.

    If security is a problem to you then the perl porters are hoping to address that with a MGRAS list (modules generally recognized as safe *g*). Joking aside there are back burner plans to slim down what perl comes with, but enhance the list of what the perl porters consider to be valuable modules that havent been put in core becuase its too big already.

    You should know that by restricting yourself to core modules you are restricting yourself to a very limited subset of the codebase on CPAN that the perl porters consider worthy enough to be included. Its just not feasable to make it bigger, in fact a lot of people complain its too big as it is.


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi


Re: temporary file zipping
by Anonymous Monk on Apr 04, 2004 at 05:04 UTC

    I've got Archive::Zip installed on my system and do not remembering installing it, so I am not sure if it is a core module. If so, something like this should suit you fine. This script is even tested ;)

    #!c:/perl/bin/perl -w $|++; use strict; use CGI::Simple; use Archive::Zip; my $fileToUpload = 'foobar.txt'; my $CGI = CGI::Simple->new(); 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 $CGI->header( -type => 'application/zip', -attachment => "$fileToUpload.zip" ); binmode( STDOUT ); # once again, for win32 $zip->writeToFileHandle(\*STDOUT);
Re: temporary file zipping
by matija (Priest) on Apr 04, 2004 at 05:23 UTC
    I know it doesn't come with Perl package, but perhaps you would consider an Apache module?

    Look for mod_gzip - it can compress files on the fly (or it can cache them), and it has ways for you to specify which files should and which shouldn't be compressed before they are sent out.