zoukankan
html css js c++ java
在C#中运用SharpZipLib和unrar进行解压缩
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.IO;
using
ICSharpCode.SharpZipLib.Zip;
namespace
Campton.Tools
{
public
class
ZipManager
{
构造函数
#region
构造函数
public
ZipManager()
{ }
#endregion
私有静态变量
#region
私有静态变量
private
static
ZipManager _instance;
#endregion
静态方法
#region
静态方法
/**/
///
<summary>
///
创建实例方法
///
</summary>
///
<returns>
返回ZipManager对象实例
</returns>
public
static
ZipManager Instance()
{
if
(_instance
==
null
)
_instance
=
new
ZipManager();
return
_instance;
}
#endregion
公有方法
#region
公有方法
/**/
///
<summary>
///
解压文件
///
</summary>
///
<param name="zipFile">
zip文件(完整的绝对路径)
</param>
///
<param name="destFolder">
目标目录(绝对路径)
</param>
///
<returns>
是否解压成功(true 为成功 false 为失败)
</returns>
public
bool
UnCompress(
string
zipFile,
string
destFolder)
{
if
(
!
File.Exists(zipFile))
return
false
;
string
exts
=
Path.GetExtension(zipFile);
if
(
string
.Compare(exts,
"
.zip
"
,
true
)
==
0
)
{
return
UnZip(zipFile, destFolder);
}
else
if
(
string
.Compare(exts,
"
.rar
"
,
true
)
==
0
)
{
return
UnRar(zipFile, destFolder);
}
else
{
return
false
;
}
}
#endregion
私有方法
#region
私有方法
/**/
///
<summary>
///
对Zip文件进行解压
///
</summary>
///
<param name="zipFile">
zip文件(完整的绝对路径)
</param>
///
<param name="destFolder">
目标目录(绝对路径)
</param>
///
<returns>
是否解压成功(true 为成功 false 为失败)
</returns>
private
bool
UnZip(
string
zipFile,
string
destFolder)
{
if
(
!
File.Exists(zipFile))
return
false
;
if
(
!
Directory.Exists(destFolder))
Directory.CreateDirectory(destFolder);
FileInfo fi
=
new
FileInfo(zipFile);
try
{
using
(ZipInputStream zis
=
new
ZipInputStream(fi.OpenRead()))
{
ZipEntry ze;
while
((ze
=
zis.GetNextEntry())
!=
null
)
{
string
path
=
Path.Combine(destFolder, ze.Name);
if
(ze.IsDirectory)
{
Directory.CreateDirectory(path);
}
else
{
using
(Stream outStream
=
new
FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write))
{
int
size
=
2048
;
byte
[] data
=
new
byte
[size];
while
(size
>
0
)
{
size
=
zis.Read(data,
0
, data.Length);
outStream.Write(data,
0
, size);
}
outStream.Flush();
}
}
}
}
}
catch
(Exception)
{
return
false
;
}
return
true
;
}
/**/
///
<summary>
///
对RAR所支持的压缩文件进行解压
///
</summary>
///
<param name="zipFile">
压缩文件(完整的绝对路径)
</param>
///
<param name="destFolder">
目标目录(绝对路径)
</param>
///
<returns>
是否解压成功(true 为成功 false 为失败)
</returns>
private
bool
UnRar(
string
zipFile,
string
destFolder)
{
if
(
!
File.Exists(zipFile))
return
false
;
if
(
!
Directory.Exists(destFolder))
Directory.CreateDirectory(destFolder);
Unrar unRar
=
new
Unrar();
try
{
unRar.DestinationPath
=
destFolder;
unRar.Open(zipFile, Unrar.OpenMode.Extract);
while
(unRar.ReadHeader())
{
unRar.Extract();
}
return
true
;
}
catch
(Exception)
{
return
false
;
}
finally
{
if
(unRar
!=
null
)
{
unRar.Close();
unRar.Dispose();
}
}
}
#endregion
}
}
查看全文
相关阅读:
创建user keywords
robotframework中list和dict variables
安装sshlibrary库报错:Could not find a version that satisfies the requirement
【转】用U盘制作启动盘后空间变小的恢复方法
docker "exec format error"
window cmd 设置IP,关闭防火墙,开启远程桌面
Linux iptables
python logging 模块
docker 命令
python xmlrpc入门
原文地址:https://www.cnblogs.com/afxcn/p/744234.html
最新文章
python3 日期加减
python3 fabric建立ssh连接
python3 时区转换
变量的作用域解析
小知识点
宏的使用
结构体的房子模型解析
调用函数时参数传递的单向性分析
strcpy和strncpy用法和区别
树莓派读取ip
热门文章
j-link "the connected j-link is defective"问题的解决
BAL数据集详解
FreeRTOS使用汇总
树莓派4B—LCD触摸屏和硬件串口配置
视觉十四讲:第八讲_直接法
视觉十四讲:第八讲_光流法(特征点追踪)
图像梯度
图像内插
opencv::parallel_for_使用说明
DateTime library时间戳
Copyright © 2011-2022 走看看