Click here to Skip to main content
Click here to Skip to main content

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

, 25 Apr 2004 CPOL
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

Comments and Discussions

 
QuestionWhat about cyrillics? Pinmemberchien-andalou16-Jul-09 6:32 
GeneralHanging onto corrupt file Pinmemberterry summers22-Mar-09 11:16 
Questionunzipped progress status Pinmemberbyteway2-Oct-07 22:38 
Hi,
 
Super article! Read it and now using it. How can I show unzip status in a progress bar? I can get the size of the file being unzipped together with the size of buf (what's actually unzipped to the outputstream) I thought I could show some kind of status. But the problem is that I get the size of the file within the zipfile, this means that: when I set the maximum value to the file size and the status value to the buffer size, I get an error because the unzipped buf size is bigger... Please tell me how to deal with this...
QuestionUnzip file stored in database Pinmembermikebiro1-Oct-07 13:11 
QuestionCan I zip without folder structure? Pinmemberjupinno_ys15-Aug-07 22:57 
AnswerRe: Can I zip without folder structure? PinmemberPranaySharma8427-Aug-07 4:55 
GeneralProblem in unzip Pinmemberhamidreza Talebi7-Aug-07 2:18 
QuestionSize of Zipped File Pinmemberkyreddy26-Jun-07 5:00 
Generalzip files with password PinmemberMember #112974824-Feb-07 3:21 
GeneralRe: zip files with password PinmemberValeri18-Apr-07 20:47 
GeneralRe: zip files with password Pinmemberamit_2006_it@yahoo.com6-Jun-07 10:05 
GeneralRe: zip files with password PinmemberValeri7-Jun-07 8:13 
QuestionHow to unzip password protected zip files? Pinmembersakthivenkatesh22-Feb-07 14:04 
AnswerRe: How to unzip password protected zip files? PinmemberValeri18-Apr-07 20:53 
GeneralGood job PinmemberjoachimF7-Dec-06 0:52 
QuestionHow can we unzip password-protected files. PinmemberAnjum Rizwi26-Nov-06 20:56 
QuestionIs it possible to read in the middle of the zip file? Pinmembershultas30-Aug-06 16:52 
AnswerRe: Is it possible to read in the middle of the zip file? PinmemberValeri30-Aug-06 21:21 
GeneralRe: Is it possible to read in the middle of the zip file? Pinmembershultas31-Aug-06 12:19 
GeneralZip Folder qithin a zip folder PinmemberM Ali Raza5-Apr-06 22:02 
GeneralProblem In Latin FileNames Pinmemberdealarconjose15-Mar-06 2:03 
GeneralAlternative (with code) PinmemberElmue21-Nov-05 1:41 
GeneralWhat about multiple folders? Pinmemberdragomir22-Dec-05 1:54 
GeneralRe: What about multiple folders? PinmemberElmue31-Dec-05 2:53 
GeneralRe: What about multiple folders? Pinmemberwyattda17-Jan-06 18:36 

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
Web01 | 2.8.150123.1 | Last Updated 26 Apr 2004
Article Copyright 2004 by Valeri Makarov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid