Server side compression using Zip or GZip

Submitted on: 1/5/2015 7:02:00 AM
By: James Felton (from psc cd)  
Level: Beginner
User Rating: By 8 Users
Compatibility: ASP (Active Server Pages)
Views: 10582
     This article describes how to use the ZipFunctions java component for ASP (provided with the article) to compress multiple files into the .zip format, or one file into a .gz file with all processing being done on the server.

This article has accompanying files
				In many large scale or user-based ASP projects, a large number of files need to be made available for download. It is inconvienient 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 unaccessable, or what if the number of files is going to greatly increase every day? 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 (see bottom of this article) 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
 set javaObject = GetObject("java:ZipFunctions")
 strResult = javaObject.test()
 response.write strResult
 set javaObject = nothing

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

GZipping a file
To GZip a file, simply use the code below

 Dim strResult
 set javaObject = GetObject("java:ZipFunctions")
 strResult = javaObject.GZipFile(Server.mappath("/asp/zipme.txt"), Server.mappath("/asp/zipme.txt.gz"))
 response.write strResult
 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.

Zipping files
Zipping multiple files requires a slightly different procedure. The files are all passed as a string, with each filename being seperated 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*

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

 Dim strResult, theFiles
 theFiles = Server.mappath("/asp/somefile.txt") & "*" & Server.mappath("/asp/")
 set javaObject = GetObject("java:ZipFunctions")
 strResult = javaObject.ZipFile(theFiles, Server.mappath("/asp/"))
 response.write strResult
 set javaObject = nothing
This compresses the file somefile.txt and into the file Once again, if 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 which was written.

Potential Uses
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.

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.