浏览代码

SevenZipCompressor增加压缩流操作

懒得勤快 3 年之前
父节点
当前提交
c58ba2d292

+ 20 - 1
Masuit.Tools.Abstractions/Files/ISevenZipCompressor.cs

@@ -1,4 +1,5 @@
-using SharpCompress.Common;
+using Masuit.Tools.Systems;
+using SharpCompress.Common;
 using System.Collections.Generic;
 using System.IO;
 
@@ -26,6 +27,15 @@ namespace Masuit.Tools.Files
         /// <param name="archiveType"></param>
         void Zip(IEnumerable<string> files, string zipFile, string rootdir = "", ArchiveType archiveType = ArchiveType.SevenZip);
 
+        /// <summary>
+        /// 压缩多个文件
+        /// </summary>
+        /// <param name="streams">多个文件流</param>
+        /// <param name="zipFile">压缩到...</param>
+        /// <param name="archiveType"></param>
+        /// <param name="disposeAllStreams">是否需要释放所有流</param>
+        void Zip(DisposeableDictionary<string, Stream> streams, string zipFile, ArchiveType archiveType = ArchiveType.Zip, bool disposeAllStreams = false);
+
         /// <summary>
         /// 将多个文件压缩到一个文件流中,可保存为zip文件,方便于web方式下载
         /// </summary>
@@ -34,5 +44,14 @@ namespace Masuit.Tools.Files
         /// <param name="archiveType"></param>
         /// <returns>文件流</returns>
         MemoryStream ZipStream(IEnumerable<string> files, string rootdir = "", ArchiveType archiveType = ArchiveType.SevenZip);
+
+        /// <summary>
+        /// 将多个文件压缩到一个文件流中,可保存为zip文件,方便于web方式下载
+        /// </summary>
+        /// <param name="streams">多个文件流</param>
+        /// <param name="archiveType"></param>
+        /// <param name="disposeAllStreams">是否需要释放所有流</param>
+        /// <returns>文件流</returns>
+        MemoryStream ZipStream(DisposeableDictionary<string, Stream> streams, ArchiveType archiveType = ArchiveType.Zip, bool disposeAllStreams = false);
     }
 }

+ 61 - 0
Masuit.Tools.Abstractions/Files/SevenZipCompressor.cs

@@ -10,6 +10,7 @@ using System.Net.Http;
 using System.Text;
 using System.Threading.Tasks;
 using System.Web;
+using Masuit.Tools.Systems;
 
 namespace Masuit.Tools.Files
 {
@@ -51,6 +52,37 @@ namespace Masuit.Tools.Files
             return ms;
         }
 
+        /// <summary>
+        /// 将多个文件压缩到一个文件流中,可保存为zip文件,方便于web方式下载
+        /// </summary>
+        /// <param name="streams">多个文件流</param>
+        /// <param name="archiveType"></param>
+        /// <param name="disposeAllStreams">是否需要释放所有流</param>
+        /// <returns>文件流</returns>
+        public MemoryStream ZipStream(DisposeableDictionary<string, Stream> streams, ArchiveType archiveType = ArchiveType.Zip, bool disposeAllStreams = false)
+        {
+            using var archive = ArchiveFactory.Create(archiveType);
+            foreach (var pair in streams)
+            {
+                archive.AddEntry(pair.Key, pair.Value, true);
+            }
+
+            var ms = new MemoryStream();
+            archive.SaveTo(ms, new WriterOptions(CompressionType.LZMA)
+            {
+                LeaveStreamOpen = true,
+                ArchiveEncoding = new ArchiveEncoding()
+                {
+                    Default = Encoding.UTF8
+                }
+            });
+            if (disposeAllStreams)
+            {
+                streams.Dispose();
+            }
+            return ms;
+        }
+
         /// <summary>
         /// 压缩多个文件
         /// </summary>
@@ -71,6 +103,35 @@ namespace Masuit.Tools.Files
             });
         }
 
+        /// <summary>
+        /// 压缩多个文件
+        /// </summary>
+        /// <param name="streams">多个文件流</param>
+        /// <param name="zipFile">压缩到...</param>
+        /// <param name="archiveType"></param>
+        /// <param name="disposeAllStreams">是否需要释放所有流</param>
+        public void Zip(DisposeableDictionary<string, Stream> streams, string zipFile, ArchiveType archiveType = ArchiveType.Zip, bool disposeAllStreams = false)
+        {
+            using var archive = ArchiveFactory.Create(archiveType);
+            foreach (var pair in streams)
+            {
+                archive.AddEntry(pair.Key, pair.Value, true);
+            }
+
+            archive.SaveTo(zipFile, new WriterOptions(CompressionType.LZMA)
+            {
+                LeaveStreamOpen = true,
+                ArchiveEncoding = new ArchiveEncoding()
+                {
+                    Default = Encoding.UTF8
+                }
+            });
+            if (disposeAllStreams)
+            {
+                streams.Dispose();
+            }
+        }
+
         /// <summary>
         /// 解压文件,自动检测压缩包类型
         /// </summary>

+ 1 - 1
Masuit.Tools.Abstractions/Masuit.Tools.Abstractions.csproj

@@ -4,7 +4,7 @@
     <LangVersion>latest</LangVersion>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
     <CodeAnalysisRuleSet />
-    <Version>2.4.7.9</Version>
+    <Version>2.4.8</Version>
     <Authors>懒得勤快</Authors>
     <Description>Masuit.Tools基础公共库,包含一些常用的操作类,大都是静态类,加密解密,反射操作,Excel简单导出,权重随机筛选算法,分布式短id,表达式树,linq扩展,文件压缩,多线程下载和FTP客户端,硬件信息,字符串扩展方法,日期时间扩展操作,中国农历,大文件拷贝,图像裁剪,验证码,断点续传,集合扩展等常用封装。</Description>
     <Copyright>懒得勤快,长空X</Copyright>

+ 54 - 0
Masuit.Tools.Abstractions/Systems/DisposeableDictionary.cs

@@ -0,0 +1,54 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Masuit.Tools.Systems;
+
+/// <summary>
+/// 值可被Dispose的字典类型
+/// </summary>
+/// <typeparam name="TKey"></typeparam>
+/// <typeparam name="TValue"></typeparam>
+public class DisposeableDictionary<TKey, TValue> : NullableDictionary<TKey, TValue>, IDisposable where TValue : IDisposable
+{
+    private bool _isDisposed;
+
+    /// <summary>
+    /// 终结器
+    /// </summary>
+    ~DisposeableDictionary()
+    {
+        Dispose(false);
+    }
+
+    /// <summary>
+    ///
+    /// </summary>
+    public void Dispose()
+    {
+        if (_isDisposed)
+        {
+            return;
+        }
+
+        Dispose(true);
+        _isDisposed = true;
+        GC.SuppressFinalize(this);
+    }
+
+    public DisposeableDictionary(IDictionary<NullObject<TKey>, TValue> dictionary) : base(dictionary)
+    {
+    }
+
+    /// <summary>
+    /// 释放
+    /// </summary>
+    /// <param name="disposing"></param>
+    public void Dispose(bool disposing)
+    {
+        foreach (var s in Values.Where(v => v != null))
+        {
+            s.Dispose();
+        }
+    }
+}

+ 4 - 0
Masuit.Tools.Abstractions/Systems/NullableDictionary.cs

@@ -34,6 +34,10 @@ public class NullableDictionary<TKey, TValue> : Dictionary<NullObject<TKey>, TVa
     {
     }
 
+    /// <summary>
+    ///
+    /// </summary>
+    /// <param name="key"></param>
     public new TValue this[NullObject<TKey> key]
     {
         get => TryGetValue(key, out var value) ? value : default;

+ 1 - 1
Masuit.Tools.AspNetCore/Masuit.Tools.AspNetCore.csproj

@@ -18,7 +18,7 @@
         <LangVersion>latest</LangVersion>
         <RepositoryType>Github</RepositoryType>
         <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
-        <Version>1.0.3</Version>
+        <Version>1.0.4</Version>
         <FileVersion>1.0</FileVersion>
         <Company>masuit.com</Company>
         <AssemblyVersion>1.0</AssemblyVersion>

+ 1 - 1
Masuit.Tools.Core/Masuit.Tools.Core.csproj

@@ -18,7 +18,7 @@ github:https://github.com/ldqk/Masuit.Tools
         <UserSecretsId>830c282f-f7c1-42be-8651-4cd06ac8e73f</UserSecretsId>
         <RepositoryType>Github</RepositoryType>
         <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
-        <Version>2.4.7.9</Version>
+        <Version>2.4.8</Version>
         <FileVersion>2.4.5.6</FileVersion>
         <Company>masuit.com</Company>
         <AssemblyVersion>2.4.5.6</AssemblyVersion>

+ 4 - 4
Masuit.Tools.Excel/Masuit.Tools.Excel.csproj

@@ -3,7 +3,7 @@
         <TargetFrameworks>netstandard2.0;netstandard2.1;net461;net5;net6</TargetFrameworks>
         <LangVersion>latest</LangVersion>
         <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
-        <Version>1.0.8</Version>
+        <Version>1.0.9</Version>
         <Authors>懒得勤快</Authors>
         <Description>Masuit.Tools.Excel导出库,支持一些简单数据的导出,支持图片列</Description>
         <Copyright>懒得勤快</Copyright>
@@ -16,9 +16,9 @@
         <RepositoryType>Github</RepositoryType>
         <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
         <PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
-        <FileVersion>1.0.8</FileVersion>
+        <FileVersion>1.0.9</FileVersion>
         <Company>masuit.org</Company>
-        <AssemblyVersion>1.0.8</AssemblyVersion>
+        <AssemblyVersion>1.0.9</AssemblyVersion>
         <PackageLicenseUrl>https://github.com/ldqk/Masuit.Tools/blob/master/LICENSE</PackageLicenseUrl>
         <EmbedUntrackedSources>true</EmbedUntrackedSources>
         <IncludeSymbols>true</IncludeSymbols>
@@ -28,7 +28,7 @@
         <DocumentationFile>.\Masuit.Tools.Excel.xml</DocumentationFile>
     </PropertyGroup>
     <ItemGroup>
-        <PackageReference Include="EPPlus" Version="5.8.7" />
+        <PackageReference Include="EPPlus" Version="5.8.8" />
         <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
         <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
     </ItemGroup>

+ 1 - 1
Masuit.Tools.NoSQL.MongoDBClient/Masuit.Tools.NoSQL.MongoDBClient.csproj

@@ -38,7 +38,7 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <PackageReference Include="MongoDB.Driver" Version="2.14.1" />
+    <PackageReference Include="MongoDB.Driver" Version="2.15.0" />
   </ItemGroup>
 
 </Project>

+ 61 - 0
Masuit.Tools/Files/SevenZipCompressor.cs

@@ -10,6 +10,7 @@ using System.Net.Http;
 using System.Text;
 using System.Threading.Tasks;
 using System.Web;
+using Masuit.Tools.Systems;
 
 namespace Masuit.Tools.Files
 {
@@ -39,6 +40,37 @@ namespace Masuit.Tools.Files
             return ms;
         }
 
+        /// <summary>
+        /// 将多个文件压缩到一个文件流中,可保存为zip文件,方便于web方式下载
+        /// </summary>
+        /// <param name="streams">多个文件流</param>
+        /// <param name="archiveType"></param>
+        /// <param name="disposeAllStreams">是否需要释放所有流</param>
+        /// <returns>文件流</returns>
+        public static MemoryStream ZipStream(DisposeableDictionary<string, Stream> streams, ArchiveType archiveType = ArchiveType.Zip, bool disposeAllStreams = false)
+        {
+            using var archive = ArchiveFactory.Create(archiveType);
+            foreach (var pair in streams)
+            {
+                archive.AddEntry(pair.Key, pair.Value, true);
+            }
+
+            var ms = new MemoryStream();
+            archive.SaveTo(ms, new WriterOptions(CompressionType.LZMA)
+            {
+                LeaveStreamOpen = true,
+                ArchiveEncoding = new ArchiveEncoding()
+                {
+                    Default = Encoding.UTF8
+                }
+            });
+            if (disposeAllStreams)
+            {
+                streams.Dispose();
+            }
+            return ms;
+        }
+
         /// <summary>
         /// 压缩多个文件
         /// </summary>
@@ -59,6 +91,35 @@ namespace Masuit.Tools.Files
             });
         }
 
+        /// <summary>
+        /// 压缩多个文件
+        /// </summary>
+        /// <param name="streams">多个文件流</param>
+        /// <param name="zipFile">压缩到...</param>
+        /// <param name="archiveType"></param>
+        /// <param name="disposeAllStreams">是否需要释放所有流</param>
+        public static void Zip(DisposeableDictionary<string, Stream> streams, string zipFile, ArchiveType archiveType = ArchiveType.Zip, bool disposeAllStreams = false)
+        {
+            using var archive = ArchiveFactory.Create(archiveType);
+            foreach (var pair in streams)
+            {
+                archive.AddEntry(pair.Key, pair.Value, true);
+            }
+
+            archive.SaveTo(zipFile, new WriterOptions(CompressionType.LZMA)
+            {
+                LeaveStreamOpen = true,
+                ArchiveEncoding = new ArchiveEncoding()
+                {
+                    Default = Encoding.UTF8
+                }
+            });
+            if (disposeAllStreams)
+            {
+                streams.Dispose();
+            }
+        }
+
         /// <summary>
         /// 解压文件,自动检测压缩包类型
         /// </summary>

+ 3 - 0
Masuit.Tools/Masuit.Tools.csproj

@@ -118,6 +118,9 @@
     <Compile Include="..\Masuit.Tools.Abstractions\Systems\DeserializeOnlyJsonPropertyAttribute.cs">
       <Link>Systems\DeserializeOnlyJsonPropertyAttribute.cs</Link>
     </Compile>
+    <Compile Include="..\Masuit.Tools.Abstractions\Systems\DisposeableDictionary.cs">
+      <Link>Systems\DisposeableDictionary.cs</Link>
+    </Compile>
     <Compile Include="..\Masuit.Tools.Abstractions\Systems\FallbackJsonProperty.cs">
       <Link>Systems\FallbackJsonProperty.cs</Link>
     </Compile>

+ 1 - 1
Masuit.Tools/package.nuspec

@@ -4,7 +4,7 @@
     <!--*-->
     <id>Masuit.Tools.Net</id>
     <!--*-->
-    <version>2.4.7.9</version>
+    <version>2.4.8</version>
     <title>Masuit.Tools</title>
     <!--*-->
     <authors>懒得勤快</authors>

+ 1 - 1
Test/Masuit.Tools.AspNetCore.ResumeFileResults.WebTest/Masuit.Tools.AspNetCore.ResumeFileResults.WebTest.csproj

@@ -23,7 +23,7 @@
   </ItemGroup>
 
   <ItemGroup>
-    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
+    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.0" />
   </ItemGroup>
 
   <ItemGroup>

+ 1 - 1
Test/Masuit.Tools.Core.Test/Masuit.Tools.Core.Test.csproj

@@ -9,7 +9,7 @@
   </PropertyGroup>
 
   <ItemGroup>
-    <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="6.0.2" />
+    <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="6.0.3" />
     <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
     <PackageReference Include="xunit" Version="2.4.1" />

+ 1 - 1
Test/Masuit.Tools.Test/Masuit.Tools.Test.csproj

@@ -105,7 +105,7 @@
       <Version>2.2.8</Version>
     </PackageReference>
     <PackageReference Include="NUnit">
-      <Version>3.13.2</Version>
+      <Version>3.13.3</Version>
     </PackageReference>
     <PackageReference Include="System.Runtime.CompilerServices.Unsafe">
       <Version>6.0.0</Version>