Click here to Skip to main content
11,642,216 members (62,255 online)
Click here to Skip to main content

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

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

 
QuestionWhat about cyrillics? Pin
chien-andalou16-Jul-09 5:32
memberchien-andalou16-Jul-09 5:32 
GeneralHanging onto corrupt file Pin
terry summers22-Mar-09 10:16
memberterry summers22-Mar-09 10:16 
Questionunzipped progress status Pin
byteway2-Oct-07 21:38
memberbyteway2-Oct-07 21:38 
QuestionUnzip file stored in database Pin
mikebiro1-Oct-07 12:11
membermikebiro1-Oct-07 12:11 
QuestionCan I zip without folder structure? Pin
jupinno_ys15-Aug-07 21:57
memberjupinno_ys15-Aug-07 21:57 
AnswerRe: Can I zip without folder structure? Pin
PranaySharma8427-Aug-07 3:55
memberPranaySharma8427-Aug-07 3:55 
GeneralProblem in unzip Pin
hamidreza Talebi7-Aug-07 1:18
memberhamidreza Talebi7-Aug-07 1:18 
QuestionSize of Zipped File Pin
kyreddy26-Jun-07 4:00
memberkyreddy26-Jun-07 4:00 
Generalzip files with password Pin
Member #112974824-Feb-07 2:21
memberMember #112974824-Feb-07 2:21 
GeneralRe: zip files with password Pin
Valeri18-Apr-07 19:47
memberValeri18-Apr-07 19:47 
GeneralRe: zip files with password Pin
amit_2006_it@yahoo.com6-Jun-07 9:05
memberamit_2006_it@yahoo.com6-Jun-07 9:05 
GeneralRe: zip files with password Pin
Valeri7-Jun-07 7:13
memberValeri7-Jun-07 7:13 
QuestionHow to unzip password protected zip files? Pin
sakthivenkatesh22-Feb-07 13:04
membersakthivenkatesh22-Feb-07 13:04 
AnswerRe: How to unzip password protected zip files? Pin
Valeri18-Apr-07 19:53
memberValeri18-Apr-07 19:53 
GeneralGood job Pin
joachimF6-Dec-06 23:52
memberjoachimF6-Dec-06 23:52 
QuestionHow can we unzip password-protected files. Pin
Anjum Rizwi26-Nov-06 19:56
memberAnjum Rizwi26-Nov-06 19:56 
QuestionIs it possible to read in the middle of the zip file? Pin
shultas30-Aug-06 15:52
membershultas30-Aug-06 15:52 
We have thousands and thousands of zip files on our server. I have written a program that opens up these zip files and extracts certain files from them (i.e, *.doc files). I have been doing this over VPN, and it is much slower than going into the office. Most of the time I only need one .DOC file that is in a 5 or 10MB zip, and it happens to be the last file stored in most of the zips. What is happening is that the program is reading the zip file until it finds a file it wants and then extracts only that file. The problem is since these files are stored at the end of the .ZIP, it takes many minutes for it to iterate through all of the files that are in the ZIP (even though I don't want to do anything with them, or try to read them, etc...) and it just takes a while.

I am wondering if ZIP stores the file contents in a header, and if it does, if it is possible to just read that header, figure out what files you need and what byte offset it is at and then "skip" right to that file without reading the entire zip file? This would make my helper application a million times faster. I've looked around for other solutions but I can't seem to see any that can do it, it appears as if this Java class needs to read the contents of the entire file to get you what you want.

Thanks!
AnswerRe: Is it possible to read in the middle of the zip file? Pin
Valeri30-Aug-06 20:21
memberValeri30-Aug-06 20:21 
GeneralRe: Is it possible to read in the middle of the zip file? Pin
shultas31-Aug-06 11:19
membershultas31-Aug-06 11:19 
GeneralZip Folder qithin a zip folder Pin
M Ali Raza5-Apr-06 21:02
memberM Ali Raza5-Apr-06 21:02 
GeneralProblem In Latin FileNames Pin
dealarconjose15-Mar-06 1:03
memberdealarconjose15-Mar-06 1:03 
GeneralAlternative (with code) Pin
Elmue21-Nov-05 0:41
memberElmue21-Nov-05 0:41 
GeneralWhat about multiple folders? Pin
dragomir22-Dec-05 0:54
memberdragomir22-Dec-05 0:54 
GeneralRe: What about multiple folders? Pin
Elmue31-Dec-05 1:53
memberElmue31-Dec-05 1:53 
GeneralRe: What about multiple folders? Pin
wyattda17-Jan-06 17:36
memberwyattda17-Jan-06 17: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
Web02 | 2.8.150731.1 | Last Updated 26 Apr 2004
Article Copyright 2004 by Valeri Makarov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid