在C#应用程序中自动解压zip文件
在C#应用程序中自动解压zip文件
//解压
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;
private void butImg_Click(object sender, System.EventArgs e)
{
if (folderBrowserDialog1.ShowDialog()==DialogResult.OK)
{
try
{
txtb1.Text=folderBrowserDialog1.SelectedPath;
//解压
UnZip(txtb1.Text+"\\SBDAT.ZIP");
this.butt2.Enabled=true;
}
catch(Exception ee)
{
this.butt2.Enabled=false;
this.Alert("请检查路径是否正确!浏览解压路径出错,原因:"+ee.Message );
}
}
}
/// <summary>
/// 解压程序
/// </summary>
/// <param name="strFile"></param>
public void UnZip(string strFile)
{
ZipInputStream s = new ZipInputStream(File.OpenRead(strFile));
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
//this.txt1.Text=this.txt1.Text+"\r\n"+theEntry.Name;
//string directoryName = Path.GetDirectoryName(theEntry.Name);
//string directoryName = Path.GetDirectoryName(strFile)+"\\"+theEntry.Name;
string fileName = Path.GetFileName(theEntry.Name);
// create directory
//Directory.CreateDirectory(directoryName);
if (fileName != String.Empty)
{
FileStream streamWriter = File.Create(Path.GetDirectoryName(strFile)+"\\"+theEntry.Name);
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();
}
}
s.Close();
}