|
|
using ICSharpCode.SharpZipLib.Zip;using System;using System.Collections.Generic;using System.IO;using System.Text;
namespace UpdateServiceLibrary.Helper{ public class ZipHelper { /// <summary>
/// 缓冲区大小
/// </summary>
public static int ByteSize { get; set; } = 4096;
/// <summary>
/// 压缩等级 0-9 越大压缩率越高
/// </summary>
public static int ZipLevel { get; set; } = 6;
/// <summary>
/// 解压压缩包到指定文件目录
/// </summary>
/// <param name="zipFile"></param>
/// <param name="unzipfolder"></param>
/// <param name="password"></param>
/// <returns></returns>
public static ResultModel Unzip(FileInfo zipFile, DirectoryInfo unzipfolder, string password = null) { ResultModel resultModel = new ResultModel { IsSuccess = true }; try { //待解压文件是否存在
if (!zipFile.Exists) throw new Exception("待解压文件不存在"); //创建解压目录
if (!unzipfolder.Exists) unzipfolder.Create();
ZipEntry ent; string fileName; //打开文件流
using (var fs = zipFile.OpenRead()) { //创建解压文件流
var zipStream = new ZipInputStream(fs); if (password != null) zipStream.Password = password;
//解压文件
while ((ent = zipStream.GetNextEntry()) != null) { if (string.IsNullOrEmpty(ent.Name)) continue;
fileName = Path.Combine(unzipfolder.FullName, ent.Name); //判断是否为文件夹
if (ent.IsDirectory) { if (!Directory.Exists(fileName)) Directory.CreateDirectory(fileName); continue; }
var folder = Path.GetDirectoryName(fileName); if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
//创建解压文件
int byteSize = ByteSize; byte[] buff = new byte[byteSize]; using (var fw = File.Create(fileName)) { while ((byteSize = zipStream.Read(buff, 0, buff.Length)) > 0) { fw.Write(buff, 0, byteSize); } } } } } catch(Exception ex) { resultModel.IsSuccess = false; resultModel.Message = ex.Message; } return resultModel; }
/// <summary>
/// 压缩文件夹到指定文件
/// </summary>
/// <param name="folder"></param>
/// <param name="fileInfo"></param>
/// <param name="password"></param>
/// <returns></returns>
public static bool ZipFile(DirectoryInfo folder, FileInfo fileInfo, bool createRootFolder = false, string password = null) { //需要压缩的文件夹不存在时退出
if (!folder.Exists) return false;
//创建并设定压缩流
using (ZipOutputStream zipOutputStream = new ZipOutputStream(fileInfo.Create())) { zipOutputStream.SetLevel(ZipLevel); if (password != null) zipOutputStream.Password = password;
//递归压缩文件夹
if (!ZipFile(folder, zipOutputStream, "", !createRootFolder)) return false;
//保存
zipOutputStream.Finish(); zipOutputStream.Close(); } return true; }
/// <summary>
/// 递归压缩文件夹
/// </summary>
/// <param name="folder"></param>
/// <param name="zipOutputStream"></param>
/// <param name="parentFolderName"></param>
/// <returns></returns>
private static bool ZipFile(DirectoryInfo folder, ZipOutputStream zipOutputStream, string parentFolderName, bool rootFolder = false) { try { //创建目录
ZipEntry ent; if (!rootFolder) { ent = new ZipEntry(Path.Combine(parentFolderName, folder.Name + "/")); zipOutputStream.PutNextEntry(ent); zipOutputStream.Flush(); } //初始化缓冲区
int byteSize = ByteSize; byte[] buffer = new byte[byteSize];
//创建文件
foreach (var item in folder.GetFiles()) { using (FileStream fs = item.OpenRead()) { string entPath = string.Empty; if (rootFolder) entPath = Path.Combine(parentFolderName, item.Name); else entPath = Path.Combine(parentFolderName, folder.Name + "/" + item.Name);
ent = new ZipEntry(entPath); ent.DateTime = DateTime.Now; ent.Size = item.Length;
zipOutputStream.PutNextEntry(ent);
while ((byteSize = fs.Read(buffer, 0, buffer.Length)) > 0) { zipOutputStream.Write(buffer, 0, byteSize); } } }
//递归压缩文件夹
if (!rootFolder) parentFolderName = Path.Combine(parentFolderName, folder.Name); foreach (var item in folder.GetDirectories()) { if (!ZipFile(item, zipOutputStream, parentFolderName)) return false; }
return true; } catch (Exception ex) { return false; } }
/// <summary>
/// 压缩单个文件
/// </summary>
/// <param name="sourceFile"></param>
/// <param name="targetFile"></param>
/// <param name="password"></param>
/// <returns></returns>
public static bool ZipFile(FileInfo sourceFile, FileInfo targetFile, string password = null) { //需要压缩的文件不存在时退出
if (!sourceFile.Exists) return false;
try { //创建压缩流
using (ZipOutputStream zipOutputStream = new ZipOutputStream(targetFile.Create())) { //设定压缩级别与密码
zipOutputStream.SetLevel(ZipLevel); if (password != null) zipOutputStream.Password = password;
//初始化缓冲区
int byteSize = ByteSize; byte[] buffer = new byte[byteSize];
//初始化压缩实体
ZipEntry entry = new ZipEntry(sourceFile.Name); entry.DateTime = DateTime.Now; entry.Size = sourceFile.Length;
//压缩
zipOutputStream.PutNextEntry(entry); using (var fs = sourceFile.OpenRead()) { while ((byteSize = fs.Read(buffer, 0, buffer.Length)) > 0) { zipOutputStream.Write(buffer, 0, byteSize); } }
//保存关闭
zipOutputStream.Finish(); zipOutputStream.Close(); } return true; } catch (Exception ex) { return false; } } }}
|