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);
}
}
}
没有什么说的,直接上代码.
查看全文
相关阅读:
JavaScript基本语法2
JavaScript的基本语法
在网页中加入神奇的效果
一个由表单构成的页面
进程理论要点
TCP大文件上传与UDP协议
socket编程相关阐述
网络编程
魔法方法
元类与单例解析
原文地址:https://www.cnblogs.com/LifelongLearning/p/1025131.html
最新文章
获取当前页面的URL
SQL Server 更改数据库登录密码(使用SQL Server身份登录)
创建只能显示自己发布的信息的列表视图
创建BDC(Business Data Connectivity Service)
apt-get 安装路径
Access denied for user 'root'@'localhost' (using password:YES) 解决方案[转]
Ubuntu彻底删除mysql
apache动态添加模块
apache动态编译与静态编译
认识System,System32,Syswow64
热门文章
apache 安装mod_rewrite
netstat
ubuntu防火墙设置
Web程序和应用程序服务器[转]
在eclipse中配置Tomcat并将项目部署到Tomcat上
判断二叉树是否为完全二叉树
spring cloud微服务项目的发布与部署
记录一下关于二叉树的非递归遍历
JavaScript入门2
JavaScript入门
Copyright © 2011-2022 走看看