Introduction
Recently I had to build a private online photo album application. I planned to provide the users option to upload images as a zip file. I needed this option to be implemented in ASP.Net 1.1, as my hosting service provider didn't have .Net 2.0 support. Online search lead me to a blog with this wonderful idea of using the zip option in J# and it really attracted me. I did want to try it out and the results were pretty cool. Even though this sample application has been written in .NET 2.0, .NET framework 2.0 has libraries to work with compressing and uncompressing of files. I will soon come up with an article to illustrate the use of .NET 2.0 libraries for compress and uncompressing functionalities.
In this article I will explain the usage of zip functionality in J# from C# code. The code in this application has been designed to reuse in a copy paste fashion and not as a library. Background this application consumes J# classes internally. For this we must first refer to the J# .NET library. Physically it resides as a file named vjslib.dll.
If you are not very sure how to refer to a library in your project please follow the below steps:
Right click your project in Server Explorer and click on "Add Reference" -> Select the .Net tab -> Scroll down and select "vjslib" -> Click OK and you are there.
Now you can refer the Java library classes within your application. In fact this was the first time I am trying to refer to the J# classes and personally it was a moment I would never forget in my programming life. May be thrilled about the usage of the whole power and options of Java language within my C# programs(if need arises). Import the following namespaces for ease of coding.
java.util;
java.util.zip;
java.io;
java.util.zip
These namespaces contain the classes and methods to implement the compress and uncompress functionalities within our code. The main classes used from the above namespaces are ZipFile, ZipEntry and ZipOutputSteam Enumeration Programmatically, a ZipFile object can be considered equivalent to a physical Zip file.
A ZipFile can contain multiple ZipEntry objects apart from the actual content of the zipped files. In fact each ZipEntry object is the metadata about a zip file. The ZipOutputStream class represents a writable stream pointing to a ZIP file. This stream can be used to write ZipEntry objects and content to the ZIP file. Enumeration enables iteration through each element in a collection.
Using the code below is a code listing to create a zip file.
private void Zip(string zipFileName, string[] sourceFile)
{
FileOutputStream filOpStrm = new FileOutputStream(zipFileName);
ZipOutputStream zipOpStrm = new ZipOutputStream(filOpStrm);
FileInputStream filIpStrm = null;
foreach(string strFilName in sourceFile)
{
filIpStrm = new FileInputStream(strFilName);
ZipEntry ze = new ZipEntry(Path.GetFileName(strFilName));
zipOpStrm.putNextEntry(ze);
sbyte[] buffer = new sbyte[1024];
int len = 0;
while ((len filIpStrm.read(buffer)) > = 0)
{
zipOpStrm.write(buffer, 0, len);
}
}
zipOpStrm.closeEntry();
filIpStrm.close();
zipOpStrm.close();
filOpStrm.close();
}
The above Zip() method accepts two parameters, zipFileName - Zip file name including the path and sourceFile - string array of file names that are to be zipped. The FileOutputStream class is capable of writing content to a file. Its constructor accepts the path of the file to which we wish to write. FileOutputStream object is then supplied to an Instance of ZipOutputStream class as parameter. The ZipOutputStream class represents a writable stream to a ZIP file. The foreach loops through each file to be zipped, creates corresponding zip entries and adds it to the final zip file. Taking a deeper look into the code, a FileInputStream object is created for each file to be zipped. FileInputStream object is capable of reading from a file as a stream. Then a ZipEntry object is created for each file to be zipped. The constructor of ZipEntry class accepts name of the file.
Path.GetFileName() returns the file name and extension of the specified path string. The newly created ZipEntry object is added to the ZipOutputStream object using its putNextEntry() method.
In fact, a ZipEntry merely represents metadata of file entry. You still need to add actual contents into the Zip file. Therefore you need to transfer data from source FileInputStream to destination FileOutputStream. This is exactly what the while loop does in the above piece of code. It reads content from the source file and writes it into the output zip file. Finally, closeEntry() method of ZipOutputStream class is called and this caused the physical creation of the Zip file. All the other streams created are also closed.
Anu VPosted Nov 30, 2016, 4:56 AM
Nice article.. Thanks for sharing.
Priyadarshini BPosted Jul 16, 2012, 1:33 PM
Check this out which uses Dotnetzip library. It works great. http://programmingfree.blogspot.in/2012/07/create-analyze-and-manipulate-zip-files.html
Reza IsfahaniPosted Jul 18, 2010, 8:30 AM
My email : [email protected]
Reza IsfahaniPosted Jul 18, 2010, 8:29 AM
hi, please compelet project this code (with C#)
parag moondraPosted May 4, 2009, 2:58 AM
I am getting an error message at the following line: ZipFile zipfile = new ZipFile(zipFileName); My zip file in 150MB in size and contains aroung 1,50,000 files. Please let me know if u encountered this problem and also the resolution Parag [email protected]
Steve ChuaPosted Jan 8, 2009, 8:48 PM
Hi, It seems that the source files are locked after zipped. I cannot delete the source files unless I exit my application. May I know what's the reason for this and do you have any solutions? Thanks,
riyas khanPosted Nov 4, 2008, 12:57 AM
Really an excellent article. Here I'm willingly sharing my project -> WBCZipUnzip from wbcsoftwarelab. We too inclded J# runtime libraries. Just vist and review. Thank you.
satishPosted Jan 2, 2008, 4:32 AM
The name 'GetZippedFiles' does not exist in the current context Satish
AbhaPosted Aug 8, 2007, 11:51 AM
I tried the above code in 32 bit platform and it worked but it failed to unzip files in 64 bit platform. Do you have another version of the same code which works in 64 bit too?
AbhaPosted Aug 8, 2007, 11:36 AM
I tried the above code in 32 bit platform and it worked but it failed to unzip files in 64 bit platform. Do you have another version of the same code which works in 64 bit too?
f117_b2editedPosted May 17, 2007, 6:22 PMEdited May 17, 2007, 6:49 PM
Can you please post a COMPLETE & WORKABLE code with .Net 1.1 here? Thanks.