Hi.,
How to zip and unzip files using C#
Thanks
Akila
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Mayur GujrathiPosted Nov 4, 2011, 3:14 AM
and do as following
using System;
using System.Collections;
using EACompression;
void Unzip( )
{
try
{
ZipArchive oZip = new ZipArchive( "TryIt" );
oZip.Load( "c:\\test.zip" );
ZipFile [] zs = oZip.Files;
int count = zs.Length;
string password = "";
bool overwrite = true;
for( int i = 0; i < count; i++ )
{
ZipFile z = zs[i];
oZip.ExtractTo( z, password, "c:\\unzipped", overwrite );
}
//another way is:
//oZip.ExtractAll( "c:\\uuzipped", password );
//if you want to remove one of the file from this zip archive.
//oZip.Remove( zs[0] );
}
catch( Exception ep )
{
Console.Write( ep.Message );
}
}
void Zip()
{
try
{
ZipArchive oZip = new ZipArchive( "TryIt" );
oZip.Create( "c:\\test.zip", true ); //create a new zip file;
string password = "";
//add a single file to zip file.
oZip.AddFile( "c:\\test.gif", "test.gif", password );
//add all files and sub-directory in temp folder to zip archive
oZip.AddDirectory( "c:\\temp", password );
}
catch( Exception ep )
{
Console.Write( ep.Message );
}
}
Datta KharadPosted Nov 4, 2011, 2:26 AM
private void btnZip_Click(object sender, EventArgs e)
{
if (files != null)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
txtZip.Text = saveFileDialog1.FileName;
Zip(txtZip.Text.Trim(), files);
}
}
}
private List
{
List
Enumeration zipEnum = zipfil.entries();
while (zipEnum.hasMoreElements())
{
ZipEntry zip = (ZipEntry)zipEnum.nextElement();
lstZip.Add(zip);
}
return lstZip;
}
private void Zip(string zipFileName, string[] sourceFile)
{
FileOutputStream filOpStrm = new FileOutputStream(zipFileName);
ZipOutputStream zipOpStrm = new ZipOutputStream(filOpStrm);
FileInputStream filIpStrm = null;
foreach (string strFilName in sourceFile)
{
filIpStrm = new FileInputStream(strFilName);
ZipEntry ze = new ZipEntry(Path.GetFileName(strFilName));
zipOpStrm.putNextEntry(ze);
sbyte[] buffer = new sbyte[1024];
int len = 0;
while ((len = filIpStrm.read(buffer)) >= 0)
{
zipOpStrm.write(buffer, 0, len);
}
}
zipOpStrm.closeEntry();
filIpStrm.close();
zipOpStrm.close();
filOpStrm.close();
}
private void Extract(string zipFileName, string destinationPath)
{
FileInfo fInf = new FileInfo(zipFileName);
progressBar1.Visible = true;
progressBar2.Visible = true;
progressBar1.Minimum = 0;
progressBar1.Maximum = Convert.ToInt32((fInf.Length)/100);
lstExtractedFiles.Items.Clear();
ZipFile zipfile = new ZipFile(zipFileName);
List
progressBar2.Minimum = 0;
progressBar2.Maximum = zipFiles.Count;
lstExtractedFiles.Items.Add("Files Extracted to: " + destinationPath);
lstExtractedFiles.Items.Add("------------------------------------------------------------------------------------------------------------------------");
foreach (ZipEntry zipFile in zipFiles)
{
if (!zipFile.isDirectory())
{
InputStream s = zipfile.getInputStream(zipFile);
try
{
Directory.CreateDirectory(destinationPath + "\\" + Path.GetDirectoryName(zipFile.getName()));
FileOutputStream dest = new FileOutputStream(Path.Combine(destinationPath + "\\" + Path.GetDirectoryName(zipFile.getName()), Path.GetFileName(zipFile.getName())));
try
{
int len = 0;
sbyte[] buffer = new sbyte[8000];
while ((len = s.read(buffer)) >= 0)
{
dest.write(buffer, 0, len);
if (progressBar1.Value + (8000 / 1000) < progressBar1.Maximum)
{
progressBar1.Value = (progressBar1.Value + (8000 / 1000));
}
else
{
string d = "";
}
progressBar1.Refresh();
}
}
finally
{
dest.close();
}
}
finally
{
s.close();
}
}
lstExtractedFiles.Items.Add(zipFile.getName());
lstExtractedFiles.Refresh();
progressBar1.Value = 0;
progressBar2.Value = progressBar2.Value + 1;
progressBar2.Refresh();
}
progressBar2.Value = 0;
progressBar1.Visible = false;
progressBar2.Visible = false;
}
private void btnBrowse_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
files = openFileDialog1.FileNames;
lstBoxFilesToZip.DataSource = files;
}
}
private void btnExtract_Click(object sender, EventArgs e)
{
if(txtExtract.Text.Trim().Length > 0)
{
if(folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
Extract(txtExtract.Text.Trim(), folderBrowserDialog1.SelectedPath);
}
}
}
private void btnExtractBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.Multiselect = false;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtExtract.Text = openFileDialog1.FileName;
lstExtractedFiles.Items.Clear();
}
openFileDialog1.Multiselect = true;
}
If you got correct answer then mark as Accepted Answer.