加解压开源组件-SharpZipLib
使用国外开源加压解压库ICSharpCode.SharpZipLib实现加压,该库的官方网站为
http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
使用前需先添加引用:using ICSharpCode.SharpZipLib.Zip;
#region 加压解压方法
/// <summary>
/// 功能:压缩文件(暂时只压缩文件夹下一级目录中的文件,文件夹及其子级被忽略)
/// </summary>
/// <param name="dirPath">被压缩的文件夹夹路径</param>
/// <param name="zipFilePath">生成压缩文件的路径,为空则默认与被压缩文件夹同一级目录,名称为:文件夹名+.zip</param>
/// <param name="err">出错信息</param>
/// <returns>是否压缩成功</returns>
public bool ZipFile(string dirPath, string zipFilePath, out string err)
{
err = "";
if (dirPath == string.Empty)
{
err = "要压缩的文件夹不能为空!";
return false;
}
if (!Directory.Exists(dirPath))
{
err = "要压缩的文件夹不存在!";
return false;
}
//压缩文件名为空时使用文件夹名+.zip
if (zipFilePath == string.Empty)
{
if (dirPath.EndsWith("\\"))
{
dirPath = dirPath.Substring(0, dirPath.Length - 1);
}
zipFilePath = dirPath + ".zip";
}
try
{
string[] filenames = Directory.GetFiles(dirPath);
using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
{
s.SetLevel(9);
byte[] buffer = new byte[4096];
foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
s.Finish();
s.Close();
}
}
catch (Exception ex)
{
err = ex.Message;
return false;
}
return true;
}
/// <summary>
/// 功能:解压zip格式的文件。
/// </summary>
/// <param name="zipFilePath">压缩文件路径</param>
/// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>
/// <param name="err">出错信息</param>
/// <returns>解压是否成功</returns>
public bool UnZipFile(string zipFilePath, string unZipDir, out string err)
{
err = "";
if (zipFilePath == string.Empty)
{
err = "压缩文件不能为空!";
return false;
}
if (!File.Exists(zipFilePath))
{
err = "压缩文件不存在!";
return false;
}
//解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹
if (unZipDir == string.Empty)
unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
if (!unZipDir.EndsWith("\\"))
unZipDir += "\\";
if (!Directory.Exists(unZipDir))
Directory.CreateDirectory(unZipDir);
try
{
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
if (directoryName.Length > 0)
{
Directory.CreateDirectory(unZipDir + directoryName);
}
if (!directoryName.EndsWith("\\"))
directoryName += "\\";
if (fileName != String.Empty)
{
using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
{
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
}
}
}//while
}
}
catch (Exception ex)
{
err = ex.Message;
return false;
}
return true;
}//解压结束
#endregion
转载于:https://www.cnblogs.com/jake-hl/p/4022086.html
加解压开源组件-SharpZipLib相关推荐
- Python加解压文件gzip库操作一文详解
目录 一.gzip GZIP概念 文件格式 二.Python gzip库 gzip.open gzip.GzipFile压缩和解压 gzip.comress()压缩数据 解压数据 第一种 第二种 第三 ...
- C++压缩解压开源库ZIP
1.ZIP下载 ZIP 主要是用于简单的压缩和解压,引入比较方便,而且极其易使用,方便用户操作. 下载地址:http://www.codeproject.com/Articles/7530/Zip-U ...
- 如何在 C# 中用 SharpZipLib 进行 ZIP 压缩与解压(转)
转自:http://www.cftea.com/c/2008/04/A1FQ34RYSYNLFT47.asp SharpZipLib 是一个免费的组件,可以利用它对 ZIP 等多种格式进行压缩与解压. ...
- bat 命令返回结果_bat教程[284] unzip解压
古树屋Click to follow us (1)unzip命令的简介 C:\Users\86137\Desktop>unzip -hUnZip 6.00 of 20 April 2009, b ...
- python怎么解压rar文件_Python解压 rar、zip、tar文件的方法
Q1 :如何解压 rar 压缩包文件? A : Step1:检查是否有 rarfile 第三方库,若没有该模块,则需要进行安装 : Step2:参考代码如下: import rarfile impor ...
- mobaxterm(linux)解压7z.001.001/7z.002.002等分卷文件
下载github代码数据集时通常下载到7z.001.001等文件.这些文件产生是由于总文件过大,因此压缩时将其平均划分成n个分卷,也就是001/002/003.要得到可直接使用的数据文件,需要先将这些 ...
- python解压rar_Python解压 rar、zip、tar文件的方法
Q1 :如何解压 rar 压缩包文件? A : Step1:检查是否有 rarfile 第三方库,若没有该模块,则需要进行安装 : Step2:参考代码如下: import rarfile impor ...
- Universal-Image-Loader,android-Volley,Picasso、Fresco和Glide开源组件加载网络图片的优缺点比较...
在android中的加载网络图片是一件十分令人头疼的事情,在网上有着许多关于加载网络图片的开源库,可以让我们十分方便的加载网络图片.在这里我主要介绍一下我自己在使用Volley, Picasso, U ...
- Universal-Image-Loader,android-Volley,Picasso、Fresco和Glide五大Android开源组件加载网络图片的优缺点比较
在android中的加载网络图片是一件十分令人头疼的事情,在网上有着许多关于加载网络图片的开源库,可以让我们十分方便的加载网络图片.在这里我主要介绍一下我自己在使用Volley, Picasso, U ...
最新文章
- C++基础之指向成员的指针
- yolov5损失函数笔记
- VC采集网页所有表单域
- MATLAB中line函数的用法
- 网络学习笔记网络通讯
- Shiro学习总结(4)——Shrio登陆验证实例详细解读
- ztree 实例地址
- python爬虫什么意思-这样学Python爬虫,想爬什么爬什么
- greenplum客户端工具_GreenPlum数据加载工具gpload | 信春哥,系统稳,闭眼上线不回滚!...
- iPhone防止系统自动下载更新
- VScode 英文翻译成中文插件(英语差的福音)
- Python 读取5张Excel的Sheet自动生成3张Sheet分析结果(减轻同事的工作量,让原本大约2个小时的工作量缩减到1分钟内)
- SCI收录中国期刊一览表
- Qt中出现0xc0000135错误
- 昂科自动烧录器对英飞凌IR38164M系列芯片烧录,效果显著
- Code3Kingdom - Tech Support
- 关于cesium1.92以上版本无法加载draco压缩模型问题
- 笔记本设置WiFi热点命令操作
- 记一次搭建 nodebb 论坛
- Sonic安装部署之——iOS设备接入