C# 制作Zip压缩包
压缩包制作也是很多项目中需要用到的功能。比如有大量的文件(假设有10000个)需要上传,1个1个的上传似乎不太靠谱(靠,那得传到什么时候啊?),这时我们可以制作一个压缩包zip,直接传这个文件到服务器端,然后在服务器目录解压,释放里面的文件。
这里我们选用ICSharpCode.SharpZipLib这个类库来实现我们的需求。
ICSharpCode.SharpZipLib提供了两个压缩算法的实现,分别是BZIP2(压缩时间长,压缩率高)和GZIP(压缩效率高,压缩率低)。
先定义一个枚举,用于程序标示是哪个压缩算法。
单个文件的压缩:
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) |
18 | FileStream inStream = File.OpenRead(srcFileName); |
19 | FileStream outStream = File.Open(zipFileName, FileMode.Create); |
22 | BZip2.Compress(inStream, outStream, true , 4096); |
31 | FileStream srcFile = File.OpenRead(srcFileName); |
33 | GZipOutputStream zipFile = new GZipOutputStream(File.Open(zipFileName, FileMode.Create)); |
35 | byte [] fileData = new byte [srcFile.Length]; |
36 | srcFile.Read(fileData, 0, ( int )srcFile.Length); |
37 | zipFile.Write(fileData, 0, fileData.Length); |
单个文件的解压缩:
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) |
17 | FileStream inStream = File.OpenRead(zipFileName); |
18 | FileStream outStream = File.Open(unzipFileName, FileMode.Create); |
19 | BZip2.Decompress(inStream, outStream, true ); |
22 | GZipInputStream zipFile = new GZipInputStream(File.OpenRead(zipFileName)); |
23 | FileStream destFile = File.Open(unzipFileName, FileMode.Create); |
25 | int bufferSize = 2048 * 2; |
26 | byte [] fileData = new byte [bufferSize]; |
28 | while (bufferSize > 0) |
30 | bufferSize = zipFile.Read(fileData, 0, bufferSize); |
31 | zipFile.Write(fileData, 0, bufferSize); |
上面的两个方法在引用了dll后,可以直接使用。
看到这里,相信读者有疑问了,如果我想让多个文件压缩到1个zip包里呢?甚至可以给文件加密?给zip包加注释?
好吧,我这里继续贴两个方法,都经过测试,可用。
制作压缩包:
01 | #region 制作压缩包(多个文件压缩到一个压缩包,支持加密、注释) |
03 | /// 制作压缩包(多个文件压缩到一个压缩包,支持加密、注释) |
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) |
12 | using (ZipOutputStream zos = new ZipOutputStream(File.Open(zipedFileName, FileMode.OpenOrCreate))) |
14 | if (compresssionLevel != 0) |
16 | zos.SetLevel(compresssionLevel); |
19 | if (! string .IsNullOrEmpty(password)) |
21 | zos.Password = password; |
24 | if (! string .IsNullOrEmpty(comment)) |
26 | zos.SetComment(comment); |
30 | foreach ( string file in Directory.GetFiles(topDirectoryName, "*.jpg" , SearchOption.AllDirectories)) |
32 | if (File.Exists(file)) |
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); |
39 | ZipEntry entry = new ZipEntry(item.Name); |
40 | zos.PutNextEntry(entry); |
41 | zos.Write(buffer, 0, buffer.Length); |
解压缩包:
01 | #region 解压缩包(将压缩包解压到指定目录) |
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) |
10 | using (ZipInputStream zis = new ZipInputStream(File.Open(zipedFileName, FileMode.OpenOrCreate))) |
12 | if (! string .IsNullOrEmpty(password)) |
14 | zis.Password = password; |
18 | while ((zipEntry = zis.GetNextEntry()) != null ) |
20 | string directoryName = Path.GetDirectoryName(unZipDirectory); |
21 | string pathName = Path.GetDirectoryName(zipEntry.Name); |
22 | string fileName = Path.GetFileName(zipEntry.Name); |
24 | pathName = pathName.Replace( "." , "$" ); |
25 | directoryName += "\\" + pathName; |
27 | if (!Directory.Exists(directoryName)) |
29 | Directory.CreateDirectory(directoryName); |
32 | if (! string .IsNullOrEmpty(fileName)) |
34 | FileStream fs = File.Create(Path.Combine(directoryName, fileName)); |
36 | byte [] bytes = new byte [2048]; |
39 | size = zis.Read(bytes, 0, bytes.Length); |
42 | fs.Write(bytes, 0, size); |
调用时我们可以这么写:
ZipFileUtil.ZipFiles(@"E:\\test\\", "E:\\test.zip", 1, "admin", "this is admin's comment.");//制作压缩包
ZipFileUtil.UnZipFiles("E:\\test.zip", "E:\\guwei4037\\", "admin");//解压缩包