博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用...
阅读量:7062 次
发布时间:2019-06-28

本文共 3724 字,大约阅读时间需要 12 分钟。

工作中我们很多时候需要进行对文件进行压缩,比较通用的压缩的dll就是ICSharpCode.SharpZipLib.dll,废话不多了,网上也有很多的资料,我将其最常用的两个函数整理了一下,提供了一个通用的类,这样在工作中可以快速的完成压缩和解压缩的动作哦

官网下载地址:   

 

1. 在项目中添加对ICSharpCode.SharpZipLib.dll的引用;

 

2. 在需要使用到ICSharpCode.SharpZipLib中定义的类的编码界面中将其导入(Imports)

 

1 using ICSharpCode.SharpZipLib.Zip; 2 using System; 3 using System.IO; 4  5 namespace ZTO.WayBill.Utilities 6 { 7     ///  8     /// 压缩类 9     /// http://www.cnblogs.com/kissdodog/p/3525295.html 10 11     /// 12     public class ZipHelper13     {14         /// 15         /// 压缩文件夹16         /// 17         /// 源目录18         /// ZipOutputStream对象19         public static void Compress(string source, ZipOutputStream s)20         {21             string[] filenames = Directory.GetFileSystemEntries(source);22             foreach (string file in filenames)23             {24                 if (Directory.Exists(file))25                 {26                     // 递归压缩子文件夹27                     Compress(file, s);28                 }29                 else30                 {31                     using (FileStream fs = File.OpenRead(file))32                     {33                         byte[] buffer = new byte[4 * 1024];34                         // 此处去掉盘符,如D:\123\1.txt 去掉D:35                         ZipEntry entry = new ZipEntry(file.Replace(Path.GetPathRoot(file), ""));36                         entry.DateTime = DateTime.Now;37                         s.PutNextEntry(entry);38                         int sourceBytes;39                         do40                         {41                             sourceBytes = fs.Read(buffer, 0, buffer.Length);42                             s.Write(buffer, 0, sourceBytes);43                         } while (sourceBytes > 0);44                     }45                 }46             }47         }48 49         /// 50         /// 解压缩51         /// 52         /// 压缩包完整路径地址53         /// 解压路径是哪里54         /// 
55 public static bool Decompress(string sourceFile, string targetPath)56 {57 if (!File.Exists(sourceFile))58 {59 throw new FileNotFoundException(string.Format("未能找到文件 '{0}' ", sourceFile));60 }61 if (!Directory.Exists(targetPath))62 {63 Directory.CreateDirectory(targetPath);64 }65 using (var s = new ZipInputStream(File.OpenRead(sourceFile)))66 {67 ZipEntry theEntry;68 while ((theEntry = s.GetNextEntry()) != null)69 {70 if (theEntry.IsDirectory)71 {72 continue;73 }74 string directorName = Path.Combine(targetPath, Path.GetDirectoryName(theEntry.Name));75 string fileName = Path.Combine(directorName, Path.GetFileName(theEntry.Name));76 if (!Directory.Exists(directorName))77 {78 Directory.CreateDirectory(directorName);79 }80 if (!String.IsNullOrEmpty(fileName))81 {82 using (FileStream streamWriter = File.Create(fileName))83 {84 int size = 4096;85 byte[] data = new byte[size];86 while (size > 0)87 {88 streamWriter.Write(data, 0, size);89 size = s.Read(data, 0, data.Length);90 }91 }92 }93 }94 }95 return true;96 }97 }98 }

 

你可能感兴趣的文章
[转]虚拟机下Ubuntu共享主机文件(Ubuntu、VMware、共享)
查看>>
高血压 治疗 偏方
查看>>
HtmlAttribute HTML属性处理类
查看>>
[书目20130316]jQuery UI开发指南
查看>>
Sql Server系列:开发存储过程
查看>>
Find INTCOL#=1001 in col_usage$?
查看>>
AutoCAD 命令统计魔幻球的实现过程--(3)
查看>>
dp学习笔记1
查看>>
newlisp debugger
查看>>
Java进阶02 异常处理
查看>>
图文介绍openLDAP在windows上的安装配置
查看>>
Heritrix 3.1.0 源码解析(十二)
查看>>
java 动态代理
查看>>
微信5.0绑定银行卡教程
查看>>
数字转换为壹仟贰佰叁拾肆的Java方法
查看>>
引发网页布局灾难的7个大错误
查看>>
一个表单对应多个提交按钮,每个提交按钮对应不同的行为
查看>>
tomcat集群时统计session与在线人数
查看>>
Android程序完全退出
查看>>
【Linux】目录权限与文件权限
查看>>