Important alert: (current site time 9/1/2014 9:30:39 PM EDT)
 

article

Server side compression using Zip or GZip

Email
Submitted on: 5/21/2000 10:34:37 AM
By: James Felton 
Level: Beginner
User Rating: By 8 Users
Compatibility: ASP (Active Server Pages)
Views: 31956
 
     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
 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				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*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 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 including:McAfee.com

 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.

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

12/13/2000 9:57:35 AMJim Dvorak

I can get this class to work on my Personal Web Server locally, but when I try it on our web server (running IIS on Win 2K OS) it does not work. I have placed the class file in the WINNT/JAVA/TRUSTLIB folder.
(If this comment was disrespectful, please report it.)

 
12/13/2000 9:59:48 AMJim Dvorak

I can get this java class to work on my local machine (using PWS), but when I put it out on our web server (IBM Netfinity running IIS on Win 2K), it does not work. Also, the only error message I get is "Couldn't create dl.zip" with no further explanantion, so I have no idea what is NOT happening. I did place the class file in the WINNT/JAVA/TRUSTLIB folder as directed.

Any ideas?
(If this comment was disrespectful, please report it.)

 
4/20/2001 11:32:29 AMhaihong

The gzip function doesn't work totally, the gzip file is saved, but can't be openned and it's empty. Zip function works in my personal web server. But the whole thing doesn't work for our web server.

Would anyone kindly help me to solve this problem?

Thanks in advance.

My email add is: hzhang@craworld.com

Haihong
(If this comment was disrespectful, please report it.)

 
6/21/2001 5:16:07 PMSebastien D'Errico

I evaluate it on the win98se IIS4 ( server test ) and after on the Win2000 server. Everything was works. So, I gave it a 5 on 5 because it works, it simple and the code seem nice !

Thanks for this code !
Chow chow !
Sebastien D'Errico.
(If this comment was disrespectful, please report it.)

 
3/20/2002 5:48:43 AMAja

Well I used it on IIS5 in Win2000 wihout any problems! Great deal! Aja
(If this comment was disrespectful, please report it.)

 
3/28/2002 10:16:44 AMFaith

What about to allow to use recusive compressing, and self-extractor?
(If this comment was disrespectful, please report it.)

 
4/16/2002 2:36:47 AMArthur Anab Shams

Dear James Felton
Your article has been a great help in one of my projects. Thankyou for this contribution.


(If this comment was disrespectful, please report it.)

 
7/5/2002 10:19:06 AMAndrew

I have it running great on a Win2K Pro test environment (Winnt\Java\TrustLib), copied it to the production, WinNT 4.0 Sp6 Server and I get an 'Invalid Syntax' error on the GetObject line -- comments, support or suggestions are much appreciated !!
Thanks...
(If this comment was disrespectful, please report it.)

 
7/5/2002 10:21:07 AMAndrew

I have this running on a Win2K Pro development environment - works GREAT !! Copy the application to the production WinNT 4.0 SP6 Server and I am getting an 'Invalid Syntax' error on the GetObject line -- any comments, suggestions or support would be appreciated.
Thanks...
(If this comment was disrespectful, please report it.)

 
10/3/2002 1:24:44 PM

We have a question regarding the code and hope the author will be able to answer it:

Our ASP application is running on a shared Windows 2000 server. Therefore, we don't have access to the system directory to install Java classes etc. Our directory does have execute permission. Is it possible to achieve the same functionality (unzip file via ASP) on a shared server by using this or any other approach?

Thank you in advance for you help!

Sincerely,

Misha Goussev
mishagoussev@e-t-i.com

(If this comment was disrespectful, please report it.)

 
11/9/2002 11:31:08 AM

Thank you alot, this code works great! Is there a way I can zip a whole folder instead of Just certin files?
(If this comment was disrespectful, please report it.)

 
4/22/2003 4:38:00 PM

Thank you alot, this code works great! Is there a way I can zip a whole folder instead of Just certin files? Use a file system object to list all the files in the directory, process it into the format file1*file2*file3 etc, and pass that string to the zip code. If you dont know how to use FSO to list files, search psc as there are plenty of examples. Anybody using this script dont forget that if you are using NTFS you will need to give file write permission to the IIS accounts on the folders in which the zip files will be placed. (Just thought id save you the 20 mins I spent on it :P) CIAO CIAO (not chow chow) -Adam
(If this comment was disrespectful, please report it.)

 
6/12/2003 6:29:28 AM

Thanks for the code, is there is any way to zip file for mac users?
(If this comment was disrespectful, please report it.)

 
11/4/2003 2:55:57 PM

Hey. I have this zip function working on my server, but I was wondering if there is any way I can modify the code you showed to do the following: when the zip file is downloaded, the items in the zip contain relative paths. This way the user can extract the file and it will create a folder hierarchy instead of placing everything in one folder. How can I do this? Thanks
(If this comment was disrespectful, please report it.)

 
12/19/2003 5:39:46 AM

I tried this code on my local machine having WinXP, it worked. But it doesn't work on Win 2003 server. It Just gives message error '800401e4' and no description for this error. I don't know why it is not working. Pleas help me.
(If this comment was disrespectful, please report it.)

 
2/26/2004 1:40:12 PM

Great code, I have the same need as the person who posted on 11/4/2003, the ability to save full path info in the zip file so that when files are unziped they get placed in the correct directory. Any Ideas?
(If this comment was disrespectful, please report it.)

 
9/27/2004 10:23:20 AM

I am also upgrading to windows 2003 server and am getting the same error '800401e4'. Is there anyway round this problem? Many Thanks..
(If this comment was disrespectful, please report it.)

 
12/2/2004 12:31:46 PM

I am also upgrading to windows 2003 server and am getting the same error '800401e4'. Is there anyway round this problem? Many Thanks.
(If this comment was disrespectful, please report it.)

 
2/28/2005 11:05:39 AM

I am using windows 2003 server and get error '800401e4'. Could you tell me how to solve this problem, Thank you very much.
(If this comment was disrespectful, please report it.)

 
3/4/2005 1:05:10 PM

What is the latest version of java that this supports? I need to install it on the live server.
Thank you, Andre
(If this comment was disrespectful, please report it.)

 
4/13/2005 4:50:23 PM

I too had this problem - put class file in relevant folder on win 2003 server, ran the asp test page, error happened. (1) found that in fact Java was not installed on my server. (2) installed Java from Sun - same problem (3) removed sun Java (4) installed MS Java (had a copy laying round on network) (5) rebooted (6) test passed. SO! I can't say whether it was the reboot or the installation of MS Java that did it . . .
(If this comment was disrespectful, please report it.)

 
4/26/2005 3:55:33 AM

If you are getting error '800401e4' using ZipFunctions.class in Windows 2003, then you should install MSJAVA and reboot. This should solve your problem.
(If this comment was disrespectful, please report it.)

 
11/16/2006 11:15:03 PMsoftwaredesigns

I am trying to run this on windows 2003 server and I have registered the class file in windows/java/trustlib and also used javareg to register it. I still get the error 800401e4 without any messages. I have seen many in the list have been getting this error.
Please advise as to how to correct this.
Thanks
(If this comment was disrespectful, please report it.)

 
12/27/2006 2:14:42 PMKrishna

Please let me know what will be code for following situation.
there are 5 files in the directory as under
file1.txt
file21.txt
file3.txt
file41.txt
file5.txt

Now, Suppose I select 2 files (file21.txt, file41.txt) on a condition thru SQL command in ASP( I'm collecting thru Record Set RS ).

Please help me so that the component zips the selected files.

Thanks

Krishna

(If this comment was disrespectful, please report it.)

 

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.