C# ICSharpCode.SharpZipLib.Zip 的使用
public static class ZipFileHelper{#region 加压解压方法/// <summary> /// 功能:压缩文件(暂时只压缩文件夹下一级目录中的文件,文件夹及其子级被忽略) /// </summary> /// <param name="dirPath">被压缩的文件夹夹路径</param> /// <param name="zipFilePath">生成压缩文件的路径,为空则默认与被压缩文件夹同一级目录,名称为:文件夹名+.zip</param> /// <param name="err">出错信息</param> /// <returns>是否压缩成功</returns> public static bool ZipFile(string dirPath, string zipFilePath, out string err){err = "";if (dirPath == string.Empty){err = "要压缩的文件夹不能为空!";return false;}if (!Directory.Exists(dirPath)){err = "要压缩的文件夹不存在!";return false;}//压缩文件名为空时使用文件夹名+.zip if (zipFilePath == string.Empty){if (dirPath.EndsWith("\\")){dirPath = dirPath.Substring(0, dirPath.Length - 1);}zipFilePath = dirPath + ".zip";}try{string[] filenames = Directory.GetFiles(dirPath);using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath))){s.SetLevel(9);byte[] buffer = new byte[4096];foreach (string file in filenames){ZipEntry entry = new ZipEntry(Path.GetFileName(file));entry.DateTime = DateTime.Now;s.PutNextEntry(entry);using (FileStream fs = File.OpenRead(file)){int sourceBytes;do{sourceBytes = fs.Read(buffer, 0, buffer.Length);s.Write(buffer, 0, sourceBytes);} while (sourceBytes > 0);}}s.Finish();s.Close();}}catch (Exception ex){err = ex.Message;return false;}return true;}/// <summary> /// 功能:解压zip格式的文件。 /// </summary> /// <param name="zipFilePath">压缩文件路径</param> /// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param> /// <param name="err">出错信息</param> /// <returns>解压是否成功</returns> public static bool UnZipFile(string zipFilePath, string unZipDir, out string err){err = "";if (zipFilePath == string.Empty){err = "压缩文件不能为空!";return false;}if (!File.Exists(zipFilePath)){err = "压缩文件不存在!";return false;}//解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹 if (unZipDir == string.Empty)unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));if (!unZipDir.EndsWith("\\"))unZipDir += "\\";if (!Directory.Exists(unZipDir))Directory.CreateDirectory(unZipDir);try{using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath))){ZipEntry theEntry;while ((theEntry = s.GetNextEntry()) != null){string directoryName = Path.GetDirectoryName(theEntry.Name);string fileName = Path.GetFileName(theEntry.Name);if (directoryName.Length > 0){Directory.CreateDirectory(unZipDir + directoryName);}if (!directoryName.EndsWith("\\"))directoryName += "\\";if (fileName != String.Empty){using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name)){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;}}}}}//while }}catch (Exception ex){err = ex.Message;return false;}return true;}//解压结束 #endregion}
转载于:https://www.cnblogs.com/GoCircle/p/6429848.html
C# ICSharpCode.SharpZipLib.Zip 的使用相关推荐
- C#利用ICSharpCode.SharpZipLib.dll压缩文件和解压文件
/// <summary> /// 压缩文件 /// </summary> using System; using System.IO; using ICSharpCode ...
- C# ZipHelper C#公共类 -- ICSharpCode.SharpZipLib.dll实现压缩和解压
关于本文档的说明 本文档基于ICSharpCode.SharpZipLib.dll的封装,常用的解压和压缩方法都已经涵盖在内,都是经过项目实战积累下来的 1.基本介绍 由于项目中需要用到各种压缩将文件 ...
- ICSharpCode.SharpZipLib 开源压缩库使用示例
官方网站:http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx 插件描述: ICSharpCode.SharpZipLib.dl ...
- ICSharpCode.SharpZipLib 压缩、解压文件 附源码
http://www.icsharpcode.net/opensource/sharpziplib/ 有SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, GZip, ...
- ICSharpCode.SharpZipLib压缩解压
一.使用ICSharpCode.SharpZipLib.dll: 下载地址 http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.asp ...
- ICSharpCode.SharpZipLib 压缩、解压文件
http://www.icsharpcode.net/opensource/sharpziplib/ 有SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, GZip, ...
- ICSharpCode.SharpZipLib.dll,MyZip.dll,Ionic.Zip.dll 使用
MyZip.dll : 有BUG,会把子目录的文件解压到根目录.. ICSharpCode.SharpZipLib.dll: 把ICSharpCode.SharpZipLib.dll复制一份,重命名为 ...
- zip (ICSharpCode.SharpZipLib.dll文件需要下载)
ZipClass zc=new ZipClass (); zc.ZipDir(@"E:\1\新建文件夹", @"E:\1\新建文件夹.zip", 1);//压缩 ...
- C#文件压缩:ICSharpCode.SharpZipLib生成zip、tar、tar.gz
原文地址:https://blog.csdn.net/nihao198503/article/details/9204115 将代码原封不动的copy过来,只是因为有关tar的文章太少,大多都是zip ...
最新文章
- 利用边缘检测计算物体面积(内含源码)
- python写用用户名密码程序_Python创建用户名和密码程序
- 服务器中毒 HTML.IFrame.laka
- java面向字符的输入流_详细解读Java编程中面向字符的输入流
- 第十二题:设int x=1,float y=2,则表达式x/y的值是:
- adsl 路由器默认密码
- 个人知识体系思维导图_“知识体系”打得好,学霸孩子跑不了,巧用“思维导图”来帮忙...
- 量子计算机 程序,量子计算机程序 会早于量子计算机出现
- C# List集合求交集
- 腾讯的KDD competition
- 水系图一般在哪里找得到_雨水排水系统施工及设备要点详解!
- 弃用个人博客站重返CSDN缘由
- ABSOLUTE评估肿瘤纯度
- 【前端作业系列】HTML基础点 , 训练<有序列表><无序列表>(2022年6月15日作业)
- 《回炉重造》——泛型
- vue+element自动计算天数
- 微信公众账号分类入门知识
- 解决iPhone无法连接iTunes
- Cesium粒子系统、火焰粒子、喷水粒子
- PS学习笔记 day1
热门文章
- mvc ---- ajax 提交过来的Json格式如何处理(解析)
- Wordpress: contact form 7 表单内容同行
- Git CMD - push: Update remote refs along with associated objects
- 滴滴专车——司机提现流程
- Android菜单详解——理解android中的Menu
- Oracle创建用户、表空间、导入导出、...命令
- 岗位提成系数怎么算_2019年春节长假坚守岗位,你的加班工资怎么算?
- fs_struct和file_struct关系
- cocos2d-x 输出debug信息
- js网页如何获取手机屏幕宽度