网络传输压缩DLL: ICSharpCode.SharpZipLib
来源:互联网 发布:prezi for mac 中文版 编辑:程序博客网 时间:2024/05/16 12:02
ICSharpCode.SharpZipLib
UnityGZip 压缩数据DLL,这个文章实在包含了在U3D中的应用以及源代码和Dll
http://www.icsharpcode.net/opensource/sharpziplib/ 有SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, GZip, BZip2 和Tar格式
好了,深入的大家还要多多研究,今天我们简单介绍一下 简单的 单文件、文件夹的压缩和解压
先给大家看一下效果:
一、引入ICSharpCode.SharpZipLib
我们新建个帮助类 ZipHelper.cs 然后 添加 dll 引用
二、添加完dll引用之后 我们 需要添加 这几个Using引用
1 using ICSharpCode.SharpZipLib.Checksums; 2 using ICSharpCode.SharpZipLib.Zip;3 using System;4 using System.IO;
三、压缩单个文件
这里我添加了几个参数 大家可以根据自己的需要 修改一下
/// ZIP:压缩文件夹 /// add yuangang by 2016-06-13 /// </summary> /// <param name="DirectoryToZip">需要压缩的文件夹(绝对路径)</param> /// <param name="ZipedPath">压缩后的文件路径(绝对路径)</param> /// <param name="ZipedFileName">压缩后的文件名称(文件名,默认 同源文件夹同名)</param> /// <param name="IsEncrypt">是否加密(默认 加密)</param> public static void ZipDirectory(string DirectoryToZip, string ZipedPath, string ZipedFileName = "", bool IsEncrypt = true) { //如果目录不存在,则报错 if (!System.IO.Directory.Exists(DirectoryToZip)) { throw new System.IO.FileNotFoundException("指定的目录: " + DirectoryToZip + " 不存在!"); } //文件名称(默认同源文件名称相同) string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + "\\" + new DirectoryInfo(DirectoryToZip).Name + ".zip" : ZipedPath + "\\" + ZipedFileName + ".zip"; using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName)) { using (ZipOutputStream s = new ZipOutputStream(ZipFile)) { if (IsEncrypt) { //压缩文件加密 s.Password = "123"; } ZipSetp(DirectoryToZip, s, ""); } } } /// <summary> /// 递归遍历目录 /// add yuangang by 2016-06-13 /// </summary> private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath) { if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar) { strDirectory += Path.DirectorySeparatorChar; } Crc32 crc = new Crc32(); string[] filenames = Directory.GetFileSystemEntries(strDirectory); foreach (string file in filenames)// 遍历所有的文件和目录 { if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件 { string pPath = parentPath; pPath += file.Substring(file.LastIndexOf("\\") + 1); pPath += "\\"; ZipSetp(file, s, pPath); } else // 否则直接压缩文件 { //打开压缩文件 using (FileStream fs = File.OpenRead(file)) { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1); ZipEntry entry = new ZipEntry(fileName); entry.DateTime = DateTime.Now; entry.Size = fs.Length; fs.Close(); crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; s.PutNextEntry(entry); s.Write(buffer, 0, buffer.Length); } } } }
四、压缩文件夹
1 /// <summary> 2 /// ZIP:压缩文件夹 3 /// add yuangang by 2016-06-13 4 /// </summary> 5 /// <param name="DirectoryToZip">需要压缩的文件夹(绝对路径)</param> 6 /// <param name="ZipedPath">压缩后的文件路径(绝对路径)</param> 7 /// <param name="ZipedFileName">压缩后的文件名称(文件名,默认 同源文件夹同名)</param> 8 /// <param name="IsEncrypt">是否加密(默认 加密)</param> 9 public static void ZipDirectory(string DirectoryToZip, string ZipedPath, string ZipedFileName = "", bool IsEncrypt = true)10 {11 //如果目录不存在,则报错12 if (!System.IO.Directory.Exists(DirectoryToZip))13 {14 throw new System.IO.FileNotFoundException("指定的目录: " + DirectoryToZip + " 不存在!");15 }16 17 //文件名称(默认同源文件名称相同)18 string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + "\\" + new DirectoryInfo(DirectoryToZip).Name + ".zip" : ZipedPath + "\\" + ZipedFileName + ".zip";19 20 using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName))21 {22 using (ZipOutputStream s = new ZipOutputStream(ZipFile))23 {24 if (IsEncrypt)25 {26 //压缩文件加密27 s.Password = “123”;28 }29 ZipSetp(DirectoryToZip, s, "");30 }31 }32 }33 /// <summary>34 /// 递归遍历目录35 /// add yuangang by 2016-06-1336 /// </summary>37 private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)38 {39 if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)40 {41 strDirectory += Path.DirectorySeparatorChar;42 }43 Crc32 crc = new Crc32();44 45 string[] filenames = Directory.GetFileSystemEntries(strDirectory);46 47 foreach (string file in filenames)// 遍历所有的文件和目录48 {49 50 if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件51 {52 string pPath = parentPath;53 pPath += file.Substring(file.LastIndexOf("\\") + 1);54 pPath += "\\";55 ZipSetp(file, s, pPath);56 }57 58 else // 否则直接压缩文件59 {60 //打开压缩文件61 using (FileStream fs = File.OpenRead(file))62 {63 64 byte[] buffer = new byte[fs.Length];65 fs.Read(buffer, 0, buffer.Length);66 67 string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1);68 ZipEntry entry = new ZipEntry(fileName);69 70 entry.DateTime = DateTime.Now;71 entry.Size = fs.Length;72 73 fs.Close();74 75 crc.Reset();76 crc.Update(buffer);77 78 entry.Crc = crc.Value;79 s.PutNextEntry(entry);80 81 s.Write(buffer, 0, buffer.Length);82 }83 }84 }85 }
五、解压一个ZIP文件
1 /// <summary> 2 /// ZIP:解压一个zip文件 3 /// add yuangang by 2016-06-13 4 /// </summary> 5 /// <param name="ZipFile">需要解压的Zip文件(绝对路径)</param> 6 /// <param name="TargetDirectory">解压到的目录</param> 7 /// <param name="Password">解压密码</param> 8 /// <param name="OverWrite">是否覆盖已存在的文件</param> 9 public static void UnZip(string ZipFile, string TargetDirectory, string Password, bool OverWrite = true)10 {11 //如果解压到的目录不存在,则报错12 if (!System.IO.Directory.Exists(TargetDirectory))13 {14 throw new System.IO.FileNotFoundException("指定的目录: " + TargetDirectory + " 不存在!");15 }16 //目录结尾17 if (!TargetDirectory.EndsWith("\\")) { TargetDirectory = TargetDirectory + "\\"; }18 19 using (ZipInputStream zipfiles = new ZipInputStream(File.OpenRead(ZipFile)))20 {21 zipfiles.Password = Password;22 ZipEntry theEntry;23 24 while ((theEntry = zipfiles.GetNextEntry()) != null)25 {26 string directoryName = "";27 string pathToZip = "";28 pathToZip = theEntry.Name;29 30 if (pathToZip != "")31 directoryName = Path.GetDirectoryName(pathToZip) + "\\";32 33 string fileName = Path.GetFileName(pathToZip);34 35 Directory.CreateDirectory(TargetDirectory + directoryName);36 37 if (fileName != "")38 {39 if ((File.Exists(TargetDirectory + directoryName + fileName) && OverWrite) || (!File.Exists(TargetDirectory + directoryName + fileName)))40 {41 using (FileStream streamWriter = File.Create(TargetDirectory + directoryName + fileName))42 {43 int size = 2048;44 byte[] data = new byte[2048];45 while (true)46 {47 size = zipfiles.Read(data, 0, data.Length);48 49 if (size > 0)50 streamWriter.Write(data, 0, size);51 else52 break;53 }54 streamWriter.Close();55 }56 }57 }58 }59 60 zipfiles.Close();61 }62 }
zhe屎
0 0
- 网络传输压缩DLL: ICSharpCode.SharpZipLib
- ICSharpCode.SharpZipLib压缩解压
- ICSharpCode.SharpZipLib.dll 使用方法
- ICSharpCode.SharpZipLib.dll实现压缩解压一个树形目录
- ICSharpCode.SharpZipLib.dll实现压缩解压一个树形目录
- ICSharpCode.SharpZipLib.dll实现压缩解压一个树形目录
- 通过ICSharpCode.SharpZipLib.dll压缩和解压ZIP文件
- 压缩解压目录和文件--ICSharpCode.SharpZipLib.dll 算法 (转)
- C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件
- .Net 下利用ICSharpCode.SharpZipLib.dll实现文件压缩、解压缩
- C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件
- C#+ICSharpCode.SharpZipLib.dll实现压缩解压缩功能
- C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件
- 压缩/解压(ICSharpCode.SharpZipLib 类库)
- ICSharpCode.SharpZipLib组件压缩.docx
- 用ICSharpCode.SharpZipLib进行压缩
- Unity3d 字符串压缩 ICSharpCode.SharpZipLib
- ICSharpCode.SharpZipLib实现压缩解压
- 欢迎使用CSDN-markdown编辑器
- Windows7与Window2008 64位IIS7上面DCOM配置Excel、Word等
- hrbust 1864人类希望——kokoⅠ【数学+快速幂】
- 无刷新上传
- React Native (五):上下拉刷新加载
- 网络传输压缩DLL: ICSharpCode.SharpZipLib
- MySQL事务实现方法 、数据库索引
- 我的lamda表达式的注意事项
- linux下高并发服务器实现
- ADO.Net存储过程transaction的使用事项
- 我的注意细节
- scrapy 爬虫入门(1)
- 引用类型和值类型
- iOS Runtime (二)