Solved

Sending a Zip file....

Posted on 1997-12-14
8
186 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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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)

738 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