The IO::Compress::Zip docs say you can pass a reference to a scalar to get the data written there. This will use more memory, but here's an example that includes the Content-Length header:

#!/usr/bin/perl use warnings; use strict; use CGI::Simple; use IO::Compress::Zip qw/$ZipError/; use Encode qw/encode/; my @lines = (qw/ Hello World Foo Bar /, "\N{U+1F42A}"); my $CRLF = "\r\n"; my $encoding = "UTF-8"; # or maybe "CP1252" for Windows my $zipdata; my $z = IO::Compress::Zip->new(\$zipdata, Name => "Filename.txt" ) or die "zip failed: $ZipError\n"; for my $line (@lines) { $z->print( encode($encoding, $line.$CRLF, Encode::FB_CROAK|Encode::LEAVE_SRC) ); } $z->close(); my $q = CGI::Simple->new(); print $q->header( -type => 'application/zip', -Content_Disposition => qq{attachment; filename="Download.zip"}, -Content_Length => length($zipdata) ); binmode STDOUT; # just to play it safe print $zipdata;

As for the code you showed, I'd strongly recommend proper indentation (perltidy can help with that). I also have the feeling that you might not be using strict and warnings, or that you're using too many global variables (e.g. $statement). And a nitpick: I wouldn't use $CRLF for outputting the HTTP headers, "\n" is enough, every webserver I've worked with understands that (but really, you should be using a module to write the headers anyway, as I showed above).

Also, as I mentioned, you can save the memory of the @resp array by merging those two loops. For example:

$z->print( encode( $encoding, "RECORD#\tREVISION#\tBOOK#\tCHAP#\tVERSE +#\tTEXT, AS EDITED BY: $curdate $curtime (Pacific Time)$CRLF", Encode +::FB_CROAK|Encode::LEAVE_SRC ) ); while ( ( $recnum, $revnum, $book, $chap, $verse, $text ) = $quest->fe +tchrow_array() ) { $z->print( encode( $encoding, "$recnum\t$revnum\t$book\t$chap\t$ve +rse\t$text$CRLF", Encode::FB_CROAK|Encode::LEAVE_SRC ) ); }

If any of the fields you're outputting could possibly contain tabs, you might want to switch to Text::CSV for proper handling of all of those complexities.


In reply to Re^5: Correct Perl settings for sending zipfile to browser by haukex
in thread Correct Perl settings for sending zipfile to browser by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.