From 3ef5f7532483938a6f839d42419f09e21f35bb6d Mon Sep 17 00:00:00 2001
From: dyy <807692433@qq.com>
Date: Wed, 21 Dec 2022 16:21:10 +0800
Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=A2=9E=E5=8A=A0=E3=80=91=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0ZipHelper=EF=BC=9A=E5=8E=8B=E7=BC=A9=E6=96=87=E4=BB=B6?=
=?UTF-8?q?=E5=92=8C=E8=A7=A3=E5=8E=8Bzip=E6=A0=BC=E5=BC=8F=E6=96=87?=
=?UTF-8?q?=E4=BB=B6=E7=9A=84=E5=8A=9F=E8=83=BD=EF=BC=9B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Learun.Util/Learun.Util.csproj | 1 +
.../Learun.Util/Learun.Util/Web/ZipHelper.cs | 160 ++++++++++++++++++
2 files changed, 161 insertions(+)
create mode 100644 Learun.Framework.Ultimate V7/Learun.Framework.Module/Learun.Util/Learun.Util/Web/ZipHelper.cs
diff --git a/Learun.Framework.Ultimate V7/Learun.Framework.Module/Learun.Util/Learun.Util/Learun.Util.csproj b/Learun.Framework.Ultimate V7/Learun.Framework.Module/Learun.Util/Learun.Util/Learun.Util.csproj
index 6153a1661..78ebc041b 100644
--- a/Learun.Framework.Ultimate V7/Learun.Framework.Module/Learun.Util/Learun.Util/Learun.Util.csproj
+++ b/Learun.Framework.Ultimate V7/Learun.Framework.Module/Learun.Util/Learun.Util/Learun.Util.csproj
@@ -141,6 +141,7 @@
+
diff --git a/Learun.Framework.Ultimate V7/Learun.Framework.Module/Learun.Util/Learun.Util/Web/ZipHelper.cs b/Learun.Framework.Ultimate V7/Learun.Framework.Module/Learun.Util/Learun.Util/Web/ZipHelper.cs
new file mode 100644
index 000000000..dd2c1ca8b
--- /dev/null
+++ b/Learun.Framework.Ultimate V7/Learun.Framework.Module/Learun.Util/Learun.Util/Web/ZipHelper.cs
@@ -0,0 +1,160 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.IO;
+using System.Diagnostics;
+using ICSharpCode.SharpZipLib;
+using ICSharpCode.SharpZipLib.Zip;
+using ICSharpCode.SharpZipLib.Checksums;
+using ICSharpCode.SharpZipLib.Core;
+
+namespace ZipOneCode.ZipProvider
+{
+ public class ZipHelper
+ {
+ ///
+ /// 压缩文件
+ ///
+ ///
+ ///
+ public static void CreateZip(string sourceFilePath, string destinationZipFilePath)
+ {
+ if (sourceFilePath[sourceFilePath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
+ sourceFilePath += System.IO.Path.DirectorySeparatorChar;
+
+ ZipOutputStream zipStream = new ZipOutputStream(File.Create(destinationZipFilePath));
+ zipStream.SetLevel(6); // 压缩级别 0-9
+ CreateZipFiles(sourceFilePath, zipStream, sourceFilePath);
+
+ zipStream.Finish();
+ zipStream.Close();
+ }
+
+ ///
+ /// 递归压缩文件
+ ///
+ /// 待压缩的文件或文件夹路径
+ /// 打包结果的zip文件路径(类似 D:\WorkSpace\a.zip),全路径包括文件名和.zip扩展名
+ ///
+ private static void CreateZipFiles(string sourceFilePath, ZipOutputStream zipStream, string staticFile)
+ {
+ //Crc32 crc = new Crc32();
+ string[] filesArray = Directory.GetFileSystemEntries(sourceFilePath);
+ foreach (string file in filesArray)
+ {
+ if (Directory.Exists(file)) //如果当前是文件夹,递归
+ {
+ CreateZipFiles(file, zipStream, staticFile);
+ }
+
+ else //如果是文件,开始压缩
+ {
+ string tempFile = file.Substring(staticFile.LastIndexOf("\\") + 1);
+ ZipEntry entry = new ZipEntry(tempFile);
+ zipStream.PutNextEntry(entry);
+
+ FileStream fileStream = File.OpenRead(file);
+
+ byte[] buffer = new byte[fileStream.Length];
+ fileStream.Read(buffer, 0, buffer.Length);
+
+ //entry.DateTime = DateTime.Now;
+ //entry.Size = fileStream.Length;
+ //crc.Reset();
+ //crc.Update(buffer);
+ //entry.Crc = crc.Value;
+
+ zipStream.Write(buffer, 0, buffer.Length);
+ fileStream.Close();
+
+ }
+ }
+ }
+
+ ///
+ /// 功能:解压zip格式的文件。
+ ///
+ /// 压缩文件路径
+ /// 解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹
+ /// 解压是否成功
+ public static bool UnZip(string zipFilePath, string unZipDir)
+ {
+ try
+ {
+ if (zipFilePath == string.Empty)
+ {
+ throw new Exception("压缩文件不能为空!");
+ }
+ if (!File.Exists(zipFilePath))
+ {
+ throw new FileNotFoundException("压缩文件不存在!");
+ }
+ //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹
+ if (unZipDir == string.Empty)
+ {
+ unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
+ }
+ if (!unZipDir.EndsWith("/"))
+ {
+ unZipDir += "/";
+ }
+ if (!Directory.Exists(unZipDir))
+ {
+ Directory.CreateDirectory(unZipDir);
+ }
+ using (var 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 (!string.IsNullOrEmpty(directoryName))
+ {
+ Directory.CreateDirectory(unZipDir + directoryName);
+ }
+ if (directoryName != null && !directoryName.EndsWith("/"))
+ {
+ }
+
+ if (fileName != String.Empty)
+ {
+ using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
+ {
+
+ int size;
+ byte[] data = new byte[2048];
+ while (true)
+ {
+ size = s.Read(data, 0, data.Length);
+ if (size > 0)
+ {
+ streamWriter.Write(data, 0, size);
+ }
+ else
+ {
+ break;
+ }
+ }
+ streamWriter.Close();
+ }
+ }
+ }
+ s.Close();
+ }
+ return true;
+ }
+ catch (Exception e)
+ {
+
+ return false;
+ }
+
+ }
+
+
+ }
+}
\ No newline at end of file