Creating Zip Files on the FlyBy James Felton
In many large scale or user-based ASP projects, a large number of files need to be made available for download. It is inconvenient for users to have to download files one at a time as this would take a lot of their time and care to do so. A solution? Zip the files. Normally this needs to be done manually - but what if it is not possible to do this? What if the server is going to be inaccessable, or what if the number of files is going to greatly increase every day, or the users can select a dynamic number of files to download? Ladies and Gentlemen, I bring you the ZipFunctions component!
What is it?
It is an open-source component written in Java. It is completely free, and grants the magic ability to gzip a single file, or zip one or more files. It has been tested thoroughly under PWS and IIS 4.0.
Installing the component
First download the component and then copy the
ZipFunctions.class file into the
directory. If you are a Java developer, then you may want to edit the source code. When
it comes to compiling, simply compile as normal (optimise with the
if you are using Sun's JDK) and then copy the file into
Using the component
A testing function is integrated into the component. This allows you to check to see if the component installed correctly. To use it the code is as follows:
if the component was installed correctly, then you should see the text:
as an output.
GZipping a file (gzip if a format usually used on UNIX boxes)
To GZip a file, simply use the code below:
This will compress the file
zipme.txt into the file
No files get deleted in the process, although if there is already a
it will be overwritten. Only one file can be GZipped at a time. If an error occurs, it
will be stated in
strResult. If everything goes to plan, then
strResult will read
Created filename. where filename
is the file which was written to.
To create a link to the file, you must hack through this output a bit. The following
code demonstrates how to take the output of a successful
output a link to the newly created GZip file:
Simply replace the
response.write strResult in the GZip example with
the code seen above to output a link to the newly created GZip file.
Zipping multiple files requires a slightly different procedure. The files are all passed as a string, with each filename being separated by an asterisk (
*). If only
one file is passed, no asterisk is needed, so the string could be something like:
file1.gif. If two files are passed then one asterisk is needed and the
string would look something like this:
file1.gif*file2.htm. Adding another
file, another asterisk is needed and the string would become
To create the zip file, the following code is used:
This compresses the file
zipfile.zip. Once again, if
exists, it will be overwritten. Any errors will be reported in
Again, if no errors occur, then
strResult will read
Created filename. where filename is the zip file that was
written. Finally, if you want to display a link to the newly created zip file, simply
response.write strResult line in the above code with:
Potential Uses and Updates
There are a large number of uses for this component, but beware - zipping takes up a lot of processing, and the more large files you compress, the longer it will take. Constant zipping could cause problems to your server if it is not very powerful.
It would be fairly simple to zip a whole directory, certain files, databases, etc. The type of site that ZipFunctions will be used on is the only thing that can determine the potential uses of the component.
Updates will be few and far between. The next version of ZipFunctions will come out in about 5-6 weeks and will be available from http://www4.ewebcity.com/javaasp.