To read the article online, visit http://www.4GuysFromRolla.com/webtech/030800-1.shtml

Creating Zip Files on the Fly

By 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 windows\java\trustlib 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 -O flag if you are using Sun's JDK) and then copy the file into windows\java\trustlib.

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:

<%
 Dim strResult

 'Create an instance of the component
 set javaObject = GetObject("java:ZipFunctions")
 
 'Execute the test method and output its result
 strResult = javaObject.test()
 response.write strResult
 
 'Clean up!
 set javaObject = nothing
%>

if the component was installed correctly, then you should see the text: test ok! as an output.

GZipping a file (gzip if a format usually used on UNIX boxes)
To GZip a file, simply use the code below:

<%
 Dim strResult
 
 'Create an instance of the component
 set javaObject = GetObject("java:ZipFunctions")
 
 'GZip a file using the GZipFile method
 strResult = javaObject.GZipFile(Server.mappath("/asp/zipme.txt"), _
                             Server.mappath("/asp/zipme.txt.gz"))

 'Output strResult
 response.write strResult

 'Clean up!
 set javaObject = nothing
%>

This will compress the file zipme.txt into the file zipme.txt.gz. No files get deleted in the process, although if there is already a zipme.txt.gz 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 strResult and output a link to the newly created GZip file:

 'Display a link to the newly created GZip file
 Dim strLink
 strLink = Replace(Replace(strResult, "Created " & _
                 Server.MapPath("/"), ""), "\", "/")
 strLink = Left(strLink, Len(strLink) - 1)
 Response.Write "<A HREF=""" & strLink & """>"
 Response.Write strLink & "</A>"

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 files
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 file1.gif*file2.htm*file3.zip.

To create the zip file, the following code is used:

<%
 Dim strResult, theFiles
 theFiles = Server.mappath("/asp/somefile.txt") & "*" & _
                    Server.mappath("/asp/anotherfile.zip")
 set javaObject = GetObject("java:ZipFunctions")
 strResult = javaObject.ZipFile(theFiles, _
                    Server.mappath("/asp/zipfile.zip"))
 response.write strResult
 set javaObject = nothing
%>

This compresses the file somefile.txt and anotherfile.zip into the file zipfile.zip. Once again, if zipfile.zip already exists, it will be overwritten. Any errors will be reported in strResult. 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 replace the response.write strResult line in the above code with:

 'Display a link to the newly created GZip file
 Dim strLink
 strLink = Replace(Replace(strResult, "Created " & _
                 Server.MapPath("/"), ""), "\", "/")
 strLink = Left(strLink, Len(strLink) - 1)
 Response.Write "<A HREF=""" & strLink & """>"
 Response.Write strLink & "</A>"

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.

Happy Programming!


Attachments

  • Download ZipFunctions.class
  • Visit the Java-ASP HomePage


  • Article Information
    Article Title: Creating Zip Files on the Fly
    Article Author: James Felton
    Published Date: Wednesday, March 08, 2000
    Article URL: http://www.4GuysFromRolla.com/webtech/030800-1.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers