-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathZipHelper.cs
More file actions
287 lines (252 loc) · 10.4 KB
/
Copy pathZipHelper.cs
File metadata and controls
287 lines (252 loc) · 10.4 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* 作用:通过 SharpZipLib 实现文件/目录压缩/解压缩。
* */
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
namespace Helper.Core.Library
{
#region 逻辑处理辅助枚举
internal class ZipFormat
{
public const string Zip = ".zip";
public const string Rar = ".rar";
private static List<string> formatList;
public static List<string> FormatList
{
get
{
if (formatList == null || formatList.Count == 0)
{
formatList = new List<string>()
{
Zip,
Rar
};
}
return formatList;
}
}
}
#endregion
public class ZipHelper
{
#region 私有属性常量
private const string DirectoryNotExistsException = "目录不存在";
private const string FileNotExistsException = "文件不存在";
private const string ZipFormatErrorException = "文件后缀不正确";
#endregion
#region 对外公开方法
/// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="directoryPath">压缩目录</param>
/// <param name="zipPath">Zip 文件路径</param>
/// <param name="append">是否附加</param>
/// <param name="password">密码</param>
/// <param name="compressLevel">压缩等级,取值范围:0-9</param>
/// <returns></returns>
public static bool Compress(string directoryPath, string zipPath, bool append, string password = "", int compressLevel = 6)
{
// 如果目录不存在
if (!Directory.Exists(directoryPath)) throw new Exception(DirectoryNotExistsException);
directoryPath = directoryPath.Replace("/", "\\");
if (directoryPath.EndsWith("\\")) directoryPath = directoryPath.TrimEnd(new char[] { '\\' });
// 检查后缀
string suffix = FileHelper.GetSuffix(zipPath);
// 如果生成文件后缀不是 .zip|.rar,抛出异常
if (ZipFormat.FormatList.IndexOf(suffix) < 0) throw new Exception(ZipFormatErrorException);
// 创建文件目录
bool directoryResult = FileHelper.CreateDirectory(zipPath);
if (!directoryResult) return false;
if (!append)
{
if (File.Exists(zipPath)) File.Delete(zipPath);
}
bool result = false;
using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipPath)))
{
zipStream.SetLevel(compressLevel);
if (!string.IsNullOrEmpty(password)) zipStream.Password = password;
result = CompressDirectory(directoryPath, zipStream);
zipStream.Finish();
}
return result;
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="filePath">文件地址</param>
/// <param name="zipPath">生成 Zip 路径</param>
/// <param name="password">密码</param>
/// <param name="compressLevel">压缩等级,取值范围:0-9</param>
/// <returns></returns>
public static bool Compress(string filePath, string zipPath, string password = "", int compressLevel = 6)
{
if (!File.Exists(filePath)) throw new Exception(FileNotExistsException);
ZipOutputStream zipStream = null;
FileStream fileStream = null;
ZipEntry zipEntry = null;
try
{
// 检查后缀
string suffix = FileHelper.GetSuffix(zipPath);
// 如果生成文件后缀不是 .zip|.rar,抛出异常
if (ZipFormat.FormatList.IndexOf(suffix) < 0) throw new Exception(ZipFormatErrorException);
// 创建文件目录
bool directoryResult = FileHelper.CreateDirectory(zipPath);
if (!directoryResult) return false;
fileStream = File.OpenRead(filePath);
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
fileStream.Close();
using (fileStream = File.Create(zipPath))
{
using (zipStream = new ZipOutputStream(fileStream))
{
if (!string.IsNullOrEmpty(password)) zipStream.Password = password;
zipEntry = new ZipEntry(Path.GetFileName(filePath));
zipStream.PutNextEntry(zipEntry);
zipStream.SetLevel(compressLevel);
zipStream.Write(buffer, 0, buffer.Length);
}
}
return true;
}
catch
{
throw;
}
finally
{
if (zipStream != null) zipStream.Close();
if (fileStream != null) fileStream.Close();
if (zipEntry != null) zipEntry = null;
}
}
/// <summary>
/// 解压缩文件
/// </summary>
/// <param name="zipPath">Zip 路径</param>
/// <param name="directoryPath">输出目录</param>
/// <param name="password">密码</param>
/// <returns></returns>
public static bool UnCompress(string zipPath, string directoryPath, string password = "")
{
FileStream fileStream = null;
ZipInputStream zipStream = null;
ZipEntry zipEntry = null;
try
{
string suffix = FileHelper.GetSuffix(zipPath);
// 如果生成文件后缀不是 .zip|.rar,抛出异常
if (ZipFormat.FormatList.IndexOf(suffix) < 0) throw new Exception(ZipFormatErrorException);
// 如果文件不存在
if (!File.Exists(zipPath)) throw new Exception(FileNotExistsException);
// 创建输出目录
bool directoryResult = FileHelper.CreateDirectory(directoryPath, true);
if (!directoryResult) return false;
zipStream = new ZipInputStream(File.OpenRead(zipPath));
if (!string.IsNullOrEmpty(password)) zipStream.Password = password;
string filePath = "";
while ((zipEntry = zipStream.GetNextEntry()) != null)
{
if (!string.IsNullOrEmpty(zipEntry.Name))
{
filePath = Path.Combine(directoryPath, zipEntry.Name);
filePath = filePath.Replace('/', '\\');
if (filePath.EndsWith("\\"))
{
Directory.CreateDirectory(filePath);
continue;
}
if (File.Exists(filePath)) File.Delete(filePath);
using (fileStream = File.Create(filePath))
{
int size = 1024;
byte[] data = new byte[size];
while (true)
{
size = zipStream.Read(data, 0, data.Length);
if (size > 0)
{
fileStream.Write(data, 0, data.Length);
}
else
{
break;
}
}
fileStream.Flush();
}
}
}
return true;
}
catch
{
throw;
}
finally
{
if (fileStream != null) fileStream.Close();
if (zipStream != null) zipStream.Close();
if (zipEntry != null) zipEntry = null;
}
}
#endregion
#region 逻辑处理私有方法
/// <summary>
/// 遍历压缩文件夹文件
/// </summary>
/// <param name="directoryPath"></param>
/// <param name="zipStream"></param>
/// <param name="parentDirectoryPath"></param>
/// <returns></returns>
private static bool CompressDirectory(string directoryPath, ZipOutputStream zipStream, string parentDirectoryPath = "")
{
ZipEntry zipEntry = null;
FileStream fileStream = null;
string entryDirectoryPath = "";
bool result = false;
try
{
entryDirectoryPath = Path.Combine(parentDirectoryPath, Path.GetFileName(directoryPath) + "\\");
zipEntry = new ZipEntry(entryDirectoryPath);
zipStream.PutNextEntry(zipEntry);
zipStream.Flush();
string[] filePathList = Directory.GetFiles(directoryPath);
foreach (string filePath in filePathList)
{
fileStream = File.OpenRead(filePath);
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
zipEntry = new ZipEntry(Path.Combine(entryDirectoryPath + Path.GetFileName(filePath)));
zipEntry.DateTime = DateTime.Now;
zipEntry.Size = fileStream.Length;
fileStream.Close();
zipStream.PutNextEntry(zipEntry);
zipStream.Write(buffer, 0, buffer.Length);
}
result = true;
}
catch
{
throw;
}
finally
{
if (zipEntry != null) zipEntry = null;
if (fileStream != null) fileStream.Close();
}
string[] childDirectoryPathList = Directory.GetDirectories(directoryPath);
foreach (string childDirectoryPath in childDirectoryPathList)
{
if (!CompressDirectory(childDirectoryPath, zipStream, entryDirectoryPath)) return false;
}
return result;
}
#endregion
}
}