Click here to Skip to main content
11,719,903 members (90,027 online)
Click here to Skip to main content

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

, 25 Apr 2004 CPOL 208.3K 3.2K 47
Rate this:
Please Sign up or sign in to vote.
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:

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

Now, we can use it:

// 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)

Share

About the Author

Valeri Makarov
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

You may also be interested in...

Comments and Discussions

 
GeneralRe: Alternative (with code) Pin
chris1755-Dec-06 4:49
memberchris1755-Dec-06 4:49 
GeneralAble to zip directories and their contents Pin
nicholaskeytholeong26-Aug-05 19:01
membernicholaskeytholeong26-Aug-05 19:01 
GeneralRe: Able to zip directories and their contents Pin
obinna_eke30-May-06 1:29
memberobinna_eke30-May-06 1:29 
GeneralRe: Able to zip directories and their contents Pin
sanjayg1233-Mar-07 21:59
membersanjayg1233-Mar-07 21:59 
GeneralZipping folders and their subdirectories Pin
nicholaskey12-Aug-05 17:39
sussnicholaskey12-Aug-05 17:39 
GeneralRe: Zipping folders and their subdirectories Pin
nicholaskeytholeong12-Aug-05 17:42
membernicholaskeytholeong12-Aug-05 17:42 
GeneralThank You Pin
Seth O'Neal6-May-05 7:37
memberSeth O'Neal6-May-05 7:37 
GeneralThere is a bug in those classes Pin
Nishant S9-May-04 3:56
staffNishant S9-May-04 3:56 
GeneralRe: There is a bug in those classes Pin
Valeri9-May-04 4:28
memberValeri9-May-04 4:28 
GeneralRe: There is a bug in those classes Pin
Nishant S9-May-04 4:30
staffNishant S9-May-04 4:30 
GeneralRe: There is a bug in those classes Pin
cmhasan29-Nov-04 5:25
membercmhasan29-Nov-04 5:25 
GeneralThis actually has a problem. Pin
udaan20033-May-04 5:46
memberudaan20033-May-04 5:46 
GeneralRe: This actually has a problem. Pin
Valeri3-May-04 7:02
memberValeri3-May-04 7:02 
GeneralRe: This actually has a problem. Pin
udaan20034-May-04 5:58
memberudaan20034-May-04 5:58 
GeneralRe: This actually has a problem. Pin
Valeri4-May-04 7:00
memberValeri4-May-04 7:00 
GeneralThis has been covered in a fair bit of detail in another article... Pin
Duncanma25-Apr-04 22:22
memberDuncanma25-Apr-04 22:22 
GeneralRe: This has been covered in a fair bit of detail in another article... Pin
Valeri25-Apr-04 22:45
memberValeri25-Apr-04 22:45 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150901.1 | Last Updated 26 Apr 2004
Article Copyright 2004 by Valeri Makarov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid