Click here to Skip to main content
15,886,026 members
Articles / Programming Languages / C#
Article

Zip and Unzip from a C# program using J# runtime

Rate me:
Please Sign up or sign in to vote.
4.54/5 (38 votes)
25 Apr 2004CPOL 272.9K   3.6K   47   42
Zip and Unzip from a C# program using java.util.zip

Introduction

There are a lot of libraries to work with zip files. I found a very easy way to work with zip files from a C# program. The Microsoft .Net Framework 1.1 comes with new language: J#. Let's look at it a little. For sure, Java is a parent of it; if so, java.util.zip is somewhere here! I spent a few minutes and found it in vjslib.dll. Let's do the work. Start a new C# project and select Windows Application. Our goal is something like that:

Image 1

All we need to do is: add reference to our project:

Image 2

Now, we can use it:

C#
// Output stream 
java.io.FileOutputStream fos = new java.io.FileOutputStream(zipFileName); 

// Tie to zip stream 
java.util.zip.ZipOutputStream zos = new java.util.zip.ZipOutputStream(fos); 

// Stream with source file 
java.io.FileInputStream fis = new java.io.FileInputStream(sourceFile); 

// It's our entry in zip 
java.util.zip.ZipEntry ze = new java.util.zip.ZipEntry(sourceFile); 

zos.putNextEntry(ze); 
sbyte[] buffer = new sbyte[1024]; 
int len; 

// Read and write until done 
while((len = fis.read(buffer)) >= 0) 
{ 
    zos.write(buffer, 0, len); 
}
 
// Close everything 
zos.closeEntry(); 
fis.close(); 
zos.close(); 
fos.close(); 

Conclusion

That's all! Demo application is included.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Russian Federation Russian Federation
Over 12 years of experience as a Software Developer/Engineer, development and production support applications for MS DOS, MS Windows.
Certified C Programmer
Certified C++ Programmer
Certified Visual FoxPro Programmer
Certified C# Programmer

Comments and Discussions

 
GeneralAble to zip directories and their contents Pin
nicholaskeytholeong26-Aug-05 19:01
nicholaskeytholeong26-Aug-05 19:01 
Hi Valeri, I have finally came out with the algorithm to zip a particular folder and it's content which includes the subfolders and the files that are located in both the folder and the subfolders.

I have been searching for an example to zip a folder without using the opensource library (SharpZipLib) but I can't find any. Then, I decided to write the codes myself in the end. It took me a few lines to do both zip and unzip folder operation as well as zipping and unzipping files. wooohoooo yay.

I shall share my codes to the codeproject.com viewers soon. Big Grin | :-D


GeneralRe: Able to zip directories and their contents Pin
obinna_eke30-May-06 1:29
obinna_eke30-May-06 1:29 
GeneralRe: Able to zip directories and their contents Pin
sanjayg1233-Mar-07 21:59
sanjayg1233-Mar-07 21:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.