弹来弹去跑马灯!

c# 用DotNetZip来解压/压缩文件

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//https://archive.codeplex.com/?p=dotnetzip
//最新在Nuget 下载DotNetZip
 
using Ionic.Zip;
 
 
private void button1_Click(object sender, EventArgs e)
        {
            string msg = "";
            ExtractFile("D:\\1.zip", "d:\\OK\\");
            ExtractFile("D:\\1.zip", "d:\\OK\\", false, out msg);
        }
 
 
 
        public static bool ZipFolder(
                    String sourceFilePath,
                    String targetFileFullPath,
                    Boolean isUsePassword,
                    Int32 maxOutputSegmentSiez,
                    out String errMessage)
        {
            try
            {
                using (ZipFile zip = new ZipFile(Encoding.Default))
                {
                    errMessage = String.Empty;
                    zip.Comment = "压缩文件时间" + System.DateTime.Now.ToString("G");
                    zip.Name = Guid.NewGuid().ToString().ToUpper() + ".zip";
                    if (isUsePassword)
                        zip.Password = "123";
                    zip.MaxOutputSegmentSize = maxOutputSegmentSiez * 1000;
                    zip.BufferSize = 1024;
                    zip.CaseSensitiveRetrieval = true;
                    zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
                    zip.AddDirectory(sourceFilePath);
                    zip.Save(targetFileFullPath);
                    return true;
                }
            }
            catch (Exception ex) { errMessage = ex.Message; return false; }
        }
 
 
        public static bool ExtractFile(String sourceFileFullPath, String targetFolderPath, Boolean isUsePassword, out String errMessage)
        {
            try
            {
                Encoding encoding = Encoding.UTF8;
                errMessage = String.Empty;
                var options = new ReadOptions { StatusMessageWriter = System.Console.Out, Encoding = encoding };
                using (ZipFile zip = ZipFile.Read(sourceFileFullPath, options))
                {
                    if (isUsePassword)
                    { //假设有密码123
                        zip.Password = "123";
                    }
                    zip.AlternateEncoding = encoding;
                    zip.ExtractAll(targetFolderPath, ExtractExistingFileAction.OverwriteSilently);//一次批量解压
                    return true;
                }
            }
            catch (Exception ex) { errMessage = ex.Message; return false; }
        }
 
 
 
 
        public static bool ExtractFile(String sourceFileFullPath, String targetFolderPath)
        {
            try
            {
                Encoding encoding = Encoding.UTF8;
 
                var options = new ReadOptions { StatusMessageWriter = System.Console.Out, Encoding = encoding };
                using (ZipFile zip = ZipFile.Read(sourceFileFullPath, options))
                {
                    zip.AlternateEncoding = encoding;
                    foreach (var f in zip.Entries)
                    {//一个个解压
                        f.Extract(targetFolderPath, ExtractExistingFileAction.OverwriteSilently);
                    }
 
                    return true;
                }
            }
            catch (Exception ex)
            {
 
                return false;
            }
        }

  

posted @   wgscd  阅读(3285)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示