jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
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
93
94
95
96
97
98
99
100
101
102
103
104
ICSharpCode.SharpZipLib.Zip使用示例代码及下载地址
 
 
ICSharpCode.SharpZipLib.Zip封装了ZIP文件在线压缩解压的一个dll,这里收集了ICSharpCode.SharpZipLib.Zip使用示例代码,方便有需要的人使用.下载地址:http://www.icsharpcode.net/OpenSource/SharpZipLib/
 
Code
class ZIP
    {
      /**//// <summary>压缩文件</summary>
        /// <param name="filename">filename生成的文件的名称,如:C\123\123.zip</param>
       /// <param name="directory">directory要压缩的文件夹路径</param>
        /// <returns></returns>
       public static bool PackFiles(string filename, string directory)
        {
          try
            {
 
                directory = directory.Replace("/", "\\");
 
                if (!directory.EndsWith("\\"))
                    directory += "\\";
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
               //ICSharpCode.SharpZipLib.Zip.ZipFile pp = new ZipFile();
                //FastZip fz = new FastZip();
                //fz.CreateEmptyDirectories = true;
                //fz.CreateZip(filename, directory, true, "");
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
 
 
 
        /**//// <summary>解压文件</summary>
        /// <param name="file">压缩文件的名称,如:C:\123\123.zip</param>
       /// <param name="dir">dir要解压的文件夹路径</param>
        /// <returns></returns>
        public static bool UnpackFiles(string file, string dir)
        {
           try
            {
                if (!File.Exists(file))
                    return false;
 
                dir = dir.Replace("/", "\\");
               if (!dir.EndsWith("\\"))
                   dir += "\\";
 
                if (!Directory.Exists(dir))
                   Directory.CreateDirectory(dir);
 
                ZipInputStream s = new ZipInputStream(File.OpenRead(file));
                ZipEntry theEntry;
               while ((theEntry = s.GetNextEntry()) != null)
               {
 
                    string directoryName = Path.GetDirectoryName(theEntry.Name);
                    string fileName = Path.GetFileName(theEntry.Name);
 
                    if (directoryName != String.Empty)
                        Directory.CreateDirectory(dir + directoryName);
 
                  if (fileName != String.Empty)
                    {
                       FileStream streamWriter = File.Create(dir + 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();
               return true;
           }
            catch (Exception)
            {
               return false;
           }
        }
 
    }