c#解压文件的实例方法
时间:2022-04-25 09:49:40
#region 解压 文件 zip 格式 rar 格式
/// <summary>
///解压文件
/// </summary>
/// <param name="fileFromUnZip">解压前的文件路径(绝对路径)</param>
/// <param name="fileToUnZip">解压后的文件目录(绝对路径)</param>
public static void UnpackFile(string fileFromUnZip, string fileToUnZip)
{
//获取压缩类型
string unType = fileFromUnZip.Substring(fileFromUnZip.LastIndexOf(".") + 1, 3).ToLower();
switch (unType)
{
case "rar":
UnRar(fileFromUnZip, fileToUnZip);
break;
case "zip":
UnZip(fileFromUnZip, fileToUnZip);
break;
}
}
//解压rar格式的文件
private static void UnRar(string fileFromUnZip, string fileToUnZip)
{
using (Process Process1 = new Process())// 开启一个进程 执行解压工作
{
string ServerDir = ConfigurationManager.AppSettings["UnpackFile"].ToString();//rar工具的安装路径 必须要安装 WinRAR //例于:C:\Program Files (x86)\WinRAR\RAR.exe
Process1.StartInfo.UseShellExecute = false;
Process1.StartInfo.RedirectStandardInput = true;
Process1.StartInfo.RedirectStandardOutput = true;
Process1.StartInfo.RedirectStandardError = true;
Process1.StartInfo.CreateNoWindow = true;
Process1.StartInfo.FileName = ServerDir;
Process1.StartInfo.Arguments = " x -inul -y " + fileFromUnZip + " " + fileToUnZip;
Process1.Start();//解压开始
Process1.WaitForExit();
Process1.Close();
}
}
// 解压zip 文件
public static void UnZip(string fileFromUnZip, string fileToUnZip)
{
ZipInputStream inputStream = new ZipInputStream(File.OpenRead(fileFromUnZip));
ZipEntry theEntry;
while ((theEntry = inputStream.GetNextEntry()) != null)
{
fileToUnZip += "/";
string fileName = Path.GetFileName(theEntry.Name);
string path = Path.GetDirectoryName(fileToUnZip) + "/";
// Directory.CreateDirectory(path);//生成解压目录
if (fileName != String.Empty)
{
FileStream streamWriter = File.Create(path + fileName);//解压文件到指定的目录
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = inputStream.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
streamWriter.Close();
}
}
inputStream.Close();
}
#endregion
标签:解压,文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#创建Excel多级分组的方法
2022-03-21 15:20:34
![](https://img.aspxhome.com/file/2023/5/81045_0s.jpg)
Java语法糖之个数可变的形参的实现
2023-12-05 00:13:20
WinForm调用jar包的方法分析
2023-11-17 04:09:46
![](https://img.aspxhome.com/file/2023/9/94589_0s.png)
Java监听器ActionListener与MouseListener的执行顺序说明
2022-02-04 20:08:23
![](https://img.aspxhome.com/file/2023/9/72629_0s.png)
.NET企业级项目中遇到的国际化问题和解决方法
2022-12-03 08:18:06
基于Java ActiveMQ的实例讲解
2023-08-24 08:39:38
解决java文件流处理异常 mark/reset not supported问题
2022-10-05 14:28:08
![](https://img.aspxhome.com/file/2023/8/81228_0s.jpg)
详解Java中用于查找对象哈希码值的hashCode()函数
2023-09-17 02:52:19
通过实例了解Java 8创建Stream流的5种方法
2023-04-13 22:41:02
![](https://img.aspxhome.com/file/2023/8/77398_0s.jpg)
Java几个实例带你进阶升华下篇
2021-06-11 01:19:46
Java shiro安全框架使用介绍
2023-07-09 05:24:35
![](https://img.aspxhome.com/file/2023/3/69653_0s.png)
Java 8 中 Map 骚操作之 merge() 的使用方法
2022-03-31 01:19:27
Android+SQLite数据库实现的生词记事本功能实例
2023-06-18 10:41:35
C#删除只读文件或文件夹(解决File.Delete无法删除文件)
2022-06-30 15:01:59
Android通过记住密码功能学习数据存储类SharedPreferences详解及实例
2023-05-21 21:37:49
![](https://img.aspxhome.com/file/2023/1/95011_0s.png)
C# Email发送邮件 对方打开邮件可获得提醒
2022-01-07 17:59:14
android实现简易计算器
2023-06-21 04:26:09
![](https://img.aspxhome.com/file/2023/4/83514_0s.jpg)
Java设置PDF跨页表格重复显示表头行的步骤详解
2023-09-21 23:50:33
![](https://img.aspxhome.com/file/2023/9/84629_0s.png)
解决JSTL foEach标签 刷新报错的方法
2021-08-12 13:48:25
C#中委托、事件和回调的使用及说明
2023-08-13 01:00:24
![](https://img.aspxhome.com/file/2023/6/82136_0s.png)