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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using System;
using System.IO;
using System.Collections;
using HH.WMS.Utils.ZipLib.Zip;
using HH.WMS.Utils.ZipLib.Checksums;
using HH.WMS.Utils.ZipLib.GZip;
 
namespace HH.WMS.Utils
{
    public class Zipper : IDisposable
    {
        #region ÊôÐÔ
        private string m_FolderToZIP;
        private ArrayList m_FileNamesToZIP;
        private string m_FileNameZipped;
 
        private ZipOutputStream m_ZipStream = null;
        private Crc32 m_Crc;
 
        #region Begin for Public Properties
        /**/
        /// <summary>
        /// Folder Name to ZIP
        /// Like "C:Test"
        /// </summary>
        public string FolderToZIP
        {
            get { return m_FolderToZIP; }
            set { m_FolderToZIP = value; }
        }
 
        /**/
        /// <summary>
        /// File Name to ZIP
        /// Like "C:TestTest.txt"
        /// </summary>
        public ArrayList FileNamesToZIP
        {
            get { return m_FileNamesToZIP; }
            set { m_FileNamesToZIP = value; }
        }
 
        /**/
        /// <summary>
        /// Zipped File Name 
        /// Like "C:TestMyZipFile.ZIP"
        /// </summary>
        public string FileNameZipped
        {
            get { return m_FileNameZipped; }
            set { m_FileNameZipped = value; }
        }
        #endregion
 
        /**/
        /// <summary>
        /// The construct
        /// </summary>
        public Zipper()
        {
            this.m_FolderToZIP = "";
            this.m_FileNamesToZIP = new ArrayList();
            this.m_FileNameZipped = "";
        }
        #endregion
 
        #region ZipFolder
        /**/
        /// <summary>
        /// Zip one folder : single level
        /// Before doing this event, you must set the Folder and the ZIP file name you want
        /// </summary>
        public void ZipFolder()
        {
            if (this.m_FolderToZIP.Trim().Length == 0)
            {
                throw new Exception("You must setup the folder name you want to zip!");
            }
 
            if (Directory.Exists(this.m_FolderToZIP) == false)
            {
                throw new Exception("The folder you input does not exist! Please check it!");
            }
 
            if (this.m_FileNameZipped.Trim().Length == 0)
            {
                throw new Exception("You must setup the zipped file name!");
            }
 
            string[] fileNames = Directory.GetFiles(this.m_FolderToZIP.Trim());
 
            if (fileNames.Length == 0)
            {
                throw new Exception("Can not find any file in this folder(" + this.m_FolderToZIP + ")!");
            }
 
            // Create the Zip File
            this.CreateZipFile(this.m_FileNameZipped);
 
            // Zip all files
            foreach (string file in fileNames)
            {
                this.ZipSingleFile(file);
            }
 
            // Close the Zip File
            this.CloseZipFile();
        }
        #endregion
 
        #region ZipFiles
        /**/
        /// <summary>
        /// Zip files
        /// Before doing this event, you must set the Files name and the ZIP file name you want
        /// </summary>
        public void ZipFiles()
        {
            if (this.m_FileNamesToZIP.Count == 0)
            {
                throw new Exception("You must setup the files name you want to zip!");
            }
 
            foreach (object file in this.m_FileNamesToZIP)
            {
                if (File.Exists(((string)file).Trim()) == false)
                {
                    throw new Exception("The file(" + (string)file + ") you input does not exist! Please check it!");
                }
            }
 
            if (this.m_FileNameZipped.Trim().Length == 0)
            {
                throw new Exception("You must input the zipped file name!");
            }
 
            // Create the Zip File
            this.CreateZipFile(this.m_FileNameZipped);
 
            // Zip this File
            foreach (object file in this.m_FileNamesToZIP)
            {
                this.ZipSingleFile((string)file);
            }
 
            // Close the Zip File
            this.CloseZipFile();
        }
        #endregion
 
        #region CreateZipFile
        /**/
        /// <summary>
        /// Create Zip File by FileNameZipped
        /// </summary>
        /// <param name="fileNameZipped">zipped file name like "C:TestMyZipFile.ZIP"</param>
        private void CreateZipFile(string fileNameZipped)
        {
            this.m_Crc = new Crc32();
            this.m_ZipStream = new ZipOutputStream(File.Create(fileNameZipped));
            this.m_ZipStream.SetLevel(6); // 0 - store only to 9 - means best compression
        }
        #endregion
 
        #region CloseZipFile
        /**/
        /// <summary>
        /// Close the Zip file
        /// </summary>
        private void CloseZipFile()
        {
            this.m_ZipStream.Finish();
            this.m_ZipStream.Close();
            this.m_ZipStream = null;
        }
        #endregion
 
        #region ZipSingleFile
        /**/
        /// <summary>
        /// Zip single file 
        /// </summary>
        /// <param name="fileName">file name like "C:TestTest.txt"</param>
        private void ZipSingleFile(string fileNameToZip)
        {
            // Open and read this file
            FileStream fso = File.OpenRead(fileNameToZip);
 
            // Read this file to Buffer
            byte[] buffer = new byte[fso.Length];
            fso.Read(buffer, 0, buffer.Length);
 
            // Create a new ZipEntry
            ZipEntry zipEntry = new ZipEntry(fileNameToZip.Split('\\')[fileNameToZip.Split('\\').Length - 1]);
            zipEntry.DateTime = DateTime.Now;
            // set Size and the crc, because the information
            // about the size and crc should be stored in the header
            // if it is not set it is automatically written in the footer.
            // (in this case size == crc == -1 in the header)
            // Some ZIP programs have problems with zip files that don't store
            // the size and crc in the header.
            zipEntry.Size = fso.Length;
 
            fso.Close();
            fso = null;
 
            // Using CRC to format the buffer
            this.m_Crc.Reset();
            this.m_Crc.Update(buffer);
            zipEntry.Crc = this.m_Crc.Value;
 
            // Add this ZipEntry to the ZipStream
            this.m_ZipStream.PutNextEntry(zipEntry);
            this.m_ZipStream.Write(buffer, 0, buffer.Length);
        }
        #endregion
 
        #region IDisposable member
 
        /**/
        /// <summary>
        /// Release all objects
        /// </summary>
        public void Dispose()
        {
            if (this.m_ZipStream != null)
            {
                this.m_ZipStream.Close();
                this.m_ZipStream = null;
            }
        }
 
        #endregion
    }
}