C#中关于zip压缩解压帮助类的封装
来源:互联网 发布:手机体育直播软件 编辑:程序博客网 时间:2024/06/06 10:50
c#下压缩解压,主要是用第三方类库进行封装的。ICSharpCode.SharpZipLib.dll类库,链接地址为你官方下载链接。压缩主要是用流的方式进行压缩的。
压缩文件及文件夹。文件压缩很简单,把待压缩的文件用流的方式读到内存中,然后放到压缩流中。就可以了。文件夹就稍微麻烦下了。因为要把待压缩的文件夹解压后保留文件夹文件的层次结构。所以我的实现方式就是 递归遍历文件夹中的文件。计算其相对位置放到压缩流中。
代码如下
- /// <summary>
- /// 压缩文件或者文件夹
- /// </summary>
- /// <param name="_depositPath">压缩后文件的存放路径 如C:\\windows\abc.zip</param>
- /// <returns></returns>
- public bool CompressionZip(string _depositPath)
- {
- bool result = true;
- FileStream fs = null;
- try
- {
- ZipOutputStream ComStream = new ZipOutputStream(File.Create(_depositPath));
- ComStream.SetLevel(9); //压缩等级
- foreach (string path in AbsolutePaths)
- {
- //如果是目录
- if (Directory.Exists(path))
- {
- ZipFloder(path, ComStream, path);
- }
- else if (File.Exists(path))//如果是文件
- {
- fs = File.OpenRead(path);
- byte[] bts = new byte[fs.Length];
- fs.Read(bts, 0, bts.Length);
- ZipEntry ze = new ZipEntry(new FileInfo(path).Name);
- ComStream.PutNextEntry(ze); //为压缩文件流提供一个容器
- ComStream.Write(bts, 0, bts.Length); //写入字节
- }
- }
- ComStream.Finish(); // 结束压缩
- ComStream.Close();
- }
- catch (Exception ex)
- {
- if (fs != null)
- {
- fs.Close();
- }
- errorMsg = ex.Message;
- result = false;
- }
- return result;
- }
- //压缩文件夹
- private void ZipFloder(string _OfloderPath, ZipOutputStream zos, string _floderPath)
- {
- foreach (FileSystemInfo item in new DirectoryInfo(_floderPath).GetFileSystemInfos())
- {
- if (Directory.Exists(item.FullName))
- {
- ZipFloder(_OfloderPath, zos, item.FullName);
- }
- else if (File.Exists(item.FullName))//如果是文件
- {
- DirectoryInfo ODir = new DirectoryInfo(_OfloderPath);
- string fullName2 = new FileInfo(item.FullName).FullName;
- string path = ODir.Name + fullName2.Substring(ODir.FullName.Length, fullName2.Length - ODir.FullName.Length);//获取相对目录
- FileStream fs = File.OpenRead(fullName2);
- byte[] bts = new byte[fs.Length];
- fs.Read(bts, 0, bts.Length);
- ZipEntry ze = new ZipEntry(path);
- zos.PutNextEntry(ze); //为压缩文件流提供一个容器
- zos.Write(bts, 0, bts.Length); //写入字节
- }
- }
- }
关于解压 解压就简单多了。有文件解压文件,有文件夹 遍历,解压其中的文件。解压的文件中已经包含了其与文件夹的层次关系。
- /// <summary>
- /// 解压
- /// </summary>
- /// <param name="_depositPath">压缩文件路径</param>
- /// <param name="_floderPath">解压的路径</param>
- /// <returns></returns>
- public bool DeCompressionZip(string _depositPath, string _floderPath)
- {
- bool result = true;
- FileStream fs=null;
- try
- {
- ZipInputStream InpStream = new ZipInputStream(File.OpenRead(_depositPath));
- ZipEntry ze = InpStream.GetNextEntry();//获取压缩文件中的每一个文件
- Directory.CreateDirectory(_floderPath);//创建解压文件夹
- while (ze != null)//如果解压完ze则是null
- {
- if (ze.IsFile)//压缩zipINputStream里面存的都是文件。带文件夹的文件名字是文件夹\\文件名
- {
- string[] strs=ze.Name.Split('\\');//如果文件名中包含’\\‘则表明有文件夹
- if (strs.Length > 1)
- {
- //两层循环用于一层一层创建文件夹
- for (int i = 0; i < strs.Length-1; i++)
- {
- string floderPath=_floderPath;
- for (int j = 0; j < i; j++)
- {
- floderPath = floderPath + "\\" + strs[j];
- }
- floderPath=floderPath+"\\"+strs[i];
- Directory.CreateDirectory(floderPath);
- }
- }
- fs = new FileStream(_floderPath+"\\"+ze.Name, FileMode.OpenOrCreate, FileAccess.Write);//创建文件
- //循环读取文件到文件流中
- while (true)
- {
- byte[] bts = new byte[1024];
- int i= InpStream.Read(bts, 0, bts.Length);
- if (i > 0)
- {
- fs.Write(bts, 0, i);
- }
- else
- {
- fs.Flush();
- fs.Close();
- break;
- }
- }
- }
- ze = InpStream.GetNextEntry();
- }
- }
- catch (Exception ex)
- {
- if (fs != null)
- {
- fs.Close();
- }
- errorMsg = ex.Message;
- result = false;
- }
- return result;
- }
最后做个总结。C#作为高级语言,其强大的类库和第三方提供的类库。可以做很多事情。但也有弊端,用第三方类库性能不是很高。我压缩几百M的东西。cpu瞬间跑到50%多。比360压缩和zip压缩性能差远了。所以此类也就适用压缩比较小的东西。
完整例子下载地址
- C#中关于zip压缩解压帮助类的封装
- c#压缩解压帮助类
- .NET中zip的压缩和解压
- .NET中zip的压缩和解压
- C#压缩解压zip 文件
- C#压缩解压zip 文件
- C#压缩解压zip 文件
- C#操作zip压缩解压
- C# ZIP 压缩解压操作
- C#压缩解压zip 文件
- 压缩/解压封装的类
- c#中关于压缩数据流和解压数据流的实现
- C#多级文件夹及文件的zip压缩解压
- zip 的 压缩与解压
- SD卡中zip压缩包的解压
- .NET中zip的压缩和解压——SharpCompress
- C# 文件压缩与解压(ZIP格式)
- C# 文件压缩与解压(ZIP格式)
- 利用蜘蛛盘问qushi链接
- svn server搭建过程及注意事项
- 《深入理解计算机系统》家庭作业
- 22222343445
- 我们应该如何对待网站的死链
- C#中关于zip压缩解压帮助类的封装
- SAP HANA 资料合集!
- 765734554368578
- java 内部类
- 提高网站的收录率的方法
- SecureCRT空闲状态下,保持与服务器的连接
- ORACLE日期转换 操作
- 滑雪_poj_1088(记忆化搜索).java
- Appcelerator Titanium 3.x Win7 64位平台安装步骤