Solved

Sending a Zip file....

Posted on 1997-12-14
8
182 Views
Last Modified: 2013-12-25
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
Comment
Question by:rtavares
  • 4
  • 4
8 Comments
 
LVL 6

Accepted Solution

by:
alamo earned 40 total points
ID: 1831650
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
 

Author Comment

by:rtavares
ID: 1831651
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
 
LVL 6

Expert Comment

by:alamo
ID: 1831652
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: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

Author Comment

by:rtavares
ID: 1831653
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
 
LVL 6

Expert Comment

by:alamo
ID: 1831654
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
 

Author Comment

by:rtavares
ID: 1831655
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
 
LVL 6

Expert Comment

by:alamo
ID: 1831656
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
 

Author Comment

by:rtavares
ID: 1831657
ok...
Thanks for your help!
You really help me alot!

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

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question