标签:
压缩包制作也是很多项目中需要用到的功能。比如有大量的文件(假设有10000个)需要上传,1个1个的上传似乎不太靠谱(靠,那得传到什么时候啊?),这时我们可以制作一个压缩包zip,直接传这个文件到服务器端,然后在服务器目录解压,释放里面的文件。
这里我们选用ICSharpCode.SharpZipLib这个类库来实现我们的需求。
ICSharpCode.SharpZipLib提供了两个压缩算法的实现,分别是BZIP2(压缩时间长,压缩率高)和GZIP(压缩效率高,压缩率低)。
先定义一个枚举,用于程序标示是哪个压缩算法。
01 |
/// <summary> |
02 |
/// 压缩枚举 |
03 |
/// </summary> |
04 |
public enum ZipEnum |
05 |
{ |
06 |
//压缩时间长,压缩率高 |
07 |
BZIP2, |
08 |
09 |
//压缩效率高,压缩率低 |
10 |
GZIP |
11 |
} |
单个文件的压缩:
01 |
#region 制作压缩包(单个文件压缩) |
02 |
/// <summary> |
03 |
/// 制作压缩包(单个文件压缩) |
04 |
/// </summary> |
05 |
/// <param name="sourceFileName">原文件</param> |
06 |
/// <param name="zipFileName">压缩文件</param> |
07 |
/// <param name="zipEnum">压缩算法枚举</param> |
08 |
/// <returns>压缩成功标志</returns> |
09 |
public static bool ZipFile( string srcFileName, string zipFileName, ZipEnum zipEnum) |
10 |
{ |
11 |
bool flag = true ; |
12 |
try |
13 |
{ |
14 |
switch (zipEnum) |
15 |
{ |
16 |
case ZipEnum.BZIP2: |
17 |
18 |
FileStream inStream = File.OpenRead(srcFileName); |
19 |
FileStream outStream = File.Open(zipFileName, FileMode.Create); |
20 |
21 |
//参数true表示压缩完成后,inStream和outStream连接都释放 |
22 |
BZip2.Compress(inStream, outStream, true , 4096); |
23 |
24 |
inStream.Close(); |
25 |
outStream.Close(); |
26 |
27 |
28 |
break ; |
29 |
case ZipEnum.GZIP: |
30 |
31 |
FileStream srcFile = File.OpenRead(srcFileName); |
32 |
33 |
GZipOutputStream zipFile = new GZipOutputStream(File.Open(zipFileName, FileMode.Create)); |
34 |
35 |
byte [] fileData = new byte [srcFile.Length]; |
36 |
srcFile.Read(fileData, 0, ( int )srcFile.Length); |
37 |
zipFile.Write(fileData, 0, fileData.Length); |
38 |
39 |
srcFile.Close(); |
40 |
zipFile.Close(); |
41 |
42 |
break ; |
43 |
default : break ; |
44 |
} |
45 |
} |
46 |
catch |
47 |
{ |
48 |
flag = false ; |
49 |
} |
50 |
return flag; |
51 |
} |
52 |
#endregion |
单个文件的解压缩:
01 |
#region 解压缩包(单个文件解压缩) |
02 |
/// <summary> |
03 |
/// 解压缩包(单个文件解压缩) |
04 |
/// </summary> |
05 |
/// <param name="zipFileName">压缩文件</param> |
06 |
/// <param name="unzipFileName">解压缩文件</param> |
07 |
/// <param name="zipEnum">压缩算法枚举</param> |
08 |
/// <returns>压缩成功标志</returns> |
09 |
public static bool UnZipFile( string zipFileName, string unzipFileName, ZipEnum zipEnum) |
10 |
{ |
11 |
bool flag = true ; |
12 |
try |
13 |
{ |
14 |
switch (zipEnum) |
15 |
{ |
16 |
case ZipEnum.BZIP2: |
17 |
FileStream inStream = File.OpenRead(zipFileName); |
18 |
FileStream outStream = File.Open(unzipFileName, FileMode.Create); |
19 |
BZip2.Decompress(inStream, outStream, true ); |
20 |
break ; |
21 |
case ZipEnum.GZIP: |
22 |
GZipInputStream zipFile = new GZipInputStream(File.OpenRead(zipFileName)); |
23 |
FileStream destFile = File.Open(unzipFileName, FileMode.Create); |
24 |
25 |
int bufferSize = 2048 * 2; |
26 |
byte [] fileData = new byte [bufferSize]; |
27 |
28 |
while (bufferSize > 0) |
29 |
{ |
30 |
bufferSize = zipFile.Read(fileData, 0, bufferSize); |
31 |
zipFile.Write(fileData, 0, bufferSize); |
32 |
} |
33 |
destFile.Close(); |
34 |
zipFile.Close(); |
35 |
break ; |
36 |
default : break ; |
37 |
} |
38 |
} |
39 |
catch |
40 |
{ |
41 |
flag = false ; |
42 |
} |
43 |
return flag; |
44 |
} |
45 |
#endregion |
上面的两个方法在引用了dll后,可以直接使用。
看到这里,相信读者有疑问了,如果我想让多个文件压缩到1个zip包里呢?甚至可以给文件加密?给zip包加注释?
好吧,我这里继续贴两个方法,都经过测试,可用。
制作压缩包:
01 |
#region 制作压缩包(多个文件压缩到一个压缩包,支持加密、注释) |
02 |
/// <summary> |
03 |
/// 制作压缩包(多个文件压缩到一个压缩包,支持加密、注释) |
04 |
/// </summary> |
05 |
/// <param name="topDirectoryName">压缩文件目录</param> |
06 |
/// <param name="zipedFileName">压缩包文件名</param> |
07 |
/// <param name="compresssionLevel">压缩级别 1-9</param> |
08 |
/// <param name="password">密码</param> |
09 |
/// <param name="comment">注释</param> |
10 |
public static void ZipFiles( string topDirectoryName, string zipedFileName, int compresssionLevel, string password, string comment) |
11 |
{ |
12 |
using (ZipOutputStream zos = new ZipOutputStream(File.Open(zipedFileName, FileMode.OpenOrCreate))) |
13 |
{ |
14 |
if (compresssionLevel != 0) |
15 |
{ |
16 |
zos.SetLevel(compresssionLevel); //设置压缩级别 |
17 |
} |
18 |
19 |
if (! string .IsNullOrEmpty(password)) |
20 |
{ |
21 |
zos.Password = password; //设置zip包加密密码 |
22 |
} |
23 |
24 |
if (! string .IsNullOrEmpty(comment)) |
25 |
{ |
26 |
zos.SetComment(comment); //设置zip包的注释 |
27 |
} |
28 |
29 |
//循环设置目录下所有的*.jpg文件(支持子目录搜索) |
30 |
foreach ( string file in Directory.GetFiles(topDirectoryName, "*.jpg" , SearchOption.AllDirectories)) |
31 |
{ |
32 |
if (File.Exists(file)) |
33 |
{ |
34 |
FileInfo item = new FileInfo(file); |
35 |
FileStream fs = File.OpenRead(item.FullName); |
36 |
byte [] buffer = new byte [fs.Length]; |
37 |
fs.Read(buffer, 0, buffer.Length); |
38 |
39 |
ZipEntry entry = new ZipEntry(item.Name); |
40 |
zos.PutNextEntry(entry); |
41 |
zos.Write(buffer, 0, buffer.Length); |
42 |
} |
43 |
} |
44 |
} |
45 |
} |
46 |
#endregion |
解压缩包:
01 |
#region 解压缩包(将压缩包解压到指定目录) |
02 |
/// <summary> |
03 |
/// 解压缩包(将压缩包解压到指定目录) |
04 |
/// </summary> |
05 |
/// <param name="zipedFileName">压缩包名称</param> |
06 |
/// <param name="unZipDirectory">解压缩目录</param> |
07 |
/// <param name="password">密码</param> |
08 |
public static void UnZipFiles( string zipedFileName, string unZipDirectory, string password) |
09 |
{ |
10 |
using (ZipInputStream zis = new ZipInputStream(File.Open(zipedFileName, FileMode.OpenOrCreate))) |
11 |
{ |
12 |
if (! string .IsNullOrEmpty(password)) |
13 |
{ |
14 |
zis.Password = password; //有加密文件的,可以设置密码解压 |
15 |
} |
16 |
17 |
ZipEntry zipEntry; |
18 |
while ((zipEntry = zis.GetNextEntry()) != null ) |
19 |
{ |
20 |
string directoryName = Path.GetDirectoryName(unZipDirectory); |
21 |
string pathName = Path.GetDirectoryName(zipEntry.Name); |
22 |
string fileName = Path.GetFileName(zipEntry.Name); |
23 |
24 |
pathName = pathName.Replace( "." , "$" ); |
25 |
directoryName += "\\" + pathName; |
26 |
27 |
if (!Directory.Exists(directoryName)) |
28 |
{ |
29 |
Directory.CreateDirectory(directoryName); |
30 |
} |
31 |
32 |
if (! string .IsNullOrEmpty(fileName)) |
33 |
{ |
34 |
FileStream fs = File.Create(Path.Combine(directoryName, fileName)); |
35 |
int size = 2048; |
36 |
byte [] bytes = new byte [2048]; |
37 |
while ( true ) |
38 |
{ |
39 |
size = zis.Read(bytes, 0, bytes.Length); |
40 |
if (size > 0) |
41 |
{ |
42 |
fs.Write(bytes, 0, size); |
43 |
} |
44 |
else |
45 |
{ |
46 |
break ; |
47 |
} |
48 |
} |
49 |
fs.Close(); |
50 |
} |
51 |
} |
52 |
} |
53 |
} |
54 |
#endregion |
调用时我们可以这么写:
ZipFileUtil.ZipFiles(@"E:\\test\\", "E:\\test.zip", 1, "admin", "this is admin‘s comment.");//制作压缩包
ZipFileUtil.UnZipFiles("E:\\test.zip", "E:\\guwei4037\\", "admin");//解压缩包
标签:
原文地址:http://www.cnblogs.com/shouce/p/5353684.html