开源项目 11 Ionic Zip

时间:2020-12-09 05:26:38
using Ionic.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApp2.test1
{
public class Class11
{
//Ionic.Zip.dll
//原文:https://www.cnblogs.com/chenghu/p/4607866.html
//https://blog.csdn.net/XuWei_XuWei/article/details/18663185 public void test1()
{
string FileName = DateTime.Now.ToString("yyMMddHHmmssff");
//ZipFile实例化一个压缩文件保存路径的一个对象zip
using (ZipFile zip = new ZipFile(@"E:\\4\\" + FileName + ".zip", Encoding.Default))
{
//加密压缩
//zip.Password = "123456";
//将要压缩的文件夹添加到zip对象中去(要压缩的文件夹路径和名称)
zip.AddDirectory(@"E:\\WenQuan3_3dtile");
//将要压缩的文件添加到zip对象中去,如果文件不存在抛错FileNotFoundExcept
//zip.AddFile(@"E:\\yangfeizai\\12051214544443\\"+"Jayzai.xml");
zip.Save();
}
} public void test2()
{
string FileName = DateTime.Now.ToString("yyMMddHHmmssff");
//ZipFile实例化一个对象zip
using (ZipFile zip = new ZipFile())
{
//加密压缩
zip.Password = "";
//将要压缩的文件夹添加到zip对象中去(要压缩的文件夹路径和名称)
zip.AddDirectory(@"E:\\WenQuan3_3dtile");
//将要压缩的文件添加到zip对象中去,如果文件不存在抛错FileNotFoundExcept
//zip.AddFile(@"E:\\yangfeizai\\12051214544443\\"+"Jayzai.xml");
//用zip对象中Save重载方法保存压缩的文件,参数为保存压缩文件的路径
zip.Save(@"E:\\4\\" + FileName + ".zip");
}
} public void test3()
{
string unZipFileAddress = "E://4.zip";//解压文件地址
string unZipFileExistAssress = "E://4//";//解压后存放目录
string pass = null; using (ZipFile zip = new ZipFile(unZipFileAddress, Encoding.Default))
{
//解压密码
if (pass != null)
{
zip.Password = pass;
}
//目录存在与否
if (!Directory.Exists(unZipFileExistAssress))
{
Directory.CreateDirectory(unZipFileExistAssress);
}
//解压
foreach (ZipEntry entry in zip)
{
entry.Extract(@unZipFileExistAssress);
}
}
} /// <summary>
/// 实现解压
/// </summary>
/// <param name="unZipFileAddress">解压文件地址</param>
/// <param name="unZipFileExistAssress">解压后存放目录</param>
/// <param name="pass">解压密码</param>
public void UnZipFile(string unZipFileAddress, string unZipFileExistAssress, string pass = null)
{
using (ZipFile zip = new ZipFile(unZipFileAddress, Encoding.Default))
{
//解压密码
if (pass != null)
{
zip.Password = pass;
}
//目录存在与否
if (!Directory.Exists(unZipFileExistAssress))
{
Directory.CreateDirectory(unZipFileExistAssress);
}
//解压
foreach (ZipEntry entry in zip)
{
entry.Extract(@unZipFileExistAssress);
}
}
} }
}