C#实现压缩文件及解压文件

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);}}}
}

C#实现压缩文件及解压文件相关推荐

  1. 【踩坑】Linux java中ftp下载文件,解压文件损坏,以及图片下载打开只显示下载路径的问题

    [踩坑]Linux java中ftp下载文件,解压文件损坏,以及图片下载打开只显示下载路径的问题 一. 问题重现 二. 问题解决思路 1. 确认是不是上传就导致数据出错了 2. 是不是平台问题 三. ...

  2. tar 打包文件与解压文件

    tar 命令打包文件与解压文件 打包文件 1.把/root目录下的test1整个打包为test1.tar文件,如下所示 [root@localhost ~]# pwd /root [root@loca ...

  3. LZMA压缩文件与解压文件

    Hello,我是KitStar. 以下文章整理的不对.还请见谅. Unity的Assetbundle是使用LZMA压缩算法压缩的,它是一个开源的类库,有C. C++.C#.JAVA的类库,Unity里 ...

  4. 使用java程序下载远程zip文件并解压文件( 带注释解释代码)

    带注释解释代码 package com.zcl.Test;import java.io.*; import java.net.HttpURLConnection; import java.net.So ...

  5. python打包zip文件_python 解压文件,合并文件 打包成zip格式文件 生成MD5值

    #!/usr/bin/env python #_*_encoding:utf-8 # 2018/05/29 #augustyang #2.0 ''' 解压文件,合并文件 打包成zip格式文件 生成MD ...

  6. Linux压缩文件与解压文件(*.zip)

    1.把/home目录下面的mydata目录压缩为mydata.zip zip -r mydata.zip mydata #压缩mydata目录 2.把/home目录下面的mydata.zip解压到my ...

  7. mac 压缩文件 or 解压文件

    一. zip命令 1. 压缩某文件的命令格式如下: zip [压缩后的文件名称] [被压缩的文件名称] 将11681005该文件夹压缩为picture.zip 文件,如图所示: 2. zip相关参数解 ...

  8. Linux下python如何解压rar文件,RAR解压文件

    默认在linux下我们不能解压压缩rar文件,那我们如何使用呢? 我们可以下载rarlinux安装包实现解压压缩后缀为rar的包 下载地址:https://www.rarlab.com/downloa ...

  9. 数据结构Java06【赫夫曼树、概述、原理分析、代码实现(数据压缩、创建编码表、解码、压缩文件、解压文件)】

    学习地址:[数据结构与算法基础-java版]                  

  10. 通过C#代码 压缩/解压文件

    通过引用一DLL(ICSharpCode.dll)可以实现所述功能... 一.压缩文件 using System; using ICSharpCode.SharpZipLib; using ICSha ...

最新文章

  1. 开源项目哪家强?Github年终各大排行榜超级盘点(内附开源项目学习资源)
  2. openstack的网络、子网、端口的关系
  3. javascript中神奇的(+)加操作符
  4. 【问链-区块链与生活】 第一课 你为什么又在熬夜?
  5. Python数据分析·读取CSV文件转为字典
  6. c#程序打包,同时把netframework也打包进去
  7. SpringMVC视图解析器(转)
  8. 如何快速搭建云原生企业级数据湖架构及实践分享
  9. 这位教授2 年一篇 Science,再获教科书级的重大发现
  10. layui弹框提示层:倒计时(layui-font-red颜色定义)
  11. Python爬虫实战 | (6) 爬取猫眼电影《海王》影评
  12. skiller v3 beta2 发布
  13. 联想y7000电脑未正确启动_联想y7000wifi突然不能用了是怎么回事
  14. 【C语言】案例十六:掷骰子(随机数)
  15. windows10家庭版修改中文用户名完美解决
  16. 微信小程序登录功能wx.login
  17. 端游与页游之战:微端网游突出重围
  18. Python:import与from import的理解
  19. Vue中使用vue-video-player视频播放器
  20. 极其简单的 使用IDEA 中 实现springboot 热部署 (spring boot devtools版)

热门文章

  1. java 接入apple pay_支付的那些套路(apple pay篇)
  2. Android cpu降频工具,免root安卓cpu降频软件-安卓cpu降频软件免root版下载-游戏大玩家...
  3. html中如何把两行合并单元格,怎么把表格上下两行合并单元格合并
  4. 使用 乐吾乐topology 遇到的问题解决方法汇总
  5. Ubuntu 数字小键盘不能用解决方法
  6. boostrap中lg,md,sm,xs分别对应的像素宽度
  7. 2014年domino学习小结
  8. python msp430_MSP430
  9. 《信息化项目文档模板十——系统用户操作手册模板》
  10. 记录一下unity 加载外部视频