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

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

By , 25 Apr 2004
 

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)

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionWhat about cyrillics?memberchien-andalou16-Jul-09 5:32 
GeneralHanging onto corrupt filememberterry summers22-Mar-09 10:16 
Questionunzipped progress statusmemberbyteway2-Oct-07 21:38 
QuestionUnzip file stored in databasemembermikebiro1-Oct-07 12:11 
QuestionCan I zip without folder structure?memberjupinno_ys15-Aug-07 21:57 
AnswerRe: Can I zip without folder structure?memberPranaySharma8427-Aug-07 3:55 
GeneralProblem in unzipmemberhamidreza Talebi7-Aug-07 1:18 
QuestionSize of Zipped Filememberkyreddy26-Jun-07 4:00 
Generalzip files with passwordmemberMember #112974824-Feb-07 2:21 
Hi,
 
I just check the project from Valeri EasyZipUnzip_Demo, that what I'm looking for.
Unfortunetly I wish to zip and unzip files with password, is it possible?
 
Thanks in andvance.

 
Naim
GeneralRe: zip files with passwordmemberValeri18-Apr-07 19:47 
GeneralRe: zip files with passwordmemberamit_2006_it@yahoo.com6-Jun-07 9:05 
GeneralRe: zip files with passwordmemberValeri7-Jun-07 7:13 
QuestionHow to unzip password protected zip files?membersakthivenkatesh22-Feb-07 13:04 
AnswerRe: How to unzip password protected zip files?memberValeri18-Apr-07 19:53 
GeneralGood jobmemberjoachimF6-Dec-06 23:52 
QuestionHow can we unzip password-protected files.memberAnjum Rizwi26-Nov-06 19:56 
QuestionIs it possible to read in the middle of the zip file?membershultas30-Aug-06 15:52 
AnswerRe: Is it possible to read in the middle of the zip file?memberValeri30-Aug-06 20:21 
GeneralRe: Is it possible to read in the middle of the zip file?membershultas31-Aug-06 11:19 
GeneralZip Folder qithin a zip foldermemberM Ali Raza5-Apr-06 21:02 
GeneralProblem In Latin FileNamesmemberdealarconjose15-Mar-06 1:03 
GeneralAlternative (with code)memberElmue21-Nov-05 0:41 
GeneralWhat about multiple folders?memberdragomir22-Dec-05 0:54 
GeneralRe: What about multiple folders?memberElmue31-Dec-05 1:53 
GeneralRe: What about multiple folders?memberwyattda17-Jan-06 17:36 

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

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