说说我利用ICSharpCode进行压缩和解压缩的一些自己的一下实践过程
1:组件下载地址
参考文章:C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件
2: 文件类
//**************************************************************************************** //作者:轻狂书生 //博客地址:http://www.cnblogs.com/xiaoshuai1992 //create: 2014/2/26 //功能:实现文件压缩 //使用方法:设置参数进行压缩 //***************************************************************************************** using System; using ICSharpCode.SharpZipLib.Zip; using System.IO; using System.Text; namespace ZipCompress { public class CompressFile { /// <summary> /// 压缩文件参数 /// </summary> public ZipParameter ZipParameter { get; set; } /// <summary> /// 压缩文件参数 /// </summary> public DeZipParameter DeZipParameter { get; set; } /// <summary> /// 压缩文件参数 /// </summary> public ZipFolderParameter ZipFolderParameter { get; set; } /// <summary> /// 压缩文件返回压缩后的信息 /// </summary> /// <returns>string 返回压缩后的提示信息</returns> public void Compress() { FileStream Zip_File; ZipOutputStream ZipStream; ZipEntry ZipEntry; //string rtnMessage = "";//返回的信息 try { //循环文件,如果文件不存在就不添加的压缩里面 for (int i = 0; i < ZipParameter.ZIPFileList.Count; i++) { if (!File.Exists(ZipParameter.ZIPFileList[i])) { ZipParameter.ZIPFileList.RemoveAt(i); i--; } } //没有有文件下面的压缩不执行 if (ZipParameter.ZIPFileList.Count == 0) { return; } //没有目录进行创建 if (!Directory.Exists(ZipParameter.ZIPDirectoryName)) { Directory.CreateDirectory(ZipParameter.ZIPDirectoryName); } // 解决文档名称乱码问题,出现乱码就是因为CodePage不对 Encoding gbk = Encoding.GetEncoding("gbk"); ICSharpCode.SharpZipLib.Zip.ZipConstants.DefaultCodePage = gbk.CodePage; //文件路径,文档路径与文件名称 string strPath = ZipParameter.ZIPDirectoryName + ZipParameter.ZIPName; Zip_File = File.Create(strPath); ZipStream = new ZipOutputStream(Zip_File); ZipStream.Password = ZipParameter.Password; ZipStream.SetLevel(ZipParameter.Level); foreach (string FileToZip in ZipParameter.ZIPFileList) { Zip_File = File.OpenRead(FileToZip); byte[] buffer = new byte[Zip_File.Length]; Zip_File.Read(buffer, 0, buffer.Length); Zip_File.Close(); ZipEntry = new ZipEntry(Path.GetFileName(FileToZip)); ZipStream.PutNextEntry(ZipEntry); ZipStream.Write(buffer, 0, buffer.Length); } ZipStream.Finish(); ZipStream.Close(); Zip_File.Close(); } catch (Exception ex) { throw ex; } finally { GC.Collect(); GC.Collect(1); } } /// <summary> /// 压缩文件夹 /// </summary> /// <returns></returns> public void CompressFolder() { FastZip fz = new FastZip(); fz.CreateZip(ZipFolderParameter.ZipFileName, ZipFolderParameter.SourceDirectory, ZipFolderParameter.Recurse, ZipFolderParameter.FileFilter); } /// <summary> /// 解压缩文件,可以解压到下层文件夹 /// </summary> /// <returns>提示信息</returns> public void Decompress() { //路径处理 string strDirectory = DeZipParameter.ZIPDirectoryName; if (strDirectory == "") strDirectory = Directory.GetCurrentDirectory(); if (!strDirectory.EndsWith("\\")) strDirectory = strDirectory + "\\"; try { using (ZipInputStream s = new ZipInputStream(File.OpenRead(DeZipParameter.ZIPFullPath))) { s.Password = DeZipParameter.Password; ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = ""; string pathToZip = ""; pathToZip = theEntry.Name; if (pathToZip != "") directoryName = Path.GetDirectoryName(pathToZip) + "\\"; string fileName = Path.GetFileName(pathToZip); Directory.CreateDirectory(strDirectory + directoryName); if (fileName != "") { if ((File.Exists(strDirectory + directoryName + fileName)) || (!File.Exists(strDirectory + directoryName + fileName))) { using (FileStream streamWriter = File.Create(strDirectory + directoryName + fileName)) { int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) streamWriter.Write(data, 0, size); else break; } streamWriter.Close(); } } } } s.Close(); } } catch (Exception ex) { throw ex; } } } }
3:参数类
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ZipCompress { /// <summary> /// 解压缩的一些属性 /// </summary> public class DeZipParameter { private string zip_FullPath = ""; private string zip_DirectoryName = ""; private string zip_Password = ""; /// <summary> /// 要解压缩的文件的完整路径 /// </summary> public string ZIPFullPath { get { return zip_FullPath; } set { zip_FullPath = value; } } /// <summary> /// 解压缩的文件路径 /// </summary> public string ZIPDirectoryName { get { return zip_DirectoryName; } set { zip_DirectoryName = value; } } /// <summary> /// 加密密碼 /// </summary> public string Password { get { return zip_Password; } set { zip_Password = value; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ZipCompress { /// <summary> /// 压缩文件的类 /// </summary> public class ZipParameter { private string zip_Name = ""; private string zip_DirectoryName = ""; private List<string> zip_FileList = new List<string>(); private string zip_Password = ""; private int zip_Level = 6; /// <summary> /// 压缩后的文件名称 /// </summary> public string ZIPName { get { return zip_Name; } set { zip_Name = value; } } /// <summary> /// 压缩的文件路径 /// </summary> public string ZIPDirectoryName { get { return zip_DirectoryName; } set { zip_DirectoryName = value; } } /// <summary> /// 压缩的文件列表 /// </summary> public List<string> ZIPFileList { get { return zip_FileList; } set { zip_FileList = value; } } /// <summary> /// 加密密碼 /// </summary> public string Password { get { return zip_Password; } set { zip_Password = value; } } /// <summary> /// 压缩比例等级,默认为6,0 - store only to 9 - means best compression /// </summary> public int Level { get { return zip_Level; } set { zip_Level = value; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ZipCompress { /// <summary> /// 压缩文件夹的类 /// </summary> public class ZipFolderParameter { private string zipFileName = ""; private string sourceDirectory = ""; private bool recurse = true; private string fileFilter = ""; /// <summary> /// 生成的压缩文件 /// </summary> public string ZipFileName { get { return zipFileName; } set { zipFileName = value; } } /// <summary> /// 待压缩的文件夹 /// </summary> public string SourceDirectory { get { return sourceDirectory; } set { sourceDirectory = value; } } /// <summary> /// 文件过滤,即决定那些后缀的文件被压缩 /// </summary> public string FileFilter { get { return fileFilter; } set { fileFilter = value; } } /// <summary> /// 是否遍历子目录,True就遍历,False则不遍历 /// </summary> public bool Recurse { get { return recurse; } set { recurse = value; } } } }
4:使用
ZipParameter zp = new ZipParameter(); zp.Password = "123456"; zp.Level = 1; zp.ZIPDirectoryName = @"C:\Users\Public\Pictures\Sample Pictures\"; zp.ZIPName = "Test.zip"; zp.ZIPFileList.Add(@"C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg"); zp.ZIPFileList.Add(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"); zp.ZIPFileList.Add(@"C:\Users\WH130406P\Documents\RelatedTemplate.xlsx"); DeZipParameter dzp = new DeZipParameter(); dzp.ZIPFullPath = @"D:\项目必备\开发实例\wf20131124.zip"; dzp.ZIPDirectoryName = @"C:\Users\Public\Pictures\Sample Pictures\Test"; dzp.Password = "123456"; ZipFolderParameter zfp = new ZipFolderParameter(); zfp.ZipFileName = @"C:\Users\Public\Pictures\Sample Pictures\zipFolder.zip"; zfp.SourceDirectory = @"D:\xs\項目"; zfp.Recurse = true; CompressFile cprFile = new CompressFile(); cprFile.ZipParameter = zp; cprFile.DeZipParameter = dzp; cprFile.ZipFolderParameter = zfp; cprFile.Compress(); cprFile.Decompress(); cprFile.CompressFolder();
上面都是我使用的过程,都是实践成功了的