asp.net C#压缩打包文件例子
/// <summary> /// 压缩和解压文件 /// </summary> public class ZipClass {/// <summary>/// 所有文件缓存/// </summary>List<string> files = new List<string>();/// <summary>/// 所有空目录缓存/// </summary>List<string> paths = new List<string>();/// <summary>/// 压缩单个文件/// </summary>/// <param name="fileToZip">要压缩的文件</param>/// <param name="zipedFile">压缩后的文件全名</param>/// <param name="compressionLevel">压缩程度,范围0-9,数值越大,压缩程序越高</param>/// <param name="blockSize">分块大小</param>public void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize){if (!System.IO.File.Exists(fileToZip))//如果文件没有找到,则报错 {throw new FileNotFoundException("The specified file " + fileToZip + " could not be found. Zipping aborderd");}FileStream streamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read);FileStream zipFile = File.Create(zipedFile);ZipOutputStream zipStream = new ZipOutputStream(zipFile);ZipEntry zipEntry = new ZipEntry(fileToZip);zipStream.PutNextEntry(zipEntry);zipStream.SetLevel(compressionLevel);byte[] buffer = new byte[blockSize];int size = streamToZip.Read(buffer, 0, buffer.Length);zipStream.Write(buffer, 0, size);try{while (size < streamToZip.Length){int sizeRead = streamToZip.Read(buffer, 0, buffer.Length);zipStream.Write(buffer, 0, sizeRead);size += sizeRead;}}catch (Exception ex){GC.Collect();throw ex;}zipStream.Finish();zipStream.Close();streamToZip.Close();GC.Collect();}/// <summary>/// 压缩目录(包括子目录及所有文件)/// </summary>/// <param name="rootPath">要压缩的根目录</param>/// <param name="destinationPath">保存路径</param>/// <param name="compressLevel">压缩程度,范围0-9,数值越大,压缩程序越高</param>public void ZipFileFromDirectory(string rootPath, string destinationPath, int compressLevel){GetAllDirectories(rootPath);/* while (rootPath.LastIndexOf("\\") + 1 == rootPath.Length)//检查路径是否以"\"结尾{rootPath = rootPath.Substring(0, rootPath.Length - 1);//如果是则去掉末尾的"\"}*///string rootMark = rootPath.Substring(0, rootPath.LastIndexOf("\\") + 1);//得到当前路径的位置,以备压缩时将所压缩内容转变成相对路径。string rootMark = rootPath + "\\";//得到当前路径的位置,以备压缩时将所压缩内容转变成相对路径。Crc32 crc = new Crc32();ZipOutputStream outPutStream = new ZipOutputStream(File.Create(destinationPath));outPutStream.SetLevel(compressLevel); // 0 - store only to 9 - means best compressionforeach (string file in files){FileStream fileStream = File.OpenRead(file);//打开压缩文件byte[] buffer = new byte[fileStream.Length];fileStream.Read(buffer, 0, buffer.Length);ZipEntry entry = new ZipEntry(file.Replace(rootMark, string.Empty));entry.DateTime = DateTime.Now;// set Size and the crc, because the information// about the size and crc should be stored in the header// if it is not set it is automatically written in the footer.// (in this case size == crc == -1 in the header)// Some ZIP programs have problems with zip files that don't store// the size and crc in the header.entry.Size = fileStream.Length;fileStream.Close();crc.Reset();crc.Update(buffer);entry.Crc = crc.Value;outPutStream.PutNextEntry(entry);outPutStream.Write(buffer, 0, buffer.Length);}this.files.Clear();foreach (string emptyPath in paths){ZipEntry entry = new ZipEntry(emptyPath.Replace(rootMark, string.Empty) + "/");outPutStream.PutNextEntry(entry);}this.paths.Clear();outPutStream.Finish();outPutStream.Close();GC.Collect();}/// <summary>/// 取得目录下所有文件及文件夹,分别存入files及paths/// </summary>/// <param name="rootPath">根目录</param>private void GetAllDirectories(string rootPath){string[] subPaths = Directory.GetDirectories(rootPath);//得到所有子目录foreach (string path in subPaths){GetAllDirectories(path);//对每一个字目录做与根目录相同的操作:即找到子目录并将当前目录的文件名存入List }string[] files = Directory.GetFiles(rootPath);foreach (string file in files){this.files.Add(file);//将当前目录中的所有文件全名存入文件List }if (subPaths.Length == files.Length && files.Length == 0)//如果是空目录 {this.paths.Add(rootPath);//记录空目录 }}/// <summary>/// 解压缩文件(压缩文件中含有子目录)/// </summary>/// <param name="zipfilepath">待解压缩的文件路径</param>/// <param name="unzippath">解压缩到指定目录</param>/// <returns>解压后的文件列表</returns>public List<string> UnZip(string zipfilepath, string unzippath){//解压出来的文件列表List<string> unzipFiles = new List<string>();//检查输出目录是否以“\\”结尾if (unzippath.EndsWith("\\") == false || unzippath.EndsWith(":\\") == false){unzippath += "\\";}ZipInputStream s = new ZipInputStream(File.OpenRead(zipfilepath));ZipEntry theEntry;while ((theEntry = s.GetNextEntry()) != null){string directoryName = Path.GetDirectoryName(unzippath);string fileName = Path.GetFileName(theEntry.Name);//生成解压目录【用户解压到硬盘根目录时,不需要创建】if (!string.IsNullOrEmpty(directoryName)){Directory.CreateDirectory(directoryName);}if (fileName != String.Empty){//如果文件的压缩后大小为0那么说明这个文件是空的,因此不需要进行读出写入if (theEntry.CompressedSize == 0)break;//解压文件到指定的目录directoryName = Path.GetDirectoryName(unzippath + theEntry.Name);//建立下面的目录和子目录 Directory.CreateDirectory(directoryName);//记录导出的文件unzipFiles.Add(unzippath + theEntry.Name);FileStream streamWriter = File.Create(unzippath + 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;}}streamWriter.Close();}}s.Close();GC.Collect();return unzipFiles;} }
asp.net C#压缩打包文件例子相关推荐
- php ziparchive 压缩文件,php使用ZipArchive压缩打包文件
有时候需要在服务器端打包文件,可以使用php的ZipArchive相关类实现. 有时候需要在服务器端打包文件,可以使用php的ZipArchive相关类实现. $error = "" ...
- 记一次vue压缩打包文件及解压zip文件
一.压缩--使用archiver 选择archiver 是因为它能根据目录压缩整个文件夹,npm install archiver,js内容如下: const fs = require('fs');c ...
- ubuntu18.04 Linux包文件解压和安装,文件夹压缩打包
目录 一.bz2 二.zip和unzip 三.tar .tar.gz tar.xz .tgz 四.deb 五.7z [无法输入中文]Ubuntu18.04中使用中文输入法_Linux教程_云网牛站 - ...
- php 下载的压缩文件,php在线压缩打包rar并自动下载文件的例子
php在线压缩打包rar并自动下载文件是需要基于ZipArchive了, linux需开启zlib了,下面我们就一起来看看了,希望例子能够帮助到各位朋友. linux需开启zlib.下面是具体的开启方 ...
- asp.net使用httphandler打包多CSS或JS文件以加快页面加载速度
介绍 使用许多小得JS.CSS文件代替一个庞大的JS或CSS文件来让代码获得更好的可维护性,这是一个很好的实践.但这样做反过来却损失了网站的性能.虽然你应该将你的Javascript代码写在小文件中并 ...
- 压缩命令_Linux环境下文件压缩打包命令详解
你好,我是goldsunC 让我们一起进步吧! 前言 我们知道,在面向对象的程序设计中,一切皆对象.而在Linux操作系统中,一切皆文件,因此我们总会跟文件打交道. Linux文件系统很庞大复杂,不过 ...
- Unity 工具类 之 WWW/UnityWebRequest 下载压缩文件(zip),解压到本地且加载使用解压数据的简单案例(内也含压缩文件例子)
Unity 工具类 之 WWW/UnityWebRequest 网络下载压缩文件(zip),解压到本地,且加载使用解压数据的简单案例(内也含压缩文件例子) 目录 Unity 工具类 之 WWW/Uni ...
- linux压缩指定时间的文件,Linux下压缩某个文件夹(文件夹打包)
tar -zcvf /home/xahot.tar.gz /xahot tar -zcvf 打包后生成的文件名全路径 要打包的目录 例子:把/xahot文件夹打包后生成一个/home/xahot.ta ...
- asp.net利用RAR实现文件压缩解压缩【月儿原创】
asp.net利用RAR实现文件压缩解压缩 作者:清清月儿 主页:http://blog.csdn.net/21aspnet/ 时间:2007.6.13 如果服务器上安装了RAR程 ...
最新文章
- ElasticSearch Java Api(三) -更新索引库
- 通过反射创建动态代理对象(二)
- 【bzoj1026】[SCOI2009]windy数 数位dp
- Nexus-vPC与FHRP
- SQL Server数据库优化方案
- html滚动字幕如何向下移动,按向下键的同时,菜单选项向下移动,浏览器右边的滚动条也跟着跑怎么办。这个bug怎么改...
- ps图像压缩插件:TinyPNG and TinyJPG for Mac 支持ps2021
- 在网上找来的几个大牛,忽然觉得自己来参加ACM倒也不是一件错误的事情,梦想总是要有的,万一成了呢。
- Oracle掌管权限和角色
- autocad不能画图_学了这50条CAD技巧,画图速度提10倍!
- 91卫图助手给我的帮助
- 升为领导十大忌 职场百悟大之六
- 高级前端面试100问(必会)
- Springboot 集成 Camunda
- 如何重启Windows资源管理器
- ArcGis Server10.2 授权文件教程
- 由C注释向C++注释转换简单实现
- 蒟蒻の算法题(~~完全不会~~的期望)01
- [02]从零开始学电子技术丛书-自学电子的同学看过来
- 免费使用腾讯云每天定时签到京东领取京豆
热门文章
- 猴子会照镜子吗?科学家的这一研究意义非凡!
- 36小时,造一个亚马逊无人商店 | 实战教程+代码
- 只有程序员才懂的手势 | 每日趣闻
- 如何用一句话证明你是程序员?41 个答案揭晓!
- 王者荣耀活动精选 Blink 第三弹来袭!
- 学完 CompletionService,可以做时间管理大师?
- 使用Xcode External Build System实现Rust 项目 Capture GPU Frame 在线调试 Metal 2018.12.18
- 【算法】论平衡二叉树(AVL)的正确种植方法
- HDU 6015 Skip the Class
- Apache Camel 2.15.0 发布,Java 规则引擎