分享SharpZipLip使用代码
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);
}
}
}
没有什么说的,直接上代码.
转载于:https://www.cnblogs.com/LifelongLearning/archive/2008/01/03/1025131.html
分享SharpZipLip使用代码相关推荐
- 2月15日Python线上峰会免费学!6场精华分享,用代码“抗”疫
截至截止2月12号09时43分,新型冠状病毒在全国已确诊44726例,疑似病例已达21675例.而专家所说的"拐点"始终未至,受疫的影响,各大公司开启远程办公模式,将返回工作场所办 ...
- php暴力引流代码,利用霸屏暴力引流,免费分享霸屏代码
导语:这个方法我之前好像在哪见过. 这个霸屏很代码,特别暴力. 比如我在QQ空间给别人留言 看到没有,我在QQ空间给别人留言之后,出现这样一个效果. 这样的效果怎么引流呢? 其实很简单,因为你发了这个 ...
- html怎么写分享代码,怎么写一个文件分享网页?百度一键分享按钮HTML代码
怎么写一个文件分享网页?首先一个基本的网页包含主要的哪些就不说了,下面直接上关键的代码即可,把他放到需要的地方即可.下面一起来看看吧. 百度一键分享按钮HTML代码 下面是需要加分享按钮的css代码 ...
- 代码太多不要怕,分享一个阅读代码的神器
大家好,我是程序员小哈. 又到了知识分享时间,今天我给大家分享一个阅读代码的神器,如果对你有所帮助的话,还请文末点赞支持一下哈. 今天给大家介绍一个查看代码的编辑器软件:Source Insight, ...
- 项目分享| DIY STM32控制的电子负载(硬件方案分享,软件代码下载)
今天分享的项目是基于STM32控制的简易电子负载,具有恒流.恒压.恒电阻以及恒功率功能,最大输入电压为100V,热设计功耗为100W,最大负载电流为12A. 感兴趣的同学可以关注下.感谢! 系统结构图 ...
- java 朋友圈分享接口_Java实现微信公众平台朋友圈分享功能详细代码
其实分享的方法在微信官网有较为详细的文档说明,现就其中一些比较绕的步骤进行总结,有问题随时交流哈. 首先微信其实已经自带分享到朋友圈,朋友,qq空间等功能,对于开发微信专门提供了一个接口,可以根据需要 ...
- JAVA 实现朋友圈_Java实现微信公众平台朋友圈分享功能详细代码
其实分享的方法在微信官网有较为详细的文档说明,现就其中一些比较绕的步骤进行总结,有问题随时交流哈. 首先微信其实已经自带分享到朋友圈,朋友,qq空间等功能,对于开发微信专门提供了一个接口,可以根据需要 ...
- android 后台截屏代码,Android实现截图和分享功能的代码
先给大家展示下效果图吧 直接上代码: xml的布局: android:id="@+id/btn_jp" android:layout_marginTop="10dip&q ...
- 【视觉目标跟踪最高峰】VOT Challenge 2017 亚军北邮团队技术分享(附代码)
视觉跟踪领域国际顶级赛事 Visual-Object-Tracking Challenge (VOT) 2017年结果出炉,结合传统滤波及深度学习的方案取得最佳成绩.本文是第二名北京邮电大学代表团队的 ...
最新文章
- Kotlin学习笔记-基础语法
- matlab调用c语言驱动函数,[转载]Matlab调用C语言函数
- 牛客网专题 概率dp
- Runtime的应用
- 设计一个算法,删除一个单链表L中元素值最大的结点(假设最大值结点是唯一的)
- 下午去公司无聊了一下午
- C# 控件BackColor = Color.Transparent没有透明的原因
- 如何找到稳定的网站空间
- hutool 获取某月最后一天_Hutool之时间工具——DateUtil
- C++中this指针
- 如何配置Java环境
- 蓝图出炉:大家将能够利用英伟达技术打造自己的自动驾驶车辆
- win10修改dns服务器地址,教你Win10如何更改首选DNS服务器地址
- Excel自学笔记 第五节 怎么在列后加统一固定字符?
- 在word中插入ppt
- Nginx入门学习笔记
- problem 1278
- 图网络embeding transE及node2vec方法
- DAY-8 Rviz 转 Gazebo
- ZYNQ-AX7020学习笔记