Click here to Skip to main content
Licence CPOL
First Posted 25 Apr 2004
Views 178,901
Downloads 2,389
Bookmarked 46 times

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

By | 25 Apr 2004 | Article
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)

About the Author

Valeri Makarov

Software Developer

Russian Federation Russian Federation

Member

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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWhat about cyrillics? Pinmemberchien-andalou5:32 16 Jul '09  
GeneralHanging onto corrupt file Pinmemberterry summers10:16 22 Mar '09  
Questionunzipped progress status Pinmemberbyteway21:38 2 Oct '07  
QuestionUnzip file stored in database Pinmembermikebiro12:11 1 Oct '07  
QuestionCan I zip without folder structure? Pinmemberjupinno_ys21:57 15 Aug '07  
AnswerRe: Can I zip without folder structure? PinmemberPranaySharma843:55 27 Aug '07  
GeneralProblem in unzip Pinmemberhamidreza Talebi1:18 7 Aug '07  
QuestionSize of Zipped File Pinmemberkyreddy4:00 26 Jun '07  
Generalzip files with password PinmemberMember #11297482:21 24 Feb '07  
GeneralRe: zip files with password PinmemberValeri19:47 18 Apr '07  
GeneralRe: zip files with password Pinmemberamit_2006_it@yahoo.com9:05 6 Jun '07  
GeneralRe: zip files with password PinmemberValeri7:13 7 Jun '07  
QuestionHow to unzip password protected zip files? Pinmembersakthivenkatesh13:04 22 Feb '07  
AnswerRe: How to unzip password protected zip files? PinmemberValeri19:53 18 Apr '07  
GeneralGood job PinmemberjoachimF23:52 6 Dec '06  
QuestionHow can we unzip password-protected files. PinmemberAnjum Rizwi19:56 26 Nov '06  
QuestionIs it possible to read in the middle of the zip file? Pinmembershultas15:52 30 Aug '06  
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? PinmemberValeri20:21 30 Aug '06  
GeneralRe: Is it possible to read in the middle of the zip file? Pinmembershultas11:19 31 Aug '06  
GeneralZip Folder qithin a zip folder PinmemberM Ali Raza21:02 5 Apr '06  
GeneralProblem In Latin FileNames Pinmemberdealarconjose1:03 15 Mar '06  
GeneralAlternative (with code) PinmemberElmue0:41 21 Nov '05  
GeneralWhat about multiple folders? Pinmemberdragomir0:54 22 Dec '05  
GeneralRe: What about multiple folders? PinmemberElmue1:53 31 Dec '05  
GeneralRe: What about multiple folders? PinPopularmemberwyattda17:36 17 Jan '06  

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.

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