C#文件压缩为ZIP

ZipFile

.NET 4.5及以后版本自带的压缩

添加引用

1
2
System.IO.Compression
System.IO.Compression.FileSystem

压缩

1
2
3
string startPath = @"D:\Project\1734686502101";
string zipPath = @"D:\Project\test.zip";
ZipFile.CreateFromDirectory(startPath, zipPath);

ICSharpCode.SharpZipLib

添加依赖

1
Install-Package ICSharpCode.SharpZipLib.dll -Version 0.85.4.369

工具类

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
using System;
using System.IO;

using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;

namespace Z.Utils.Common
{
/// <summary>
/// Zip 压缩文件
/// </summary>
public class ZZipUtil
{
#region 加压方法

/// <summary>
/// 功能:压缩文件
/// </summary>
/// <param name="dirPath">被压缩的文件夹夹路径</param>
/// <param name="zipFilePath">生成压缩文件的路径,为空则默认与被压缩文件夹同一级目录,名称为:文件夹名+.zip</param>
/// <returns>是否压缩成功</returns>
public static void ZipFile(string dirPath, string zipFilePath)
{
ZipOutputStream outstream = new ZipOutputStream(File.Create(zipFilePath));
outstream.SetLevel(6);
ZipCompress(
dirPath,
outstream,
zipFilePath
);
outstream.Finish();
outstream.Close();
}

public static void ZipCompress
(
string dirPath,
ZipOutputStream outstream,
string zipFilePath
)
{
if (dirPath[dirPath.Length - 1] != Path.DirectorySeparatorChar)
{
dirPath += Path.DirectorySeparatorChar;
}
DirectoryInfo di = new DirectoryInfo(dirPath);
if (di.Exists)
{
Crc32 crc = new Crc32();
//获取指定目录下所有文件和子目录文件名称
string[] filenames = Directory.GetFileSystemEntries(dirPath);
//遍历文件
foreach (string file in filenames)
{
if (Directory.Exists(file))
{
ZipCompress(
file,
outstream,
zipFilePath
);
}
//否则,直接压缩文件
else
{
try
{
//打开文件
FileStream fs = File.OpenRead(file);
//定义缓存区对象
byte[] buffer = new byte[fs.Length];
//通过字符流,读取文件
// ReSharper disable once MustUseReturnValue
fs.Read(
buffer,
0,
buffer.Length
);
string tempfile = file.Substring(zipFilePath.LastIndexOf("\\", StringComparison.Ordinal) + 1);
ZipEntry entry = new ZipEntry(tempfile) { DateTime = DateTime.Now, Size = fs.Length };
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
outstream.PutNextEntry(entry);
//写文件
outstream.Write(
buffer,
0,
buffer.Length
);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
}

#endregion 加压方法

#region 解压

/// <summary>
/// 功能:解压zip格式的文件。
/// </summary>
/// <param name="zipFilePath">压缩文件路径</param>
/// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>
/// <param name="err">出错信息</param>
/// <returns>解压是否成功</returns>
public static bool UnZipFile
(
string zipFilePath,
string unZipDir,
out string err
)
{
err = "";
if (zipFilePath == string.Empty)
{
err = "压缩文件不能为空!";
return false;
}
if (!File.Exists(zipFilePath))
{
err = "压缩文件不存在!";
return false;
}

//解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹
if (unZipDir == string.Empty)
{
unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
}
if (!unZipDir.EndsWith("//"))
{
unZipDir += "//";
}
if (!Directory.Exists(unZipDir))
{
Directory.CreateDirectory(unZipDir);
}
try
{
using ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath));
while (s.GetNextEntry() is { } theEntry)
{
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
if (!string.IsNullOrEmpty(directoryName))
{
Directory.CreateDirectory(unZipDir + directoryName);
}
if (fileName != string.Empty)
{
using FileStream streamWriter = File.Create(unZipDir + theEntry.Name);
byte[] data = new byte[2048];
while (true)
{
int size = s.Read(
data,
0,
data.Length
);
if (size > 0)
{
streamWriter.Write(
data,
0,
size
);
}
else
{
break;
}
}
}
} //while
}
catch (Exception ex)
{
err = ex.Message;
return false;
}
return true;
} //解压结束

#endregion 解压
}
}

调用

1
ZZip.ZipFile(path, zipPath);