http://www.icsharpcode.net/opensource/sharpziplib/ 有SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, GZip, BZip2 和Tar格式
我们需要dll 在官网上也有,也可以从百度网盘下载
好了,深入的大家还要多多研究,今天我们简单介绍一下 简单的 单文件、文件夹的压缩和解压
先给大家看一下效果:
一、引入ICSharpCode.SharpZipLib
我们新建个帮助类 ZipHelper.cs 然后 添加 dll 引用
二、添加完dll引用之后 我们 需要添加 这几个Using引用
1 using ICSharpCode.SharpZipLib.Checksums; 2 using ICSharpCode.SharpZipLib.Zip; 3 using System;4 using System.IO;
三、压缩单个文件
这里我添加了几个参数 大家可以根据自己的需要 修改一下
1 /// <summary> 2 /// ZIP:压缩单个文件 3 /// add yuangang by 2016-06-13 4 /// </summary> 5 /// <param name="FileToZip">需要压缩的文件(绝对路径)</param> 6 /// <param name="ZipedPath">压缩后的文件路径(绝对路径)</param> 7 /// <param name="ZipedFileName">压缩后的文件名称(文件名,默认 同源文件同名)</param> 8 /// <param name="CompressionLevel">压缩等级(0 无 - 9 最高,默认 5)</param> 9 /// <param name="BlockSize">缓存大小(每次写入文件大小,默认 2048)</param> 10 /// <param name="IsEncrypt">是否加密(默认 加密)</param> 11 public static void ZipFile(string FileToZip, string ZipedPath, string ZipedFileName = "", int CompressionLevel = 5, int BlockSize = 2048, bool IsEncrypt = true) 12 { 13 //如果文件没有找到,则报错 14 if (!System.IO.File.Exists(FileToZip)) 15 { 16 throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!"); 17 } 18 19 //文件名称(默认同源文件名称相同) 20 string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + "\\" + new FileInfo(FileToZip).Name.Substring(0, new FileInfo(FileToZip).Name.LastIndexOf('.')) + ".zip" : ZipedPath + "\\" + ZipedFileName + ".zip"; 21 22 using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName)) 23 { 24 using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile)) 25 { 26 using (System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read)) 27 { 28 string fileName = FileToZip.Substring(FileToZip.LastIndexOf("\\") + 1); 29 30 ZipEntry ZipEntry = new ZipEntry(fileName); 31 32 if (IsEncrypt) 33 { 34 //压缩文件加密 35 ZipStream.Password = “123”; 36 } 37 38 ZipStream.PutNextEntry(ZipEntry); 39 40 //设置压缩级别 41 ZipStream.SetLevel(CompressionLevel); 42 43 //缓存大小 44 byte[] buffer = new byte[BlockSize]; 45 46 int sizeRead = 0; 47 48 try 49 { 50 do 51 { 52 sizeRead = StreamToZip.Read(buffer, 0, buffer.Length); 53 ZipStream.Write(buffer, 0, sizeRead); 54 } 55 while (sizeRead > 0); 56 } 57 catch (System.Exception ex) 58 { 59 throw ex; 60 } 61 62 StreamToZip.Close(); 63 } 64 65 ZipStream.Finish(); 66 ZipStream.Close(); 67 } 68 69 ZipFile.Close(); 70 } 71 }
四、压缩文件夹
1 /// <summary> 2 /// ZIP:压缩文件夹 3 /// add yuangang by 2016-06-13 4 /// </summary> 5 /// <param name="DirectoryToZip">需要压缩的文件夹(绝对路径)</param> 6 /// <param name="ZipedPath">压缩后的文件路径(绝对路径)</param> 7 /// <param name="ZipedFileName">压缩后的文件名称(文件名,默认 同源文件夹同名)</param> 8 /// <param name="IsEncrypt">是否加密(默认 加密)</param> 9 public static void ZipDirectory(string DirectoryToZip, string ZipedPath, string ZipedFileName = "", bool IsEncrypt = true) 10 { 11 //如果目录不存在,则报错 12 if (!System.IO.Directory.Exists(DirectoryToZip)) 13 { 14 throw new System.IO.FileNotFoundException("指定的目录: " + DirectoryToZip + " 不存在!"); 15 } 16 17 //文件名称(默认同源文件名称相同) 18 string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + "\\" + new DirectoryInfo(DirectoryToZip).Name + ".zip" : ZipedPath + "\\" + ZipedFileName + ".zip"; 19 20 using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName)) 21 { 22 using (ZipOutputStream s = new ZipOutputStream(ZipFile)) 23 { 24 if (IsEncrypt) 25 { 26 //压缩文件加密 27 s.Password = “123”; 28 } 29 ZipSetp(DirectoryToZip, s, ""); 30 } 31 } 32 } 33 /// <summary> 34 /// 递归遍历目录 35 /// add yuangang by 2016-06-13 36 /// </summary> 37 private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath) 38 { 39 if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar) 40 { 41 strDirectory += Path.DirectorySeparatorChar; 42 } 43 Crc32 crc = new Crc32(); 44 45 string[] filenames = Directory.GetFileSystemEntries(strDirectory); 46 47 foreach (string file in filenames)// 遍历所有的文件和目录 48 { 49 50 if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件 51 { 52 string pPath = parentPath; 53 pPath += file.Substring(file.LastIndexOf("\\") + 1); 54 pPath += "\\"; 55 ZipSetp(file, s, pPath); 56 } 57 58 else // 否则直接压缩文件 59 { 60 //打开压缩文件 61 using (FileStream fs = File.OpenRead(file)) 62 { 63 64 byte[] buffer = new byte[fs.Length]; 65 fs.Read(buffer, 0, buffer.Length); 66 67 string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1); 68 ZipEntry entry = new ZipEntry(fileName); 69 70 entry.DateTime = DateTime.Now; 71 entry.Size = fs.Length; 72 73 fs.Close(); 74 75 crc.Reset(); 76 crc.Update(buffer); 77 78 entry.Crc = crc.Value; 79 s.PutNextEntry(entry); 80 81 s.Write(buffer, 0, buffer.Length); 82 } 83 } 84 } 85 }
五、解压一个ZIP文件
1 /// <summary> 2 /// ZIP:解压一个zip文件 3 /// add yuangang by 2016-06-13 4 /// </summary> 5 /// <param name="ZipFile">需要解压的Zip文件(绝对路径)</param> 6 /// <param name="TargetDirectory">解压到的目录</param> 7 /// <param name="Password">解压密码</param> 8 /// <param name="OverWrite">是否覆盖已存在的文件</param> 9 public static void UnZip(string ZipFile, string TargetDirectory, string Password, bool OverWrite = true) 10 { 11 //如果解压到的目录不存在,则报错 12 if (!System.IO.Directory.Exists(TargetDirectory)) 13 { 14 throw new System.IO.FileNotFoundException("指定的目录: " + TargetDirectory + " 不存在!"); 15 } 16 //目录结尾 17 if (!TargetDirectory.EndsWith("\\")) { TargetDirectory = TargetDirectory + "\\"; } 18 19 using (ZipInputStream zipfiles = new ZipInputStream(File.OpenRead(ZipFile))) 20 { 21 zipfiles.Password = Password; 22 ZipEntry theEntry; 23 24 while ((theEntry = zipfiles.GetNextEntry()) != null) 25 { 26 string directoryName = ""; 27 string pathToZip = ""; 28 pathToZip = theEntry.Name; 29 30 if (pathToZip != "") 31 directoryName = Path.GetDirectoryName(pathToZip) + "\\"; 32 33 string fileName = Path.GetFileName(pathToZip); 34 35 Directory.CreateDirectory(TargetDirectory + directoryName); 36 37 if (fileName != "") 38 { 39 if ((File.Exists(TargetDirectory + directoryName + fileName) && OverWrite) || (!File.Exists(TargetDirectory + directoryName + fileName))) 40 { 41 using (FileStream streamWriter = File.Create(TargetDirectory + directoryName + fileName)) 42 { 43 int size = 2048; 44 byte[] data = new byte[2048]; 45 while (true) 46 { 47 size = zipfiles.Read(data, 0, data.Length); 48 49 if (size > 0) 50 streamWriter.Write(data, 0, size); 51 else 52 break; 53 } 54 streamWriter.Close(); 55 } 56 } 57 } 58 } 59 60 zipfiles.Close(); 61 } 62 }