5,550,131 members and growing! (18,865 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate License: The Code Project Open License (CPOL)

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

By Valeri Makarov

Zip and Unzip from a C# program using java.util.zip
C#, Windows, .NET 1.1, .NETVisual Studio, VS.NET2003, Dev

Posted: 25 Apr 2004
Updated: 25 Apr 2004
Views: 108,330
Bookmarked: 31 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
34 votes for this Article.
Popularity: 6.64 Rating: 4.33 out of 5
2 votes, 5.9%
1
1 vote, 2.9%
2
1 vote, 2.9%
3
9 votes, 26.5%
4
21 votes, 61.8%
5

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


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
Occupation: Software Developer
Location: Russian Federation Russian Federation

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 40 (Total in Forum: 40) (Refresh)FirstPrevNext
Subject  Author Date 
Questionunzipped progress statusmemberbyteway22:38 2 Oct '07  
QuestionUnzip file stored in databasemembermikebiro13:11 1 Oct '07  
GeneralCan I zip without folder structure?memberjupinno_ys22:57 15 Aug '07  
AnswerRe: Can I zip without folder structure?memberPranaySharma844:55 27 Aug '07  
GeneralProblem in unzipmemberhamidreza Talebi2:18 7 Aug '07  
QuestionSize of Zipped Filememberkyreddy5:00 26 Jun '07  
Generalzip files with passwordmember3:21 24 Feb '07  
GeneralRe: zip files with passwordmemberValeri20:47 18 Apr '07  
GeneralRe: zip files with passwordmemberamit_2006_it@yahoo.com10:05 6 Jun '07  
GeneralRe: zip files with passwordmemberValeri8:13 7 Jun '07  
GeneralHow to unzip password protected zip files?membersakthivenkatesh14:04 22 Feb '07  
GeneralRe: How to unzip password protected zip files?memberValeri20:53 18 Apr '07  
GeneralGood jobmemberjoachimF0:52 7 Dec '06  
GeneralHow can we unzip password-protected files.memberAnjum Rizwi20:56 26 Nov '06  
GeneralIs it possible to read in the middle of the zip file?membershultas16:52 30 Aug '06  
AnswerRe: Is it possible to read in the middle of the zip file?memberValeri21:21 30 Aug '06  
GeneralRe: Is it possible to read in the middle of the zip file?membershultas12:19 31 Aug '06  
GeneralZip Folder qithin a zip foldermemberM Ali Raza22:02 5 Apr '06  
GeneralProblem In Latin FileNamesmemberdealarconjose2:03 15 Mar '06  
GeneralAlternative (with code)memberElmue1:41 21 Nov '05  
GeneralWhat about multiple folders?memberdragomir1:54 22 Dec '05  
GeneralRe: What about multiple folders?memberElmue2:53 31 Dec '05  
GeneralRe: What about multiple folders?memberwyattda18:36 17 Jan '06  
GeneralRe: Alternative (with code)memberchris1755:49 5 Dec '06  
GeneralAble to zip directories and their contentsmembernicholaskeytholeong20:01 26 Aug '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Apr 2004
Editor: Nishant Sivakumar
Copyright 2004 by Valeri Makarov
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project