首页 > 其他分享 >ICSharpCode.SharpZipLib 初级使用

ICSharpCode.SharpZipLib 初级使用

时间:2023-03-04 17:04:04浏览次数:32  
标签:TargetDirectory string buffer System 初级 IO ICSharpCode new SharpZipLib

https://blog.csdn.net/vividboy/article/details/2418503

https://www.cnblogs.com/Leo_wl/p/5582871.html

其中将压缩包进行服务器端解压的过程就是通过ICSharpCode.SharpZipLib.dll来实现的。对于这个dll文件,可以通过搜索这个dll文件的名字下载到。

原来没有使用过,所以拿来帮助文档依葫芦画瓢。

1. 在项目中添加对ICSharpCode.SharpZipLib.dll的引用;

2. 在需要使用到ICSharpCode.SharpZipLib中定义的类的编码界面中将其导入(Imports)

(在C#中是using);

3. 在选择命名空间的时候,你会发现在有这样几个同级的命名空间:

ICSharpCode.SharpZipLib.BZip2;

ICSharpCode.SharpZipLib.GZip;

ICSharpCode.SharpZipLib.Tar;

ICSharpCode.SharpZipLib.Zip。

这四个命名空间就对应着四种文件压缩方式,其中我们用的较多的是Zip的压缩方式,因为通过WinRAR软件就可以将文件压缩成.Zip的压缩包。这里有两点需要说明一下:

1) 目前还没有发现提供对.RAR方式压缩文件操作的方法;

2) BZip2, GZip 这两种压缩算法仅仅针对一个文件的压缩,如果你的压缩包要包含许多文件,并需要将这些文件解压出来进行操作的话,最好选用Tar和Zip的压缩方式。

关于这四种压缩算法的介绍可以从维基百科上得到,这里就不再赘述了。

/// <summary>
/// ZIP:压缩单个文件
/// add yuangang by 2016-06-13
/// </summary>
/// <param name="FileToZip">需要压缩的文件(绝对路径)</param>
/// <param name="ZipedPath">压缩后的文件路径(绝对路径)</param>
/// <param name="ZipedFileName">压缩后的文件名称(文件名,默认 同源文件同名)</param>
/// <param name="CompressionLevel">压缩等级(0 无 - 9 最高,默认 5)</param>
/// <param name="BlockSize">缓存大小(每次写入文件大小,默认 2048)</param>
/// <param name="IsEncrypt">是否加密(默认 加密)</param>
public static void ZipFile(string FileToZip, string ZipedPath, string ZipedFileName = "", int CompressionLevel = 5, int BlockSize = 2048, bool IsEncrypt = true)
{
//如果文件没有找到,则报错
if (!System.IO.File.Exists(FileToZip))
{
throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");
}

//文件名称(默认同源文件名称相同)
string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + "\\" + new FileInfo(FileToZip).Name.Substring(0, new FileInfo(FileToZip).Name.LastIndexOf('.')) + ".zip" : ZipedPath + "\\" + ZipedFileName + ".zip";

using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName))
{
using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
{
using (System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
string fileName = FileToZip.Substring(FileToZip.LastIndexOf("\\") + 1);

ZipEntry ZipEntry = new ZipEntry(fileName);

if (IsEncrypt)
{
//压缩文件加密
ZipStream.Password = “123”;
}

ZipStream.PutNextEntry(ZipEntry);

//设置压缩级别
ZipStream.SetLevel(CompressionLevel);

//缓存大小
byte[] buffer = new byte[BlockSize];

int sizeRead = 0;

try
{
do
{
sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
ZipStream.Write(buffer, 0, sizeRead);
}
while (sizeRead > 0);
}
catch (System.Exception ex)
{
throw ex;
}

StreamToZip.Close();
}

ZipStream.Finish();
ZipStream.Close();
}

ZipFile.Close();
}
}

 

/// <summary>
/// ZIP:压缩文件夹
/// add yuangang by 2016-06-13
/// </summary>
/// <param name="DirectoryToZip">需要压缩的文件夹(绝对路径)</param>
/// <param name="ZipedPath">压缩后的文件路径(绝对路径)</param>
/// <param name="ZipedFileName">压缩后的文件名称(文件名,默认 同源文件夹同名)</param>
/// <param name="IsEncrypt">是否加密(默认 加密)</param>
public static void ZipDirectory(string DirectoryToZip, string ZipedPath, string ZipedFileName = "", bool IsEncrypt = true)
{
//如果目录不存在,则报错
if (!System.IO.Directory.Exists(DirectoryToZip))
{
throw new System.IO.FileNotFoundException("指定的目录: " + DirectoryToZip + " 不存在!");
}

//文件名称(默认同源文件名称相同)
string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + "\\" + new DirectoryInfo(DirectoryToZip).Name + ".zip" : ZipedPath + "\\" + ZipedFileName + ".zip";

using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName))
{
using (ZipOutputStream s = new ZipOutputStream(ZipFile))
{
if (IsEncrypt)
{
//压缩文件加密
s.Password = “123”;
}
ZipSetp(DirectoryToZip, s, "");
}
}
}
/// <summary>
/// 递归遍历目录
/// add yuangang by 2016-06-13
/// </summary>
private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
{
if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
{
strDirectory += Path.DirectorySeparatorChar;
}
Crc32 crc = new Crc32();

string[] filenames = Directory.GetFileSystemEntries(strDirectory);

foreach (string file in filenames)// 遍历所有的文件和目录
{

if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
{
string pPath = parentPath;
pPath += file.Substring(file.LastIndexOf("\\") + 1);
pPath += "\\";
ZipSetp(file, s, pPath);
}

else // 否则直接压缩文件
{
//打开压缩文件
using (FileStream fs = File.OpenRead(file))
{

byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);

string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1);
ZipEntry entry = new ZipEntry(fileName);

entry.DateTime = DateTime.Now;
entry.Size = fs.Length;

fs.Close();

crc.Reset();
crc.Update(buffer);

entry.Crc = crc.Value;
s.PutNextEntry(entry);

s.Write(buffer, 0, buffer.Length);
}
}
}
}

 

/// <summary>
/// ZIP:解压一个zip文件
/// add yuangang by 2016-06-13
/// </summary>
/// <param name="ZipFile">需要解压的Zip文件(绝对路径)</param>
/// <param name="TargetDirectory">解压到的目录</param>
/// <param name="Password">解压密码</param>
/// <param name="OverWrite">是否覆盖已存在的文件</param>
public static void UnZip(string ZipFile, string TargetDirectory, string Password, bool OverWrite = true)
{
//如果解压到的目录不存在,则报错
if (!System.IO.Directory.Exists(TargetDirectory))
{
throw new System.IO.FileNotFoundException("指定的目录: " + TargetDirectory + " 不存在!");
}
//目录结尾
if (!TargetDirectory.EndsWith("\\")) { TargetDirectory = TargetDirectory + "\\"; }

using (ZipInputStream zipfiles = new ZipInputStream(File.OpenRead(ZipFile)))
{
zipfiles.Password = Password;
ZipEntry theEntry;

while ((theEntry = zipfiles.GetNextEntry()) != null)
{
string directoryName = "";
string pathToZip = "";
pathToZip = theEntry.Name;

if (pathToZip != "")
directoryName = Path.GetDirectoryName(pathToZip) + "\\";

string fileName = Path.GetFileName(pathToZip);

Directory.CreateDirectory(TargetDirectory + directoryName);

if (fileName != "")
{
if ((File.Exists(TargetDirectory + directoryName + fileName) && OverWrite) || (!File.Exists(TargetDirectory + directoryName + fileName)))
{
using (FileStream streamWriter = File.Create(TargetDirectory + directoryName + fileName))
{
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = zipfiles.Read(data, 0, data.Length);

if (size > 0)
streamWriter.Write(data, 0, size);
else
break;
}
streamWriter.Close();
}
}
}
}

zipfiles.Close();
}
}

 

标签:TargetDirectory,string,buffer,System,初级,IO,ICSharpCode,new,SharpZipLib
From: https://www.cnblogs.com/Dongmy/p/17178556.html

相关文章

  • airtest IDE初级教程
    一、简介AirtestIDE是一款跨平台的UI自动化测试编辑器,内置了Airtest和Poco的相关插件功能,能够使用它快速简单地编写Airtest和Poco代码。1.Airtest框架:Airtest......
  • COBOL语言初级教程(1)--COBOL简介(续)
    (续)ANSICOBOL于1985年发布COBOL-85标准。后X3J4更名为J4,负责处理COBOL语言的发展标准化。2002年,发布COBOL2002标准。该标准新特性包括:·UserDefinedFunctions·Obje......
  • COBOL语言初级教程(1)--COBOL简介
    最近由于工作需要,必须学习COBOL语言,将学习笔记整理出来,共享一下,希望有助于学习此方面的朋友。难免有错漏之错,望不吝赐教。1、COBOL简介COBOL是CommonBusiness-OrientedLan......
  • Markdown初级语法
    Markdown学习标题“#+标题名字”生成一级标题”##+标题名字“生成二级标题“###+标题名字“生成三级标题三级标题四级标题最多可以生成到六级标题字......
  • C002Android学习笔记-初级控件(二)
    一、简单控件1、文本视图TextView常用设置:①设置文本内容:xml中——text;代码中——setText;②设置文本颜色:xml中——textColor;代码中——setTe......
  • C003Android学习笔记-初级控件(三)
    一、图形基础1、图形Drawable概述:Android把所有显示出来的图形都抽象为Drawable(可绘制的),这里的图形不止是图片,还包括色块、画板、背景等;引用Dr......
  • 初级会计(一)- 实务基础
    第一章概述第一节会计概念、职能和目标一、会计概念(一)会计的定义现代会计是以货币为主要计量单位,采用专门方法和程序,对企业和行政、事业单位的经济活动过程及其结果......
  • 初级入门 ---用基本图形构建规则形体。
    WebGL坐标系本节开始学习3D形体的绘制,与之前几个章节绘制点和面不同,3D形体的顶点坐标需要包含深度信息Z轴坐标。所以我们先了解一下WebGL坐标系的概念。WebGL......
  • 杭州怎么办初级经济师证和会计师证
    凡遵守中华人民共和国宪法和法律,具有良好的道德品行和业务素质,符合初级、中级、高级经济专业技术资格考试报名条件的经济专业人员,均可报名参加相应级别的考试。(一)凡从......
  • 初级算法之树
    树比链表稍微复杂,因为链表是线性数据结构,而树不是。树的问题可以由广度优先搜索或深度优先搜索解决。在本章节中,我们提供了一个对于练习广度优先遍历很好的题目。我......