- 浏览: 181903 次
- 性别:
- 来自: 广州
文章分类
最新评论
原文:
https://www.cnblogs.com/sun-shadow/p/9205510.html
但是原文中递归获取文件数据时有问题,做了小改动。
using ICSharpCode.SharpZipLib.Checksum;
using ICSharpCode.SharpZipLib.Zip;
https://www.cnblogs.com/sun-shadow/p/9205510.html
但是原文中递归获取文件数据时有问题,做了小改动。
using ICSharpCode.SharpZipLib.Checksum;
using ICSharpCode.SharpZipLib.Zip;
/// <summary> /// 所有文件缓存 /// </summary> private static List<string> files = new List<string>(); /// <summary> /// 所有空目录缓存 /// </summary> private static List<string> paths = new List<string>(); #region 压缩单个文件 /// <summary> /// 压缩单个文件 /// </summary> /// <param name="fileToZip">要压缩的文件</param> /// <param name="zipedFile">压缩后的文件全名</param> /// <param name="compressionLevel">压缩程度,范围0-9,数值越大,压缩程序越高</param> /// <param name="blockSize">分块大小</param> public static 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(); } #endregion #region 压缩目录所有文件(递归) /// <summary> /// 压缩目录(包括子目录及所有文件) /// </summary> /// <param name="rootPath">要压缩的根目录</param> /// <param name="destinationPath">保存路径</param> /// <param name="compressLevel">压缩程度,范围0-9,数值越大,压缩程序越高</param> public static 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 compression foreach (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; entry.Size = fileStream.Length; fileStream.Close(); crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; outPutStream.PutNextEntry(entry); outPutStream.Write(buffer, 0, buffer.Length); } files.Clear(); foreach (string emptyPath in paths) { ZipEntry entry = new ZipEntry(emptyPath.Replace(rootMark, string.Empty) + "/"); outPutStream.PutNextEntry(entry); } paths.Clear(); outPutStream.Finish(); outPutStream.Close(); GC.Collect(); } #endregion #region 获取目录下的所有文件及文件夹,分别存入files及paths /// <summary> /// 取得目录下所有文件及文件夹,分别存入files及paths /// </summary> /// <param name="rootPath">根目录</param> private static void GetAllDirectories(string rootPath) { foreach (string path in Directory.GetDirectories(rootPath)) { GetAllDirectories(path);//对每一个字目录做与根目录相同的操作:即找到子目录并将当前目录的文件名存入List } string[] filess = Directory.GetFiles(rootPath); foreach (string file in filess) { files.Add(file);//将当前目录中的所有文件全名存入文件List } if (Directory.GetDirectories(rootPath).Length == filess.Length && filess.Length == 0)//如果是空目录 { paths.Add(rootPath);//记录空目录 } } #endregion #region 解压缩文件(压缩文件中含有子目录) /// <summary> /// 解压缩文件(压缩文件中含有子目录) /// </summary> /// <param name="zipfilepath">待解压缩的文件路径</param> /// <param name="unzippath">解压缩到指定目录</param> /// <returns>解压后的文件列表</returns> public static 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) continue; //解压文件到指定的目录 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; } #endregion #region 获取压缩文件的文件类型.rar或.zip public static string GetZipFileExtention(string fileFullName) { int index = fileFullName.LastIndexOf("."); if (index <= 0) { throw new Exception("The source package file is not a compress file"); } //extension string string ext = fileFullName.Substring(index); if (ext == ".rar" || ext == ".zip") { return ext; } else { throw new Exception("The source package file is not a compress file"); } } #endregion
发表评论
-
枚举类型帮助类 EnumHelper
2019-09-02 11:29 480项目中出现的问题: 项目中会有很多地方用到文字提醒,如果在每个 ... -
使用ITextSharp对pdf文件打水印
2019-08-19 17:13 693在项目中需要对pdf文件中的部分信息进行遮挡隐藏,于是找到了I ... -
HMAC_SHA1加密
2019-08-16 10:36 912hmacsha1在很多签名计算中都很常用了,这里对两种可能返回 ... -
.NET/java Office组件神器——Aspose.Total 17.x/18.x/19.x破解版+破解补丁下载【转】
2019-07-29 15:22 2492先上大神的网站地址: [url] https://masuit ... -
通过UserAgent判断智能手机(设备,Android,IOS)【转】
2019-07-02 17:51 925///<summary> /// ... -
8000401a 因为配置标识不正确,系统无法开始服务器进程。请检查用户名和密码【转】
2019-06-12 17:36 2116服务器使用了word的类库,在使用过程中日志报标题错误 解决 ... -
C#(同步调用、异步调用、异步回调)--转
2019-06-05 12:04 646本文将主要通过“同步调用”、“异步调用”、“异步回调”三个示例 ... -
C# 利用word书签写入自定义数据
2019-05-14 18:08 586原文参考地址: https://www.cnblogs.com ... -
C# 项目中引用office组件后在服务器遇到的问题集锦
2019-02-13 16:03 8861、要在服务器安装相同版本的office完整版程序 本地测试机 ... -
C# word转pdf
2019-01-17 14:55 635写在最前面,如果要部署,服务器需要安装相同版本的office, ... -
C# 操作word
2019-01-17 14:49 471需要单独引入的是COM中的Microsoft Office 1 ... -
给上传的文件生成缩略图(图片和视频)
2018-11-28 17:47 814备注:里面用到的ffmpeg ... -
获取视频文件的截图
2018-11-28 17:36 638视频获取第一帧截图的方法: https://www.cnblo ... -
excel文件转为DataTable
2018-11-02 17:38 1519在人员初始化的时候,通常会用到excel编辑好,导入到数据库中 ... -
DataTable导出excel
2018-11-02 16:36 1115网上有很多关于导出的例子,这里讲一个利用NPOI导出excel ... -
未能加载文件或程序集,System.Net.Http或它的某一个依赖项。系统找不到指定的文件。
2018-10-30 17:21 4450具体的错误描述: 未能加载文件或程序集“System.Net. ... -
将app请求的简单json串转化为字典
2017-11-16 11:18 750通过Newtonsoft的DeserializeObject& ... -
WebAPI启用session功能
2017-11-16 11:04 1692第一步:简单建立两个类,以备修改WebApi的路由方式 第一 ... -
webapi记录请求和返回日志
2017-11-15 17:12 2038需求: 记录每个接口的请求参数及返回参数,方便以后回溯 fi ... -
超过了最大请求长度。System.Web.HttpException
2017-11-13 16:50 2356请在服务器端配置 方法一: 在通过WebSer ...
相关推荐
c#zip压缩与解压c#zip压缩与解压c#zip压缩与解压c#zip压缩与解压c#zip压缩与解压
DotNetZip是一个开源的免费类库,主要提供了快速操作zip文件的工具集,VB、C#任何.Net语言都可以通过它创建、解压缩zip文件。我使用该类库最主要的目的还是因为它可以创建带密码保护的压缩文件。 只有设置了zip....
C# 实现压缩解压缩文件或者在内存中实现解压缩压缩 示例已经帮助文档.
C#实现zip压缩和解压帮助类
C# zip文件解压缩 只能实现一级目录的解压缩
C#实现zip压缩,解压zip文件源代码,附加有注释,代码清晰易懂!
C#实现zip文件压缩解压 不多说,直接上代码;需要的免费拿取即可
从别的地方转载的,挺不错的!
本例使用C#实现了,对文件和文件夹的ZIP压缩,和对Zip文件的解压。
C#解压以及压缩Zip文件简单实例源码,bin目录下放有DLL
c#编写的解压缩ZIP文件代码。 应用时,需要将附带dll加到工程中。
c# 解压zip完整demo 带进度条
C#-文件压缩操作类(指定文件压缩、指定多文件压缩、压缩包解压)
用DotNet自带的命名空间Commpression类实现的,支持大于2G文件。测试通过,供参考吧。
C#实现winform压缩解压文件夹
使用ICSharpCode.SharpZipLib.dll实现C#解压缩文件。Bin目录下有支持不同.NET版本的ICSharpCode.SharpZipLib.dll
压缩解压缩ZIP文件。调用时添加ICSharpCode.SharpZipLib.dll的引用。
一个基于C#实现的数据信息ZIP压缩与解压包装类源码及例子程序
c#写的压缩和解压缩的源码包括例子,学习压缩存储技术或者项目打包可以参考的.支持大多数格式,包括zip,rar....
C# Zip 解压 压缩 库文件 在C#环境,Unity3d开发中常用的工具