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

Does anyone know how to get wzzip to work in a cgi script?

I have written a script that creates an excel file and then zips it up using wzzip.exe. I have found that I cannot return both the HTML page to the user and create a zip file and generate an email (using MIME) with the attached zip file.

If I first return the html page it works, as does the generation of the zip file ... but the email does not arrive. If I first create the zip file and the email it returns the email with the zip file but I get the CGI Error "The specified CGI application misbehaved by not returning a complete set of HTTP headers." If I comment out the command line calls (either of them as shown below) I can do both things and attach a previously generated zip file. I want to use wzzip as most users will have winzip and when I generated zip files using the archive zip module I could not use winzip to open them. Running Wzzip I have used the following code:
1. my $zippath = qq(\"C:\\Program Files\\WinZip\\wzzip.exe\" \-a \"$zi +p_file\" \"$filetoZip\"); my $status = system ($zippath);
2. my $winzip= qq(\"c:\\program files\\winzip\\wzzip.exe"); open OUT, "| $winzip $zip_options $zip_file $filetoZip" or die "wzzip pipe: $!"; print OUT "\n"; close OUT;
I think it may have something to do with winzip changing the stdout or stderr ... but not sure how to stop this. I would really appreciate a perl monk to help me. Thankyou, Hshepherd

Replies are listed 'Best First'.
Re: CGI Wzzip and MIME
by UnstoppableDrew (Sexton) on Oct 03, 2007 at 14:18 UTC
    The incomplete headers message is generally an indicator that your script produced some output, which Apache sent to the browser. Be sure to capture stdout/stderr of any programs you execute. You can see this if you run your script on the command line. If any output is produced before the script outputs the "Content-type: text/html\n\n" it will cause that error.
Re: CGI Wzzip and MIME
by arcnon (Monk) on Oct 03, 2007 at 14:50 UTC
    print OUT "\n";
    you open a file to print a single return and close? should be like:
    print OUT $zip_data;
Re: CGI Wzzip and MIME
by hshepherd (Initiate) on Oct 03, 2007 at 23:18 UTC
    Thankyou both for your help ... but I don't want to return the wzzip.exe response as an HTML page ... I have another page that I return. I use the code
    print "Location: $url1\n"; print "\n";
    to do this and this is somehow being corrupted by the wzzip command line response when it outputs that it has added the file to the zip folder. I have tried adding the line Content-type: txt/html\n\n prior to the print location syntax ... but it still does not work. I think the answer lies in using filehandles .. but have got a bit stumped about the syntax I need to use. I have tried not using Print after running the wzzip.exe ... but this does not work either and causes the browser to freeze until it times out. Thankyou again for your responses.
      Managed to get it to work finally by replacing the print location url reference with code as shown below and by redirecting (prior to excuting wzzip.exe) the STDOUT and STDERR to text files.
      print "Content-type: text/html\n\n"; print "<HTML><HEAD>\n"; print "<TITLE>CGI Test</TITLE>\n"; print "</HEAD>\n"; print "<BODY><p>Blah Blah. For another enquiry <A HREF=\"http: +//www.something.com/somepage.html\">Click Here</A>\n"; print "</BODY></HTML>"; my $stdout = "stdout.txt"; my $newstdout = 'C:\SomeDir\SomeSubDir/' .$stdout; my $stderr = "stderr.txt"; my $newstderr = 'C:\SomeDir\SomeSubDir/' .$stderr; # redirect STDOUT & STDERR open (my $oldstdout, ">&STDOUT") or die "Can't open old STDOUT: $!"; open (my $oldstderr, ">&STDERR") or die "Can't open old STDERR: $!"; open (STDOUT, ">$newstdout") or die "Can't open STDOUT: $!"; open (STDERR, ">>$newstderr") or die "Can't open STDERR: $!"; my $zip_options = "-a"; open OUT, "| $winzip $zip_options $zip_file $filetoZip" or die "wzzip pipe: $!"; print OUT "\n"; close OUT;
      This then captured STDOUT from WZZIP rather than the program trying to send it as HTML page. I sent the HTML page back prior to wzzip ... I realise to some purists this is a terrible thing to do but as it is a repeatable job & text files can be checked for errors & it speeds up processing what the heck!