C# 实现ZIP 压缩和解压
2014-03-23 17:44
429 查看
/// <summary> /// 创建ZIP压缩文件 /// </summary> /// <param name="filePath">压缩文件的路径</param> /// <param name="userName">用户文件名</param> public static void CreateZipFile(string filePath,string userName) { if (false ==File.Exists(filePath)) return; var zipFilePath = filePath.Replace("xml", "zip"); try { using (var s = new ZipOutputStream(File.Create(zipFilePath))) { s.SetLevel(1); // 压缩级别 0-9 s.Password = userName + Common.ZipPassWord; //Zip压缩文件密码 var buffer = new byte[Common.BufferSize]; //缓冲区大小 var entry = new ZipEntry(Path.GetFileName(filePath)) { DateTime = DateTime.Now }; s.PutNextEntry(entry); using (var fs = File.OpenRead(filePath)) { //double inputBytes = 0; int sourceBytes; do { sourceBytes = fs.Read(buffer, 0, buffer.Length); //inputBytes += sourceBytes; //if (PrecentValueChange != null) // PrecentValueChange(inputBytes/fs.Length); s.Write(buffer, 0, sourceBytes); } while (sourceBytes > 0); } s.Finish(); s.Close(); if (File.Exists(filePath)) File.Delete(filePath); } } catch (Exception ex) { Console.WriteLine(ex); } } /// <summary> /// 加压指定ZIP文件 /// </summary> /// <param name="zipFilePath">ZIP文件的路径</param> /// <param name="putDocuemnt">解压后文件的位置</param> /// <param name="userName">用户文件名</param> public static void UnZipFile(string zipFilePath,string putDocuemnt,string userName) { if (false == File.Exists(zipFilePath)) return; var fastZip = new FastZip { Password = userName + Common.ZipPassWord, RestoreDateTimeOnExtract = false, RestoreAttributesOnExtract = false }; try { fastZip.ExtractZip(zipFilePath, putDocuemnt, null); } catch (Exception ex) { Console.WriteLine(ex); MessageNote.DoNote("没有权限访问该文件"); } }
相关文章推荐
- C#实现Zip压缩解压实例【转】
- C#实现Zip压缩解压实例
- C#实现zip压缩和解压
- C#实现Zip压缩解压实例
- C#实现文件夹压缩到zip以及解压
- C#实现Zip压缩解压实例
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
- C#实现Zip压缩解压实例
- Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)
- JAVA实现实用的ZIP压缩与解压
- 使用org.apache.tools.zip实现zip压缩和解压
- JAVA实现实用的ZIP压缩与解压
- JAVA API实现ZIP压缩与解压
- android自带zip轻松实现压缩解压
- 使用org.apache.tools.zip实现zip压缩和解压
- 如何在 C# 中用 SharpZipLib 进行 ZIP 压缩与解压
- WinRAR(WinZip)压缩与解压实现(C#版Window平台)
- Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)
- 使用org.apache.tools.zip实现zip压缩和解压
- 用java代码实现文件的zip压缩与解压