tx
2025-04-09 fa7510e1ed63df0366787fa4ed1b3db6426d2b46
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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;
            }
        }
        
    }
}