Click here to Skip to main content
15,881,852 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.6K   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

 
GeneralRe: zip files with password Pin
amit_2006_it@yahoo.com6-Jun-07 9:05
amit_2006_it@yahoo.com6-Jun-07 9:05 
GeneralRe: zip files with password Pin
Valeri Makarov7-Jun-07 7:13
Valeri Makarov7-Jun-07 7:13 
QuestionHow to unzip password protected zip files? Pin
sakthivenkatesh22-Feb-07 13:04
sakthivenkatesh22-Feb-07 13:04 
AnswerRe: How to unzip password protected zip files? Pin
Valeri Makarov18-Apr-07 19:53
Valeri Makarov18-Apr-07 19:53 
GeneralGood job Pin
joachimj6-Dec-06 23:52
joachimj6-Dec-06 23:52 
QuestionHow can we unzip password-protected files. Pin
Anjum Rizwi26-Nov-06 19:56
Anjum Rizwi26-Nov-06 19:56 
QuestionIs it possible to read in the middle of the zip file? Pin
shultas30-Aug-06 15:52
shultas30-Aug-06 15:52 
AnswerRe: Is it possible to read in the middle of the zip file? Pin
Valeri Makarov30-Aug-06 20:21
Valeri Makarov30-Aug-06 20:21 
GeneralRe: Is it possible to read in the middle of the zip file? Pin
shultas31-Aug-06 11:19
shultas31-Aug-06 11:19 
GeneralZip Folder qithin a zip folder Pin
M Ali Raza5-Apr-06 21:02
M Ali Raza5-Apr-06 21:02 
GeneralProblem In Latin FileNames Pin
dealarconjose15-Mar-06 1:03
dealarconjose15-Mar-06 1:03 
GeneralAlternative (with code) Pin
Elmue21-Nov-05 0:41
Elmue21-Nov-05 0:41 
GeneralWhat about multiple folders? Pin
dragomir22-Dec-05 0:54
dragomir22-Dec-05 0:54 
GeneralRe: What about multiple folders? Pin
Elmue31-Dec-05 1:53
Elmue31-Dec-05 1:53 
GeneralRe: What about multiple folders? Pin
wyattda17-Jan-06 17:36
wyattda17-Jan-06 17:36 
GeneralRe: Alternative (with code) Pin
Christopher Stratmann5-Dec-06 4:49
Christopher Stratmann5-Dec-06 4:49 
I attempted to use the SharpZipLib with the .NET 2.0 Framework and Visual Studio 2005 C# windows project and the result I got were very poor. I was able to compile and build zip files that were always currupted. I used their sample project called “SharpZipLib\samples\cs\CreateZipFile” and compilied it with the dll obtained from the .NET 2.0 download. I then proceeded to use the library with a bunch of .cs, .snk, .shfb, and .xml files and the resulting ZIP file was always currupted. Doing the same thing with the java library seems to be working just great.

Chris
GeneralAble to zip directories and their contents Pin
nicholaskeytholeong26-Aug-05 19:01
nicholaskeytholeong26-Aug-05 19:01 
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 
GeneralZipping folders and their subdirectories Pin
nicholaskeytholeong12-Aug-05 17:39
nicholaskeytholeong12-Aug-05 17:39 
GeneralRe: Zipping folders and their subdirectories Pin
nicholaskeytholeong12-Aug-05 17:42
nicholaskeytholeong12-Aug-05 17:42 
GeneralThank You Pin
Seth O'Neal6-May-05 7:37
Seth O'Neal6-May-05 7:37 
GeneralThere is a bug in those classes Pin
Nish Nishant9-May-04 3:56
sitebuilderNish Nishant9-May-04 3:56 
GeneralRe: There is a bug in those classes Pin
Valeri Makarov9-May-04 4:28
Valeri Makarov9-May-04 4:28 
GeneralRe: There is a bug in those classes Pin
Nish Nishant9-May-04 4:30
sitebuilderNish Nishant9-May-04 4:30 

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.