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,预览就丢 ...
最新文章
- 前端思想实现:面向UI编程_____前端框架设计开发
- EntityFramework 基础提供程序在 Open 上失败。
- MySQL 到底是怎么解决幻读的?
- 三台机器搭建MySQL一主二从
- ES10的动态导入文件
- 如何远程连接Windows和linux服务器
- 信息奥赛一本通(1413:确定进制)
- leetcode —— 866. 回文素数
- excel if in函数_Office教程:Excel函数AVERAGE与IF函数的组合标记平均值
- java重装机兵机甲咆哮_重装机兵之机甲咆哮流程攻略
- android audiorecord jni,Android AudioRecord初始化失败
- Java JSON中无分隔符日期字符串处理
- 几何画板中该如何插入公式
- 单片机原理与应用技术(四)———矩阵键盘汇编语言
- oppo(不root)手机查看万能钥匙破解的wifi密码实例
- 手把手教你微信第三方平台开发
- rain、rainy、raining的区别
- 漫画:5G 到底是个什么玩意儿?
- 使用U-Net 进行图像分割
- 数字图像处理之matlab常见函数
热门文章
- 管理感悟:计算缺陷的权重
- python中button中command_tkinter模块的button控件不点击也运行command
- 读取mysql表名称_JAVA动态读取mysql表的字段名索引
- 小写数字转大写_微软太坏了,这个函数居然被隐藏了,用它搞定数值转中文大小写...
- nsurl转nsdata_ios 中NSData 拼接成功,但写入本地种只有前面的data
- react 路径跳转组件不跳转_Taro 小程序开发大型实战(二):多页面跳转和 Taro UI 组件库...
- mysql 插入数据 自增长_mysql插入数据后返回自增ID的方法
- InitializeSecurityDescriptor、InitializeAcl、AddAccessAllowedAce、SetSecurityDescriptorDacl
- 3PHP如何用PDO的连接方式方式导出mysql数据
- 整理UWP中网络和设备信息获取的帮助类,需要的拿走。