博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
解压缩文件
阅读量:4583 次
发布时间:2019-06-09

本文共 7632 字,大约阅读时间需要 25 分钟。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;namespace CommonHelper{
  //引用的实体类 using ICShareCode.SharpZipLib; using ICShareCode.SharpZipLib.Zip; using ICShareCode.SharpZipLib.Checksums; public class ZipHelper { /// /// 压缩单个文件 /// /// 要压缩的文件 /// 压缩后的文件 /// 压缩等级 /// 每次写入大小 public static void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize) { if (File.Exists(fileToZip) == false) { throw new FileNotFoundException("指定压缩文件:"+fileToZip+" 不存在"); } using (FileStream ZipFile=File.Create(zipedFile)) { using (ZipOutputStream ZipStream=new ZipOutputStream(ZipFile)) { using (FileStream StreamToZip=new FileStream(fileToZip,FileMode.Open,FileAccess.Read)) { string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1); ZipEntity zipEntity = new ZipEntity(fileName); ZipStream.PutNextEntry(zipEntity); ZipStream.SetLevel(compressionLevel); byte[] buffer = new byte[blockSize]; int sizeRead = 0; try { do { sizeRead = StreamToZip.Read(buffer, 0, buffer.Length); ZipStream.Write(buffer, 0, sizeRead); } while (sizeRead > 0); } catch (Exception ex) { throw ex; } ZipStream.Finish(); ZipStream.Close(); } ZipStream.Finish(); ZipStream.Close(); } ZipFile.Close(); } } /// /// 压缩单个文件 /// /// 要压缩的文件名 /// 压缩后生成的压缩文件名 public static void ZipFile(string fileToZip, string zipedFile) { //如果没有找到则报错 if (File.Exists(fileToZip)==false) { throw new FileNotFoundException("指定压缩文件:" + fileToZip + " 不存在"); } using (FileStream fs=File.OpenRead(fileToZip)) { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); using (FileStream ZipFile=File.Create(zipedFile)) { using (ZipOutputStream ZipStream=new ZipOutputStream(ZipFile)) { string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1); ZipEntity zipEntry = new ZipEntity(fileName); ZipStream.PutNextEntry(zipEntry); ZipStream.SetLevel(5); ZipStream.Write(buffer, 0, buffer.Length); ZipStream.Finish(); ZipStream.Close(); } } } } /// /// 压缩多层目录 /// /// /// public static void ZipFileDirectory(string strDirectory, string zipedFile) { using (FileStream ZipFile=File.Create(zipedFile)) { using (ZipOutputStream s=new ZipOutputStream(ZipFile)) { ZipSetp(strDirectory, s, ""); } } } /// /// 递归遍历目录 /// /// The directory /// The ZipOutputStream Object /// The parent path public static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath) { if (strDirectory[strDirectory.Length-1]!=Path.DirectorySeparatorChar) { strDirectory += Path.DirectorySeparatorChar; } Crc32 crc = new Crc32(); string[] fileNames = Directory.GetFileSystemEntries(strDirectory); foreach (string file in fileNames) { if (Directory.Exists(file)) { string strPath = parentPath; strPath += file.Substring(file.LastIndexOf("\\") + 1); strPath += "\\"; ZipSetp(file, s, strPath); } else { using (FileStream fs=File.OpenRead(file)) { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1); ZipEntry entry = new ZipEntry(); entry.DateTime = DateTime.Now; entry.Size = fs.Length; fs.Close(); crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; s.PutNextEntry(entry); s.Write(buffer, 0, buffer.Length); } } } } /// /// 解压一个zip文件 /// eg:ZipHelper.UnZip("上传路径","保存路径","",true) /// /// The ziped file /// The STR directory /// zip 文件密码 /// 是否覆盖已存在的文件 public static void UnZip(string zipedFile, string strDirectory, string password, bool overWirte) { if (strDirectory=="") strDirectory = Directory.GetCurrentDirectory(); if (!strDirectory.EndsWith("\\")) strDirectory = strDirectory + "\\"; using (ZipOutputStream s=new ZipOutputStream(File.OpenRead(zipedFile))) { s.Password = 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)&& overWirte)|| (!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(); } } }}

 

转载于:https://www.cnblogs.com/xibianriluo/p/4843735.html

你可能感兴趣的文章
大数据量,海量数据 处理方法
查看>>
面向对象基础部分及模块
查看>>
less使用方法
查看>>
C#生成exe、dll版本号自动增加
查看>>
高效代码指泛型代替非泛型
查看>>
递归函数、匿名函数、内置函数
查看>>
第三周学习总结
查看>>
作业二:源程序版本管理软件和项目管理软件的优缺点
查看>>
jquery的DataTables插件的使用方法
查看>>
合并果子 2004年NOIP全国联赛普及组
查看>>
九度1457...
查看>>
重新开始学习javase_Exception
查看>>
排序命令sort
查看>>
Raspberry Pi开发之旅-同步时间
查看>>
Spinner的使用
查看>>
9. configparser 设置配置文件模块
查看>>
Servlet的生命周期
查看>>
统计文本单词个数,并个数大小按序排列 C#
查看>>
maven setting仓库镜像
查看>>
如何让HTML的编写更具结构性
查看>>