Click here to Skip to main content
12,631,123 members (31,224 online)
Click here to Skip to main content
Add your own
alternative version

Stats

218.9K views
3.3K downloads
47 bookmarked
Posted

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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionWhat about cyrillics? Pin
chien-andalou16-Jul-09 6:32
memberchien-andalou16-Jul-09 6:32 
GeneralHanging onto corrupt file Pin
terry summers22-Mar-09 11:16
memberterry summers22-Mar-09 11:16 
Questionunzipped progress status Pin
byteway2-Oct-07 22:38
memberbyteway2-Oct-07 22:38 
QuestionUnzip file stored in database Pin
mikebiro1-Oct-07 13:11
membermikebiro1-Oct-07 13:11 
QuestionCan I zip without folder structure? Pin
jupinno_ys15-Aug-07 22:57
memberjupinno_ys15-Aug-07 22:57 
AnswerRe: Can I zip without folder structure? Pin
PranaySharma8427-Aug-07 4:55
memberPranaySharma8427-Aug-07 4:55 
GeneralProblem in unzip Pin
hamidreza Talebi7-Aug-07 2:18
memberhamidreza Talebi7-Aug-07 2:18 
QuestionSize of Zipped File Pin
kyreddy26-Jun-07 5:00
memberkyreddy26-Jun-07 5:00 
Generalzip files with password Pin
Member #112974824-Feb-07 3:21
memberMember #112974824-Feb-07 3:21 
GeneralRe: zip files with password Pin
Valeri18-Apr-07 20:47
memberValeri18-Apr-07 20:47 
GeneralRe: zip files with password Pin
amit_2006_it@yahoo.com6-Jun-07 10:05
memberamit_2006_it@yahoo.com6-Jun-07 10:05 
GeneralRe: zip files with password Pin
Valeri7-Jun-07 8:13
memberValeri7-Jun-07 8:13 
QuestionHow to unzip password protected zip files? Pin
sakthivenkatesh22-Feb-07 14:04
membersakthivenkatesh22-Feb-07 14:04 
AnswerRe: How to unzip password protected zip files? Pin
Valeri18-Apr-07 20:53
memberValeri18-Apr-07 20:53 
GeneralGood job Pin
joachimF7-Dec-06 0:52
memberjoachimF7-Dec-06 0:52 
QuestionHow can we unzip password-protected files. Pin
Anjum Rizwi26-Nov-06 20:56
memberAnjum Rizwi26-Nov-06 20:56 
QuestionIs it possible to read in the middle of the zip file? Pin
shultas30-Aug-06 16:52
membershultas30-Aug-06 16:52 
AnswerRe: Is it possible to read in the middle of the zip file? Pin
Valeri30-Aug-06 21:21
memberValeri30-Aug-06 21:21 
GeneralRe: Is it possible to read in the middle of the zip file? Pin
shultas31-Aug-06 12:19
membershultas31-Aug-06 12:19 
GeneralZip Folder qithin a zip folder Pin
M Ali Raza5-Apr-06 22:02
memberM Ali Raza5-Apr-06 22:02 
GeneralProblem In Latin FileNames Pin
dealarconjose15-Mar-06 2:03
memberdealarconjose15-Mar-06 2:03 
GeneralAlternative (with code) Pin
Elmue21-Nov-05 1:41
memberElmue21-Nov-05 1:41 
Hello

As vjslib is buggy (don't use it) try this one:
http://www.icsharpcode.net/OpenSource/SharpZipLib/

There are 3 advantegaes over vjslib:
1.) It works without bugs.
2.) It also supports GZip, Tar and BZip2 .
3.) The DLL you need is only 140 kB instead of 3,7 MB.

The SharpZipLib has a good detailed documentation but it lacks examples.
So I post my code here which I use to zip a whole folder into a ZIP file.

Elmü

using ICSharpCode.SharpZipLib.Zip;
 
/// <summary>
/// Packs a folder and all files in all its subfolders into a ZIP file preserving the relative paths
/// Requires ICSharpCode.SharpZipLib library
/// http://www.icsharpcode.net/OpenSource/SharpZipLib
/// </summary>
static public void PackFolderIntoZipFile(string s_Folder, string s_ZipFile)
{
	ZipOutputStream i_zStream = new ZipOutputStream(File.Create(s_ZipFile));
	i_zStream.SetLevel(5); // Compression level (0-9)

	int s32_BaseFolderLen = s_Folder.Length;
	if (s_Folder.Substring(s_Folder.Length-1) != "\\") s32_BaseFolderLen ++;
 
	ArrayList i_FileList = new ArrayList();
	EnumFiles(ref i_FileList, s_Folder, "*.*", true);
 
	Byte[] u8_Buf = new Byte[0x100000]; // 1 Megabyte buffer
 
	foreach (string s_File in i_FileList)
	{
		FileStream i_fStream = null;
		// Catch errors of files which are locked (cannot be opened)
		try   { i_fStream = File.OpenRead(s_File); }
		catch { continue; }
 
		// Cut the base folder ("E:\ZipFolder\Subfolder\File.txt" -> "Subfolder/File.txt")
		// The zip archive requires "/" as separator
		string s_ZipPath = s_File.Substring(s32_BaseFolderLen).Replace("\\", "/");
 
		ZipEntry i_ZipEntr = new ZipEntry(s_ZipPath);
		FileInfo i_Info    = new FileInfo(s_File);
		i_ZipEntr.DateTime = i_Info.LastWriteTime;
		i_zStream.PutNextEntry(i_ZipEntr);
 
		while (true)
		{
			int s32_Len = i_fStream.Read(u8_Buf, 0, u8_Buf.Length);
			if (s32_Len <= 0)
				break;
			
			i_zStream.Write(u8_Buf, 0, s32_Len);
		}
 
		i_fStream.Close();
	}
 
	i_zStream.Finish();
	i_zStream.Close();
}
 
/// <summary>
/// This recursive function searches all files in the given folder and its subfolders
/// s_Filter = "*.Extension"
/// s_Path   = Start path
/// </summary>
static public void EnumFiles(ref ArrayList i_FileList, string s_Path, string s_Filter, bool b_Subfolders)
{
	if (s_Path == null || s_Path == "")
		return;
 
	string[] s_Files = Directory.GetFiles(s_Path, s_Filter.Trim());
	foreach (string s_File in s_Files)
	{
		i_FileList.Add(s_File);
	}
 
	if (b_Subfolders)
	{
		string[] s_Dirs = Directory.GetDirectories(s_Path);
		foreach (string s_Dir in s_Dirs)
		{
			EnumFiles(ref i_FileList, s_Dir, s_Filter, true);
		}
	}
}

GeneralWhat about multiple folders? Pin
dragomir22-Dec-05 1:54
memberdragomir22-Dec-05 1:54 
GeneralRe: What about multiple folders? Pin
Elmue31-Dec-05 2:53
memberElmue31-Dec-05 2:53 
GeneralRe: What about multiple folders? PinPopular
wyattda17-Jan-06 18:36
memberwyattda17-Jan-06 18:36 
GeneralRe: Alternative (with code) Pin
chris1755-Dec-06 5:49
memberchris1755-Dec-06 5:49 
GeneralAble to zip directories and their contents Pin
nicholaskeytholeong26-Aug-05 20:01
membernicholaskeytholeong26-Aug-05 20:01 
GeneralRe: Able to zip directories and their contents Pin
obinna_eke30-May-06 2:29
memberobinna_eke30-May-06 2:29 
GeneralRe: Able to zip directories and their contents Pin
sanjayg1233-Mar-07 22:59
membersanjayg1233-Mar-07 22:59 
GeneralZipping folders and their subdirectories Pin
nicholaskey12-Aug-05 18:39
sussnicholaskey12-Aug-05 18:39 
GeneralRe: Zipping folders and their subdirectories Pin
nicholaskeytholeong12-Aug-05 18:42
membernicholaskeytholeong12-Aug-05 18:42 
GeneralThank You Pin
Seth O'Neal6-May-05 8:37
memberSeth O'Neal6-May-05 8:37 
GeneralThere is a bug in those classes Pin
Nishant S9-May-04 4:56
staffNishant S9-May-04 4:56 
GeneralRe: There is a bug in those classes Pin
Valeri9-May-04 5:28
memberValeri9-May-04 5:28 
GeneralRe: There is a bug in those classes Pin
Nishant S9-May-04 5:30
staffNishant S9-May-04 5:30 
GeneralRe: There is a bug in those classes Pin
cmhasan29-Nov-04 6:25
membercmhasan29-Nov-04 6:25 
GeneralThis actually has a problem. Pin
udaan20033-May-04 6:46
memberudaan20033-May-04 6:46 
GeneralRe: This actually has a problem. Pin
Valeri3-May-04 8:02
memberValeri3-May-04 8:02 
GeneralRe: This actually has a problem. Pin
udaan20034-May-04 6:58
memberudaan20034-May-04 6:58 
GeneralRe: This actually has a problem. Pin
Valeri4-May-04 8:00
memberValeri4-May-04 8:00 
GeneralThis has been covered in a fair bit of detail in another article... Pin
Duncanma25-Apr-04 23:22
memberDuncanma25-Apr-04 23:22 
GeneralRe: This has been covered in a fair bit of detail in another article... Pin
Valeri25-Apr-04 23:45
memberValeri25-Apr-04 23:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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.161205.3 | Last Updated 26 Apr 2004
Article Copyright 2004 by Valeri Makarov
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid