Compress and Extract a Zip File
In this article, I will explain how to Compress a Folder into zip file and extract a Zip File into folder using third party dlls such as zip.exe and ICSharpCode.SharpZipLib.dll. zip.exe is used to compress a folder. ICSharpCode.SharpZipLib.dll is used to extract a zip file
Zip a Folder and Unzip a Zip File
I will explain how to Compress folder to a zip file and extract a zip file using C#. Here I used to used two dll files zip.exe and ICSharpCode.SharpZipLib.dll. Here zip.exe is used to compress a folder. ICSharpCode.SharpZipLib.dll is used to extract a zip file. It will open and read entry by entry of a zip file and paste in a folder. Compress a Folder:
First step is add zip.exe file in your bin directory. zipafolder method contain the code to compress a folder to zip file. Pass following parameters sourcefile and destinationfile into the zipafolder method. Extract a Zip File:
Add reference to ICSharpCode.SharpZipLib.dll. Add namespace using ICSharpCode.SharpZipLib.Zip;. UnZipFiles method contain the code to unzip a zip file. Pass following parameters zippathandfile,outputfolder,password,deletezipfile into the UnZipFiles method.
Full source code as follows
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Pass following parameters sourcefile and destinationfile inside zipafolder method.
//Note Sample1 folder should be present inside C drive
zipafolder(@"C:\Sample",@"C:\Sample.zip");
//Pass following parameters zippathandfile,outputfolder,password,deletezipfile into the UnZipFiles method.
//Note Sample1.zip file should be present inside C drive
UnZipFiles(@"C:\Sample1.zip", @"C:\", null, false);
}
static long GetDirectorySize(string p)
{
// 1
// Get array of all file names.
string[] a = Directory.GetFiles(p, "*.*");
// 2
// Calculate total bytes of all files in a loop.
long b = 0;
foreach (string name in a)
{
// 3
// Use FileInfo to get length of each file.
FileInfo info = new FileInfo(name);
b += info.Length;
}
// 4
// Return total size
return b;
}
public static void zipafolder(string sourcefile, string destfile)
{
long directorySize = GetDirectorySize(sourcefile);
if (File.Exists(destfile) == false)
{
System.IO.FileStream fs = System.IO.File.Create(destfile);
fs.Close();
}
//zip.exe file should be present in Base Directory
string zipfile = System.AppDomain.CurrentDomain.BaseDirectory + "zip.exe";
//Launch the zip.exe console app to do the actual zipping
//System.Diagnostics.ProcessStartInfo i = new System.Diagnostics.ProcessStartInfo(@"C:\Program Files\EPUB-COMPILER\EPUB-COMPILERSETUP\zip.exe");
System.Diagnostics.ProcessStartInfo i = new System.Diagnostics.ProcessStartInfo(zipfile);
i.CreateNoWindow = true;
string args = "";
if (sourcefile.IndexOf(" ") != -1)
{
//we got a space in the path so wrap it in double qoutes
args += "\"" + sourcefile + "\"";
}
else
{
args += sourcefile;
}
string dest = destfile;
if (dest.IndexOf(" ") != -1)
{
//we got a space in the path so wrap it in double qoutes
args += " " + "\"" + dest + "\"";
}
else
{
args += " " + dest;
}
i.Arguments = args;
//Mark the process window as hidden so
//that the progress copy window doesn't show
i.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process p = System.Diagnostics.Process.Start(i);
int size = Convert.ToInt32(Convert.ToInt32(directorySize));
System.Threading.Thread.Sleep(40000);
//p.Kill();
}
public static void UnZipFiles(string zipPathAndFile, string outputFolder, string password, bool deleteZipFile)
{
ZipInputStream s = new ZipInputStream(File.OpenRead(zipPathAndFile));
if (password != null && password != String.Empty)
s.Password = password;
ZipEntry theEntry;
string tmpEntry = String.Empty;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = outputFolder;
string fileName = Path.GetFileName(theEntry.Name);
// create directory
if (directoryName != "")
{
Directory.CreateDirectory(directoryName);
}
if (fileName != String.Empty)
{
if (theEntry.Name.IndexOf(".ini") < 0)
{
try
{
string fullPath = directoryName + "\\" + theEntry.Name;
fullPath = fullPath.Replace("\\ ", "\\");
string fullDirPath = Path.GetDirectoryName(fullPath);
if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
FileStream streamWriter = File.Create(fullPath);
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
streamWriter.Close();
}
catch
{
}
}
}
}
s.Close();
//deleteZipFile will be False which is the parameter passing inside UnZipFiles method
if (deleteZipFile)
File.Delete(zipPathAndFile);
}
}
}
I had attached sourecode of the entire project and also the ICSharpCode.SharpZiplib file for your reference