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

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


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, 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 fos = new; 

// Tie to zip stream zos = new; 

// Stream with source file fis = new; 

// It's our entry in zip ze = new; 

sbyte[] buffer = new sbyte[1024]; 
int len; 

// Read and write until done 
while((len = >= 0) 
    zos.write(buffer, 0, len); 
// Close everything 


That's all! Demo application is included.


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

Comments and Discussions

QuestionWhat about cyrillics? Pinmemberchien-andalou16-Jul-09 5:32 
GeneralHanging onto corrupt file Pinmemberterry summers22-Mar-09 10:16 
Questionunzipped progress status Pinmemberbyteway2-Oct-07 21:38 
QuestionUnzip file stored in database Pinmembermikebiro1-Oct-07 12:11 
QuestionCan I zip without folder structure? Pinmemberjupinno_ys15-Aug-07 21:57 
AnswerRe: Can I zip without folder structure? PinmemberPranaySharma8427-Aug-07 3:55 
GeneralProblem in unzip Pinmemberhamidreza Talebi7-Aug-07 1:18 
QuestionSize of Zipped File Pinmemberkyreddy26-Jun-07 4:00 
Generalzip files with password PinmemberMember #112974824-Feb-07 2:21 
GeneralRe: zip files with password PinmemberValeri18-Apr-07 19:47 
GeneralRe: zip files with password Pinmemberamit_2006_it@yahoo.com6-Jun-07 9:05 
GeneralRe: zip files with password PinmemberValeri7-Jun-07 7:13 
QuestionHow to unzip password protected zip files? Pinmembersakthivenkatesh22-Feb-07 13:04 
AnswerRe: How to unzip password protected zip files? PinmemberValeri18-Apr-07 19:53 
GeneralGood job PinmemberjoachimF6-Dec-06 23:52 
QuestionHow can we unzip password-protected files. PinmemberAnjum Rizwi26-Nov-06 19:56 
QuestionIs it possible to read in the middle of the zip file? Pinmembershultas30-Aug-06 15:52 
AnswerRe: Is it possible to read in the middle of the zip file? PinmemberValeri30-Aug-06 20:21 
GeneralRe: Is it possible to read in the middle of the zip file? Pinmembershultas31-Aug-06 11:19 
GeneralZip Folder qithin a zip folder PinmemberM Ali Raza5-Apr-06 21:02 
GeneralProblem In Latin FileNames Pinmemberdealarconjose15-Mar-06 1:03 
GeneralAlternative (with code) PinmemberElmue21-Nov-05 0:41 
As vjslib is buggy (don't use it) try this one:
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.
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
/// </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;
		while (true)
			int s32_Len = i_fStream.Read(u8_Buf, 0, u8_Buf.Length);
			if (s32_Len <= 0)
			i_zStream.Write(u8_Buf, 0, s32_Len);
/// <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 == "")
	string[] s_Files = Directory.GetFiles(s_Path, s_Filter.Trim());
	foreach (string s_File in s_Files)
	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? Pinmemberdragomir22-Dec-05 0:54 
GeneralRe: What about multiple folders? PinmemberElmue31-Dec-05 1:53 
GeneralRe: What about multiple folders? Pinmemberwyattda17-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 | Mobile
Web01 | 2.8.141022.2 | Last Updated 26 Apr 2004
Article Copyright 2004 by Valeri Makarov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid