设为首页
收藏本站
设为首页
点击收藏
手机版
手机扫一扫访问
迪恩网络手机版
关注官方公众号
微信扫一扫关注
公众号
忘记密码?
QQ登录
微信登录
登陆
注册
门户
Portal
开源
问答
系统
工作
资讯
论坛
BBS
硬件
编程
漏洞
安全
虚拟化
问答
投稿
文章
帖子
用户
快速发帖
客服电话
点击联系客服
在线时间:8:00-16:00
客服电话
132-9538-2358
电子邮件
[email protected]
APP下载
迪恩网络APP
随时随地掌握行业动态
官方微信
扫描二维码
关注迪恩网络微信公众号
问题反馈
返回顶部
电脑基础
选购指南
故障排查
笔记本
电脑组装
电视盒子
平板电脑
电脑周边
Python
Java
Node.js
c#
Ruby
JSP
LUA
ASP.NET
VBScript
VB.NET
Perl
Erlang
Scala
Julia
JavaScript
VUE
JQuery
Node.js
TypeScript
AJAX
HTML
CSS
BootStrap
综合其他
经典问答
漏洞分析
CVE漏洞
安全教程
安全设置
杀毒防毒
病毒查杀
脚本攻防
入侵防御
工具使用
加密解密
手机安全
安全其他
Docker
网址转二维码
网址缩短
网址转二维码
网址转二维码
qrcode.16892.net
qrcode.55276.net
qrcode.devdocs.uk
qrcode.mlink.in
qrcode.ogeek.cn
qrcode.ostack.cn
qrcode.shenghuobao.net
qrcode.shenzhenjia.cn
qrcode.sqlite.in
qrcode.vigge.cn
qrcode.vigge.net
qrcode.vigges.net
qrcode.wujigu.com
qrcode.xstack.ru
tools.16892.net
tools.55276.net
tools.devdocs.uk
OStack程序员社区-中国程序员成长平台
›
门户
›
编程
›
ASP.NET
›
ASP.NET编程经验
asp.net压缩解压缩zip文件
原作者: [db:作者] 来自:
[db:来源]
收藏
邀请
using System; using System.IO; using System.Diagnostics; using Microsoft.Win32; using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.SharpZipLib.Zip; ///压缩、解压缩类 namespace DotNet.Utilities { public class SharpZip { public SharpZip() { } /// <summary> /// 压缩 /// </summary> /// <param name="filename"> 压缩后的文件名(包含物理路径)</param> /// <param name="directory">待压缩的文件夹(包含物理路径)</param> public static void PackFiles(string filename, string directory) { try { FastZip fz = new FastZip(); fz.CreateEmptyDirectories = true; fz.CreateZip(filename, directory, true, ""); fz = null; } catch (Exception) { throw; } } /// <summary> /// 解压缩 /// </summary> /// <param name="file">待解压文件名(包含物理路径)</param> /// <param name="dir"> 解压到哪个目录中(包含物理路径)</param> public static bool UnpackFiles(string file, string dir) { try { if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } ZipInputStream s = new ZipInputStream(File.OpenRead(file)); ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); if (directoryName != String.Empty) { Directory.CreateDirectory(dir + directoryName); } if (fileName != String.Empty) { FileStream streamWriter = File.Create(dir + 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(); return true; } catch (Exception) { throw; } } } public class ClassZip { #region 私有方法 /// <summary> /// 递归压缩文件夹方法 /// </summary> private static bool ZipFileDictory(string FolderToZip, ZipOutputStream s, string ParentFolderName) { bool res = true; string[] folders, filenames; ZipEntry entry = null; FileStream fs = null; Crc32 crc = new Crc32(); try { entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/")); s.PutNextEntry(entry); s.Flush(); filenames = Directory.GetFiles(FolderToZip); foreach (string file in filenames) { fs = File.OpenRead(file); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(file))); 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); } } catch { res = false; } finally { if (fs != null) { fs.Close(); fs = null; } if (entry != null) { entry = null; } GC.Collect(); GC.Collect(1); } folders = Directory.GetDirectories(FolderToZip); foreach (string folder in folders) { if (!ZipFileDictory(folder, s, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip)))) { return false; } } return res; } /// <summary> /// 压缩目录 /// </summary> /// <param name="FolderToZip">待压缩的文件夹,全路径格式</param> /// <param name="ZipedFile">压缩后的文件名,全路径格式</param> private static bool ZipFileDictory(string FolderToZip, string ZipedFile, int level) { bool res; if (!Directory.Exists(FolderToZip)) { return false; } ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile)); s.SetLevel(level); res = ZipFileDictory(FolderToZip, s, ""); s.Finish(); s.Close(); return res; } /// <summary> /// 压缩文件 /// </summary> /// <param name="FileToZip">要进行压缩的文件名</param> /// <param name="ZipedFile">压缩后生成的压缩文件名</param> private static bool ZipFile(string FileToZip, string ZipedFile, int level) { if (!File.Exists(FileToZip)) { throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!"); } FileStream ZipFile = null; ZipOutputStream ZipStream = null; ZipEntry ZipEntry = null; bool res = true; try { ZipFile = File.OpenRead(FileToZip); byte[] buffer = new byte[ZipFile.Length]; ZipFile.Read(buffer, 0, buffer.Length); ZipFile.Close(); ZipFile = File.Create(ZipedFile); ZipStream = new ZipOutputStream(ZipFile); ZipEntry = new ZipEntry(Path.GetFileName(FileToZip)); ZipStream.PutNextEntry(ZipEntry); ZipStream.SetLevel(level); ZipStream.Write(buffer, 0, buffer.Length); } catch { res = false; } finally { if (ZipEntry != null) { ZipEntry = null; } if (ZipStream != null) { ZipStream.Finish(); ZipStream.Close(); } if (ZipFile != null) { ZipFile.Close(); ZipFile = null; } GC.Collect(); GC.Collect(1); } return res; } #endregion /// <summary> /// 压缩 /// </summary> /// <param name="FileToZip">待压缩的文件目录</param> /// <param name="ZipedFile">生成的目标文件</param> /// <param name="level">6</param> public static bool Zip(String FileToZip, String ZipedFile, int level) { if (Directory.Exists(FileToZip)) { return ZipFileDictory(FileToZip, ZipedFile, level); } else if (File.Exists(FileToZip)) { return ZipFile(FileToZip, ZipedFile, level); } else { return false; } } /// <summary> /// 解压 /// </summary> /// <param name="FileToUpZip">待解压的文件</param> /// <param name="ZipedFolder">解压目标存放目录</param> public static void UnZip(string FileToUpZip, string ZipedFolder) { if (!File.Exists(FileToUpZip)) { return; } if (!Directory.Exists(ZipedFolder)) { Directory.CreateDirectory(ZipedFolder); } ZipInputStream s = null; ZipEntry theEntry = null; string fileName; FileStream streamWriter = null; try { s = new ZipInputStream(File.OpenRead(FileToUpZip)); while ((theEntry = s.GetNextEntry()) != null) { if (theEntry.Name != String.Empty) { fileName = Path.Combine(ZipedFolder, theEntry.Name); if (fileName.EndsWith("/") || fileName.EndsWith("\\")) { Directory.CreateDirectory(fileName); continue; } streamWriter = File.Create(fileName); 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; } } } } } finally { if (streamWriter != null) { streamWriter.Close(); streamWriter = null; } if (theEntry != null) { theEntry = null; } if (s != null) { s.Close(); s = null; } GC.Collect(); GC.Collect(1); } } } public class ZipHelper { #region 私有变量 String the_rar; RegistryKey the_Reg; Object the_Obj; String the_Info; ProcessStartInfo the_StartInfo; Process the_Process; #endregion /// <summary> /// 压缩 /// </summary> /// <param name="zipname">要解压的文件名</param> /// <param name="zippath">要压缩的文件目录</param> /// <param name="dirpath">初始目录</param> public void EnZip(string zipname, string zippath, string dirpath) { try { the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\Shell\Open\Command"); the_Obj = the_Reg.GetValue(""); the_rar = the_Obj.ToString(); the_Reg.Close(); the_rar = the_rar.Substring(1, the_rar.Length - 7); the_Info = " a " + zipname + " " + zippath; the_StartInfo = new ProcessStartInfo(); the_StartInfo.FileName = the_rar; the_StartInfo.Arguments = the_Info; the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden; the_StartInfo.WorkingDirectory = dirpath; the_Process = new Process(); the_Process.StartInfo = the_StartInfo; the_Process.Start(); } catch (Exception ex) { throw new Exception(ex.Message); } } /// <summary> /// 解压缩 /// </summary> /// <param name="zipname">要解压的文件名</param> /// <param name="zippath">要解压的文件路径</param> public void DeZip(string zipname, string zippath) { try { the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRar.exe\Shell\Open\Command"); the_Obj = the_Reg.GetValue(""); the_rar = the_Obj.ToString(); the_Reg.Close(); the_rar = the_rar.Substring(1, the_rar.Length - 7); the_Info = " X " + zipname + " " + zippath; the_StartInfo = new ProcessStartInfo(); the_StartInfo.FileName = the_rar; the_StartInfo.Arguments = the_Info; the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden; the_Process = new Process(); the_Process.StartInfo = the_StartInfo; the_Process.Start(); } catch (Exception ex) { throw new Exception(ex.Message); } } } }
鲜花
握手
雷人
路过
鸡蛋
该文章已有
0
人参与评论
请发表评论
评论
全部评论
专题
导读
More+
上一篇:
ASP.NET学习总结
发布时间:2022-07-10
下一篇:
HttpActionDescriptor,ASP.NETWebAPI又一个重要的描述对象HttpActionDescriptor,ASP ...
发布时间:2022-07-10
热门
推荐
More+
热门
话题
More+
阅读
排行榜
返回顶部
请发表评论