Shuxia Ning
2024-11-06 adf8dc1c7cae1b12f486dcdb3d7daf4a5a59ec52
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
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.IO;
 
namespace IStation
{
    /// <summary>
    /// 压缩辅助类
    /// </summary>
    public class ZipHelper
    {
        /// <summary>
        /// 压缩文件夹
        /// </summary>
        public static void ZipFolder(string src, string dest)
        {
            if (File.Exists(dest))
                File.Delete(dest);
            var zip = new FastZip();
            zip.CreateZip(dest, src, true, String.Empty);
        }
 
        /// <summary>
        /// 解压文件夹
        /// </summary>
        public static void UnZipFolder(string src, string dest)
        {
            var zip = new FastZip();
            zip.ExtractZip(src, dest, String.Empty);
        }
 
 
        /// <summary>
        /// 压缩文件
        /// </summary>
        public static void ZipFile(string src, string dest)
        {
            using (ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(dest)))
            {
                s.SetLevel(6);
                using (FileStream fs = System.IO.File.OpenRead(src))
                {
                    byte[] buffer = new byte[4 * 1024];
                    ZipEntry entry = new ZipEntry(Path.GetFileName(src));
                    entry.DateTime = DateTime.Now;
                    s.PutNextEntry(entry);//将文件写入压缩包
                    int sourceBytes;
                    do
                    {
                        sourceBytes = fs.Read(buffer, 0, buffer.Length);
                        s.Write(buffer, 0, sourceBytes);
                    } while (sourceBytes > 0);
                    s.CloseEntry();
                }
            }
        }
 
        /// <summary>
        /// 解压文件
        /// </summary>
        public static void UnZipFile(string src, string dest)
        {
            using (ZipInputStream s = new ZipInputStream(System.IO.File.OpenRead(src)))
            {
                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    string fileName = Path.Combine(dest, Path.GetFileName(theEntry.Name));
                    if (fileName == string.Empty)
                        continue;
                    using (FileStream streamWriter = System.IO.File.Create(fileName))
                    {
                        int size = 4096;
                        byte[] data = new byte[4 * 1024];
                        while (true)
                        {
                            size = s.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, size);
                            }
                            else break;
                        }
                    }
 
                }
            }
        }
 
    }
}