• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

Sending a Zip file....

Hi!
I need to write a CGI that outputs a ZIP file...
What HTTP header do I have to send to the browser?
And after that, how do I send the Zip file?
I open the file as binary but how do I send all it's contents to the broser?

Thanks
Roger
0
rtavares
Asked:
rtavares
  • 4
  • 4
1 Solution
 
alamoCommented:
The specific content-type header is not crucial and there's no "standard" really, aplication/octet-stream should be fine (experiment if you want). Here's a script which does what you want:

$file= "/path/filename.zip";
if (!open(ZIP, "<$file")) {
 print "Content-type: text/plain\n\nCan't open .zip file $file: $!\n";
 die;
}
binmode ZIP;
binmode STDOUT;

# Type header - application/octet-stream is most generic, though many
# servers use something different, take your pick:
#  application/zip
#  application/x-xip
#  application/x-zip-compressed
#
print "Content-type: application/octet-stream\r\n";

# set the download filename (for some browsers)
print "Content-disposition: filename=\"filename.zip\"\r\n";

# Be nice and get the file length if we can, so user knows time left
$ziplen= (stat(ZIP))[7];      
print "Content-length: $ziplen\r\n" if $ziplen;

print "\r\n";
$blocksize = 2048;
while (!eof(ZIP)) {
 read ZIP,$zipdata,$blocksize;
 print $zipdata;
}
close(ZIP);

Let me know if you have any problems with this - good luck!
0
 
rtavaresAuthor Commented:
Thanks for the help...
But I still have a problem....
How do I do that on C? I can't send binary data do the browser! I only know how to get chars from the file....

Thanks,
Roger

0
 
alamoCommented:
Oops, sorry, I see "CGI" and think "Perl" for most things... at least the concept was laid out.

But... why can't you send binary data to the browser? That's exactly what you need to do. You may need to put STDOUT into binary mode, though I haven't used C for CGI work so am not certain. Post the code that doesn't work here and I'll try to help.
0
[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

 
rtavaresAuthor Commented:
My code is something like this:

#include <stdio.h>
#include <stdlib.h>

#define ZIPFILE "sendzip.zip"

void main()
{
     FILE *fp;
     int c;

     if ( (fp = fopen(ZIPFILE,"rb")) == NULL) exit(1);

     printf("application/octet-stream; name=\"file.zip\"\r\n");
     printf("Content-Lenght: 13664\r\n");
     printf("Content-Transfer-Encoding: binary\r\n");
     printf("Content-Disposition: inline; filename=\"file.zip\"\r\n\r\n");

     while ((c = getc(fp)) != EOF) putc(c, stdout);

}

What I recive on the browser is a page with the content of the ZIP file...
I don't know how to put stdout into binary mode. And maybe I shouldn't use putc and getc...

Thanks,
Roger
0
 
alamoCommented:
I see two header problems:

printf("application/octet-stream; name=\"file.zip\"\r\n")
should be
printf("Content-type: application/octet-stream\r\n")

and in "Content-Lenght:" Length is misspelled.

Fixing those two (esp. the first) should solve your problem.
0
 
rtavaresAuthor Commented:
Finally it work!
The first problem was wrong because I send you (by mistake) a test code because someone told me that IE4 ignors the "Content-Type: " and I wanted to check...
I changed the "lenght" to "length" (stupid mistake) and it worked! I didn't thounght that "Content-Length" was really necessary...

But I have a new problem... When the Save window appears and I click ok, the default name to the file is strange (something like CACHZG.). Can I change that name (using the CGI, of course!)? I thougth that name="file.zip" on the header was to do that...

Thanks again,
Roger
0
 
alamoCommented:
IE doesn't seem to pay attention to the filename parameter, I know of no way to make IE pick the right name. The closest you can get is to get the right extension, at least, by changing the Content-type from "application/octet-stream" to "application/x-zip-compressed". Assuming the user has .zip associated with x-zip-compressed (which seems common on Windows, try it on your system) then the filename IE suggests will end in .zip.

The drawback to that is that if you make that change, on Netscape instead of the "Save As" box popping up immediately, the same sort of "Do you want to open this file, or save it to disk?" dialogue as on IE opens up. You could detect which is which, I guess, and use one set of headers for Netscape and another for everyone else. A pain, though - unfortunately the pace of web development is so fast that "standards" aren't really standard.
0
 
rtavaresAuthor Commented:
ok...
Thanks for your help!
You really help me alot!

Thanks
Roger Tavares
l42495@alfa.ist.utl.pt

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now