using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
|
namespace TProduct.Common
|
{
|
public class ZipFileHelper
|
{
|
/// <summary>
|
/// 解压ZIP文件
|
/// </summary>
|
/// <param name="strZipPath">待解压的ZIP文件</param>
|
/// <param name="strUnZipPath">解压的目录</param>
|
/// <param name="overWrite">是否覆盖</param>
|
/// <returns>成功:true/失败:false</returns>
|
public static bool Decompression(string strZipPath, string strUnZipPath, bool overWrite)
|
{
|
try
|
{
|
//ReadOptions options = new ReadOptions();
|
//options.Encoding = Encoding.Default;//设置编码,解决解压文件时中文乱码
|
//var enCode = Encoding.GetEncoding("GB2312");
|
//if (enCode == null)
|
// return null;
|
using (Ionic.Zip.ZipFile zip = Ionic.Zip.ZipFile.Read(strZipPath, Encoding.Default))
|
{
|
foreach (Ionic.Zip.ZipEntry entry in zip)
|
{
|
if (string.IsNullOrEmpty(strUnZipPath))
|
{
|
strUnZipPath = strZipPath.Split('.').First();
|
}
|
if (overWrite)
|
{
|
entry.Extract(strUnZipPath, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently);//解压文件,如果已存在就覆盖
|
}
|
else
|
{
|
entry.Extract(strUnZipPath, Ionic.Zip.ExtractExistingFileAction.DoNotOverwrite);//解压文件,如果已存在不覆盖
|
}
|
}
|
return true;
|
}
|
}
|
catch (Exception)
|
{
|
return false;
|
}
|
}
|
|
}
|
}
|