Solved

Sending a Zip file....

Posted on 1997-12-14
8
188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

717 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