zoukankan
html css js c++ java
分享"SharpZipLip使用代码"
zip类
public
class
ZipClass
{
/**/
///
<summary>
///
压缩方法
///
</summary>
///
<param name="strPath">
要压缩文件夹
</param>
///
<param name="strFileName">
生成的文件名
</param>
///
<param name="PassWord">
密码
</param>
///
<returns>
1 成功 -1输入的压缩文件夹为空 -2 输入的压缩文件夹目录不存在!
</returns>
public
static
int
Zip(
string
strPath,
string
strFileName,
string
PassWord)
{
if
(
!
String.IsNullOrEmpty(strPath))
{
//
throw new Exception("输入的压缩文件夹为空");
return
-
1
;
}
if
(
!
Directory.Exists(strPath))
{
//
throw new Exception("输入的压缩文件夹目录不存在!");
return
-
2
;
}
if
(
!
File.Exists(strFileName))
{
File.Create(strFileName);
}
ZipOutputStream zip
=
new
ZipOutputStream(File.Create(strFileName));
if
(
!
string
.IsNullOrEmpty(PassWord))
zip.Password
=
PassWord;
IList
<
FileInfo
>
list
=
new
List
<
FileInfo
>
();
GetFileList(strPath, list);
try
{
byte
[] buffer
=
new
byte
[
2048
];
int
count
=
0
;
foreach
(FileInfo fi
in
list)
{
count
=
0
;
using
(FileStream fs
=
File.OpenRead(fi.FullName))
{
ZipEntry entry
=
new
ZipEntry(fi.FullName.Substring(strPath.Length
+
1
));
entry.DateTime
=
fi.LastWriteTime;
entry.Size
=
fs.Length;
zip.PutNextEntry(entry);
while
((count
=
fs.Read(buffer,
0
,
2048
))
>
0
)
{
zip.Write(buffer,
0
, count);
}
}
}
}
catch
{
throw
;
}
finally
{
zip.Finish();
zip.Close();
}
return
1
;
}
private
static
void
GetFileList(
string
strPath, IList
<
FileInfo
>
list)
{
DirectoryInfo di
=
new
DirectoryInfo(strPath);
foreach
(DirectoryInfo di1
in
di.GetDirectories())
{
GetFileList(di1.FullName, list);
}
foreach
(FileInfo fi
in
di.GetFiles())
{
list.Add(fi);
}
}
/**/
///
<summary>
///
解压缩文件
///
</summary>
///
<param name="strFileName">
压缩文件
</param>
///
<param name="strPath">
目标目录
</param>
///
<param name="PassWord">
密码
</param>
///
<returns>
1 成功 -1输入的压缩文件夹为空 -2 输入的解压缩文件夹目录不存在!-3 文件不存在
</returns>
public
static
int
UnZip(
string
strFileName,
string
strPath,
string
PassWord)
{
if
(
!
String.IsNullOrEmpty(strPath))
{
//
throw new Exception("输入的压缩文件夹为空");
return
-
1
;
}
if
(
!
Directory.Exists(strPath))
{
//
throw new Exception("输入的压缩文件夹目录不存在!");
return
-
2
;
}
if
(
!
File.Exists(strFileName))
{
//
throw new Exception("文件:" + strFileName + "不存在!");
return
-
3
;
}
ZipEntry entry;
ZipInputStream zis
=
null
;
try
{
zis
=
new
ZipInputStream(File.Open(strFileName, FileMode.Open));
if
(
!
string
.IsNullOrEmpty(PassWord))
zis.Password
=
PassWord;
byte
[] buffer
=
new
byte
[
2048
];
int
count
=
0
;
while
((entry
=
zis.GetNextEntry())
!=
null
)
{
CreateDirList(entry.Name, strPath);
string
strPath1
=
strPath
+
"
\\
"
+
entry.Name;
using
(FileStream streamWriter
=
File.Create(strPath1))
{
while
((count
=
zis.Read(buffer,
0
,
2048
))
>
0
)
{
streamWriter.Write(buffer,
0
, count);
}
}
File.SetLastWriteTime(strPath1, entry.DateTime);
}
}
catch
{
throw
;
}
finally
{
if
(zis
!=
null
)
zis.Close();
}
return
1
;
}
private
static
void
CreateDirList(
string
filename,
string
basePath)
{
string
dirName
=
basePath;
string
[] dirlevelname
=
filename.Split(
'
\\
'
);
for
(
int
i
=
0
; i
<
dirlevelname.Length
-
1
; i
++
)
{
dirName
+=
"
\\
"
+
dirlevelname[i];
if
(Directory.Exists(dirName))
{
continue
;
}
Directory.CreateDirectory(dirName);
}
}
}
没有什么说的,直接上代码.
查看全文
相关阅读:
Linux中的mv命令详解
ASP.NET问题处理---targetFramwork=‘4.0’错误
Android----二维码开发
android--HttpURLConnection(转载)
SQL server 跨库插入数据
AndroidStudio中 R文件缺失的办法
ASP.NET程序如何更新发布
Android切换页面--setContentView
Android----service
Android开发必备:颜色选择
原文地址:https://www.cnblogs.com/LifelongLearning/p/1025131.html
最新文章
node-sass安装失败,安装后无法使用 gyp verb check python checking for Python executable "python2" in the PATH
node开发概述
前端常用算法
表格里多个图片上传
mac下的tensorflow安装与测试
idea自动生成方法注释
开启服务器配置后,微信自定义菜单突然消失,需要重新启动。
公众号获取推广二维码问题(获取access_token的时侯报错access_token is invalid or not latest hints)
java时间处理相关的类
Centos7下安装postgresql
热门文章
SSH远程连接时间设置,防止老掉线。
公众号支付由body为中文时签名错误解决方法
微信支付(公众号支付JSAPI)的各种坑--缺参数--调用支付jsapi缺少参数:appld--支付验证签名失败
清理tomcat日志
python 3 基础之局部变量与全局变量
python 3 基础之自定义函数
python 3 基础之各种运算符参照表
python 3 基础之字典,详解
python 3 基础之元组tuple,详解
CentOS 7.X 防火墙服务开启关闭查看、端口的查询关闭开放
Copyright © 2011-2022 走看看