Solved

Sending a Zip file....

Posted on 1997-12-14
8
176 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
The viewer will learn how to count occurrences of each item in an array.
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)

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now