C#压缩指定的文件并生成zip文件
程序集需要添加的引用是:ICSharpCode.SharpZipLib.dll
使用实例:
//执行压缩 ClsZip.Zip(downZipPath, sourceTopPath, 1, "", allFilePath);//执行下载 ctrlDcAllGrxx.download(downZipPath, true);
压缩文件的类ClsZip:
public class ClsZip
{/// <summary>/// 生成压缩文件/// </summary>/// <param name="strZipPath">生成的zip文件的路径,包括文件名</param>/// <param name="strSourceTopDirectoryPath">要压缩的源文件的上级目录</param>/// <param name="intZipLevel">T压缩等级</param>/// <param name="strPassword">压缩包解压密码,不设密码""</param>/// <param name="filesOrDirectoriesPaths">源文件路径</param>/// <returns></returns> public static bool Zip(string strZipPath, string strSourceTopDirectoryPath, int intZipLevel, string strPassword,
List<string> filesOrDirectoriesPaths) {try { List<string> AllFilesPath = new List<string>();if (filesOrDirectoriesPaths.Count > 0) // get all files path {for (int i = 0; i < filesOrDirectoriesPaths.Count; i++) {if (File.Exists(filesOrDirectoriesPaths[i])) { AllFilesPath.Add(filesOrDirectoriesPaths[i]); }else if (Directory.Exists(filesOrDirectoriesPaths[i])) { GetDirectoryFiles(filesOrDirectoriesPaths[i], AllFilesPath); } } }if (AllFilesPath.Count > 0) { ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(strZipPath)); zipOutputStream.SetLevel(intZipLevel); zipOutputStream.Password = strPassword;for (int i = 0; i < AllFilesPath.Count; i++) {string strFile = AllFilesPath[i].ToString();try {if (strFile.Substring(strFile.Length - 1) == "") //folder {string strFileName = strFile.Replace(strSourceTopDirectoryPath, "");if (strFileName.StartsWith("")) { strFileName = strFileName.Substring(1); } ZipEntry entry = new ZipEntry(strFileName); entry.DateTime = DateTime.Now; zipOutputStream.PutNextEntry(entry); }else //file { FileStream fs = File.OpenRead(strFile);byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length);string strFileName = strFile.Replace(strSourceTopDirectoryPath, "");if (strFileName.StartsWith("")) { strFileName = strFileName.Substring(0); } ZipEntry entry = new ZipEntry(strFileName); entry.DateTime = DateTime.Now; zipOutputStream.PutNextEntry(entry); zipOutputStream.Write(buffer, 0, buffer.Length); fs.Close(); fs.Dispose(); } }catch {continue; } } zipOutputStream.Finish(); zipOutputStream.Close();return true; }else {return false; } }catch {return false; } }/// <summary>/// Gets the directory files./// </summary>/// <param name="strParentDirectoryPath">源文件路径</param>/// <param name="AllFilesPath">所有文件路径</param> public static void GetDirectoryFiles(string strParentDirectoryPath, List<string> AllFilesPath) {string[] files = Directory.GetFiles(strParentDirectoryPath);for (int i = 0; i < files.Length; i++) { AllFilesPath.Add(files[i]); }string[] directorys = Directory.GetDirectories(strParentDirectoryPath);for (int i = 0; i < directorys.Length; i++) { GetDirectoryFiles(directorys[i], AllFilesPath); }if (files.Length == 0 && directorys.Length == 0) //empty folder { AllFilesPath.Add(strParentDirectoryPath); } } }
参考:
ZipOutputStream类(1)
如果要完成一个文件或文件夹的压缩,则要使用ZipOutputStream类。ZipOutputStream是OutputStream的子类,常用操作方法如表所示。
ZipOutputStream类的常用方法
序号 |
方 法 |
类型 |
描 述 |
1 |
public ZipOutputStream (OutputStream out) |
构造 |
创建新的ZIP输出流 |
2 |
public void putNextEntry (ZipEntry e) throws IOException |
普通 |
设置每一个 ZipEntry对象 |
3 |
public void setComment (String comment) |
普通 |
设置ZIP文件的注释 |
转载于:https://www.cnblogs.com/kingangWang/archive/2012/02/07/2342023.html
C#压缩指定的文件并生成zip文件相关推荐
- python在内存中生成Zip文件!
python在内存中生成Zip文件! - 天真的好蓝啊 - 博客园 python在内存中生成Zip文件! import zipfile import StringIO class MemoryZipF ...
- python打开zip文件_Python操作Zip文件
Python操作Zip文件 需要使用到zipfile模块 读取Zip文件 随便一个zip文件,我这里用了bb.zip,就是一个文件夹bb,里面有个文件aa.txt. import zipfile # ...
- python3中的zip_Python3实现将文件归档到zip文件及从zip文件中读取数据的方法
''''' Created on Dec 24, 2012 将文件归档到zip文件,并从zip文件中读取数据 @author: liury_lab ''' # 压缩成zip文件 from zipfil ...
- 7z001怎么解压在安卓手机上面_手机怎么解压zip文件 安卓手机zip文件怎么打开?...
手机怎么解压zip文件 安卓手机zip文件怎么打开?Zip格式的文件是一种经过压缩的文件,通过压缩之后,文件的体积会变小,从而更有利于在网络上传播.经过压缩的文件,如果想要再次使用,就必须通过解压缩后 ...
- Python:pyinstaller如何将一个项目的.py和相关资源文件打包生成.exe文件
前言 写这篇博文真属于意外,因为小编不是以学术为目的的,而仅仅是以讨女朋友喜欢为目的,所以文中略有狗粮.撇开这些,相信大家找到为了找到一个可行方案已经寻寻觅觅良久,本文或许便是你的"真命天子 ...
- svn增量打包部署_利用svn的补丁文件打包生成增量文件
下面的代码是maven版本 1. 创建patch.txt增量文件 保存到 文件目录下 比如 E:\aa\patch.txt 2. 编写java代码 package utils; import java ...
- java流与文件——java生成解压缩文件(夹)
[0]README 0.1) 本文描述转自 http://blog.csdn.net/chenssy/article/details/9622171 , 旨在理解 java流与文件--java生成解压 ...
- Ubuntu 20.04.2.0 LTS 下Geany 1.36的“编译文件”和“生成当前文件”两个按键不可用
[问题描述] Ubuntu 20.04.2.0 LTS 下利用 sudo apt-get install geany 命令安装Geany 1.36后,利用Geany 1.36写C++语言程序后,会出现 ...
- [已解决]vscode环境 ros系统 msg文件不生成.h文件
vscode环境 ros系统 msg文件不生成.h文件 的解决方法 问题描述 问题原因 解决方式 问题描述 在没有用vscode编译器之前,通过修改Cmakelist文件能够正常生成msg文件的 同名 ...
- 旋转狗头:Python如何读取STL文件,生成STL文件预览图(缩略图)之进化,动态旋转Gif图
之前的文章<Python如何读取STL文件,生成STL文件预览图(缩略图)>实现了将STL文件读取加载.绘制保存为某个角度下2D的png图片,作为预览图(缩略图).但是3D转2D,预览就丢 ...
最新文章
- 查看终端进程是否死掉技巧
- 【 MATLAB 】nextpow2 函数用法之 Optimize FFT with Padding
- bookmark: partitioned tables in sql server 2005
- 实例:如何设计一款好的Metro UI应用
- Java反射机制在UI自动化测试中的一个应用
- 你的专业 VS 你妈口中你的专业
- VS2008编译boost库
- 研究机构:特斯拉Model 3是2月份最畅销电动汽车
- NOIP 2010 普及组解题报告
- 异常详细信息: System.Web.HttpException: 服务器太忙
- 服务器imm装系统,通过IMM With Remote Console为服务器安装操作系统
- Android+8.0+微信表情,微信8.0表情为什么不动?微信8.0哪些表情有动画效果?
- UIAccelerometer 加速计
- 李开复:未曾公布的乔布斯故事
- 无法打开虚拟机“Ubuntu”,ubuntu.vmx获得所有权失败
- Nginx 反向代理https
- 163邮箱接口post登录战网(一)
- [工具]-WIKI/文档编写相关软件
- 找出只包含1的最大矩形c语言,一些简单的编程练习题
- 金蝶K3数据库优化/备份操作指南
热门文章
- 安装pycuda的正确办法
- CUDA的内存泄露问题及解决办法
- Unknown run configuration type AndroidRunConfigurationType的解决办法
- 用C#代码实现求两条线段的交点并判断各种情况
- js的tree数组对象扁平化思否_JavaScript数组_二维数组_三维数组(二十五)
- kafka安装_kafka 安装部署教程
- 为什么字符串中的字符无效_JavaScript中的基本字符串与字符串对象的区别
- image.open()得到的图片是什么类型_复古门窗花格图片大全让精彩生活继续闪耀 「巴森」...
- 电脑卡顿不流畅是什么原因_为什么感觉电脑远不如手机流畅?
- JAVA获取各种日期时间