浏览代码

修正压缩bug

懒得勤快 6 年之前
父节点
当前提交
7c6a71d872

+ 1 - 3
Masuit.Tools.Core/AspNetCore/Extensions/ServiceCollectionExtensions.cs

@@ -30,13 +30,11 @@ namespace Masuit.Tools.AspNetCore.ResumeFileResults.Extensions
         /// 注入7z压缩
         /// 注入7z压缩
         /// </summary>
         /// </summary>
         /// <param name="services"></param>
         /// <param name="services"></param>
-        /// <param name="enableCache">是否启用缓存</param>
         /// <returns></returns>
         /// <returns></returns>
-        public static IServiceCollection AddSevenZipCompressor(this IServiceCollection services, bool enableCache = true)
+        public static IServiceCollection AddSevenZipCompressor(this IServiceCollection services)
         {
         {
             services.AddHttpClient();
             services.AddHttpClient();
             services.TryAddTransient<ISevenZipCompressor, SevenZipCompressor>();
             services.TryAddTransient<ISevenZipCompressor, SevenZipCompressor>();
-            SevenZipCompressor.EnableCache = enableCache;
             return services;
             return services;
         }
         }
     }
     }

+ 39 - 30
Masuit.Tools.Core/Files/Compress.cs

@@ -1,5 +1,4 @@
-using Microsoft.Extensions.Caching.Memory;
-using Microsoft.Win32;
+using Microsoft.Win32;
 using SharpCompress.Archives;
 using SharpCompress.Archives;
 using SharpCompress.Archives.Rar;
 using SharpCompress.Archives.Rar;
 using SharpCompress.Archives.Zip;
 using SharpCompress.Archives.Zip;
@@ -14,6 +13,7 @@ using System.Linq;
 using System.Net.Http;
 using System.Net.Http;
 using System.Text;
 using System.Text;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
+using System.Web;
 
 
 namespace Masuit.Tools.Files
 namespace Masuit.Tools.Files
 {
 {
@@ -23,8 +23,6 @@ namespace Masuit.Tools.Files
     public class SevenZipCompressor : ISevenZipCompressor
     public class SevenZipCompressor : ISevenZipCompressor
     {
     {
         private readonly HttpClient _httpClient;
         private readonly HttpClient _httpClient;
-        private static readonly MemoryCache _memoryCache = new MemoryCache(new MemoryCacheOptions());
-        internal static bool EnableCache { get; set; }
 
 
         /// <summary>
         /// <summary>
         /// 
         /// 
@@ -91,6 +89,7 @@ namespace Masuit.Tools.Files
             {
             {
                 dir = Path.GetDirectoryName(rar);
                 dir = Path.GetDirectoryName(rar);
             }
             }
+
             using (var archive = RarArchive.Open(rar))
             using (var archive = RarArchive.Open(rar))
             {
             {
                 var entries = ignoreEmptyDir ? archive.Entries.Where(entry => !entry.IsDirectory) : archive.Entries;
                 var entries = ignoreEmptyDir ? archive.Entries.Where(entry => !entry.IsDirectory) : archive.Entries;
@@ -117,6 +116,7 @@ namespace Masuit.Tools.Files
             {
             {
                 dir = Path.GetDirectoryName(compressedFile);
                 dir = Path.GetDirectoryName(compressedFile);
             }
             }
+
             using (Stream stream = File.OpenRead(compressedFile))
             using (Stream stream = File.OpenRead(compressedFile))
             {
             {
                 using (var reader = ReaderFactory.Open(stream))
                 using (var reader = ReaderFactory.Open(stream))
@@ -157,43 +157,51 @@ namespace Masuit.Tools.Files
         {
         {
             var archive = ZipArchive.Create();
             var archive = ZipArchive.Create();
             var dic = GetFileEntryMaps(files);
             var dic = GetFileEntryMaps(files);
-            var remoteFiles = files.Where(s => s.StartsWith("http")).ToList();
+            var remoteUrls = files.Distinct().Where(s => s.StartsWith("http")).Select(s =>
+            {
+                try
+                {
+                    return new Uri(s);
+                }
+                catch (UriFormatException)
+                {
+                    return null;
+                }
+            }).Where(u => u != null).ToList();
             foreach (var fileEntry in dic)
             foreach (var fileEntry in dic)
             {
             {
                 archive.AddEntry(Path.Combine(rootdir, fileEntry.Value), fileEntry.Key);
                 archive.AddEntry(Path.Combine(rootdir, fileEntry.Value), fileEntry.Key);
             }
             }
-            if (remoteFiles.Any())
+
+            if (remoteUrls.Any())
             {
             {
-                //var paths = remoteFiles.Select(s => new Uri(s).PathAndQuery).ToList();
-                //string pathname = new string(paths.First().Substring(0, paths.Min(s => s.Length)).TakeWhile((c, i) => paths.All(s => s[i] == c)).ToArray());
-                //Dictionary<string, string> pathDic = paths.ToDictionary(s => s, s => s.Substring(pathname.Length));
-                Parallel.ForEach(remoteFiles, url =>
+                //var dicList = remoteUrls.GroupBy(u => u.Authority).Select(g =>
+                //{
+                //    if (g.Count() > 1)
+                //    {
+                //        string pathname = new string(g.First().AbsolutePath.Substring(0, g.Min(s => s.AbsolutePath.Length)).TakeWhile((c, i) => g.All(s => s.AbsolutePath[i] == c)).ToArray());
+                //        return g.ToDictionary(s => s, s => HttpUtility.UrlDecode(s.AbsolutePath.Substring(pathname.Length)));
+                //    }
+                //    return g.ToDictionary(s => s, s => Path.GetFileName(HttpUtility.UrlDecode(s.AbsolutePath)));
+                //}).SelectMany(d => d).ToDictionary(x => x.Key, x => x.Value);
+                Parallel.ForEach(remoteUrls, url =>
                 {
                 {
-                    if (_memoryCache.TryGetValue(url, out Stream ms))
+                    _httpClient.GetAsync(url).ContinueWith(async t =>
                     {
                     {
-                        archive.AddEntry(Path.Combine(rootdir, Path.GetFileName(new Uri(url).AbsolutePath.Trim('/'))), ms);
-                    }
-                    else
-                    {
-                        _httpClient.GetAsync(url).ContinueWith(async t =>
+                        if (t.IsCompleted)
                         {
                         {
-                            if (t.IsCompleted)
+                            var res = await t;
+                            if (res.IsSuccessStatusCode)
                             {
                             {
-                                var res = await t;
-                                if (res.IsSuccessStatusCode)
-                                {
-                                    Stream stream = await res.Content.ReadAsStreamAsync();
-                                    archive.AddEntry(Path.Combine(rootdir, Path.GetFileName(new Uri(url).AbsolutePath.Trim('/'))), stream);
-                                    if (EnableCache)
-                                    {
-                                        _memoryCache.Set(url, stream, TimeSpan.FromMinutes(10));
-                                    }
-                                }
+                                Stream stream = await res.Content.ReadAsStreamAsync();
+                                //archive.AddEntry(Path.Combine(rootdir, pathDic[new Uri(url).AbsolutePath.Trim('/')]), stream);
+                                archive.AddEntry(Path.Combine(rootdir, Path.GetFileName(HttpUtility.UrlDecode(url.AbsolutePath))), stream);
                             }
                             }
-                        }).Wait();
-                    }
+                        }
+                    }).Wait();
                 });
                 });
             }
             }
+
             return archive;
             return archive;
         }
         }
 
 
@@ -236,11 +244,13 @@ namespace Masuit.Tools.Files
             {
             {
                 return new Dictionary<string, string>();
                 return new Dictionary<string, string>();
             }
             }
+
             string dirname = new string(fileList.First().Substring(0, fileList.Min(s => s.Length)).TakeWhile((c, i) => fileList.All(s => s[i] == c)).ToArray());
             string dirname = new string(fileList.First().Substring(0, fileList.Min(s => s.Length)).TakeWhile((c, i) => fileList.All(s => s[i] == c)).ToArray());
             Dictionary<string, string> dic = fileList.ToDictionary(s => s, s => s.Substring(dirname.Length));
             Dictionary<string, string> dic = fileList.ToDictionary(s => s, s => s.Substring(dirname.Length));
             return dic;
             return dic;
         }
         }
     }
     }
+
     /// <summary>
     /// <summary>
     /// SharpZip
     /// SharpZip
     /// </summary>
     /// </summary>
@@ -284,7 +294,6 @@ namespace Masuit.Tools.Files
         public static bool UnpackFiles(string file, string dir)
         public static bool UnpackFiles(string file, string dir)
         {
         {
             throw new NotImplementedException("该方法已过时,请使用SevenZipCompressor.Decompress方法替代");
             throw new NotImplementedException("该方法已过时,请使用SevenZipCompressor.Decompress方法替代");
-
         }
         }
 
 
         /// <summary>
         /// <summary>

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

@@ -2,7 +2,7 @@
 
 
   <PropertyGroup>
   <PropertyGroup>
     <TargetFramework>netcoreapp2.1</TargetFramework>
     <TargetFramework>netcoreapp2.1</TargetFramework>
-    <Version>2.2.3.1</Version>
+    <Version>2.2.3.3</Version>
     <Authors>懒得勤快</Authors>
     <Authors>懒得勤快</Authors>
     <Company>masuit.com</Company>
     <Company>masuit.com</Company>
     <Description>包含一些常用的操作类,大都是静态类,加密解密,反射操作,硬件信息,字符串扩展方法,日期时间扩展操作,大文件拷贝,图像裁剪,html处理,验证码、NoSql等常用封装。
     <Description>包含一些常用的操作类,大都是静态类,加密解密,反射操作,硬件信息,字符串扩展方法,日期时间扩展操作,大文件拷贝,图像裁剪,html处理,验证码、NoSql等常用封装。

+ 2 - 0
Masuit.Tools.UnitTest/CompressTest.cs

@@ -16,6 +16,7 @@ namespace Masuit.Tools.UnitTest
             {
             {
                 //AppContext.BaseDirectory,
                 //AppContext.BaseDirectory,
                 "http://ww3.sinaimg.cn/large/87c01ec7gy1fsq6rywto2j20je0d3td0.jpg",
                 "http://ww3.sinaimg.cn/large/87c01ec7gy1fsq6rywto2j20je0d3td0.jpg",
+                "http://192.168.16.150:3000string",
             });
             });
             Assert.True(ms.Length > 0);
             Assert.True(ms.Length > 0);
         }
         }
@@ -28,6 +29,7 @@ namespace Masuit.Tools.UnitTest
             {
             {
                 AppContext.BaseDirectory,
                 AppContext.BaseDirectory,
                 "http://ww3.sinaimg.cn/large/87c01ec7gy1fsq6rywto2j20je0d3td0.jpg",
                 "http://ww3.sinaimg.cn/large/87c01ec7gy1fsq6rywto2j20je0d3td0.jpg",
+                "http://192.168.16.150:3000string",
             }, zip);
             }, zip);
             using (FileStream stream = File.OpenRead(zip))
             using (FileStream stream = File.OpenRead(zip))
             {
             {

+ 30 - 8
Masuit.Tools/Files/Compress.cs

@@ -13,6 +13,7 @@ using System.Linq;
 using System.Net.Http;
 using System.Net.Http;
 using System.Text;
 using System.Text;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
+using System.Web;
 
 
 namespace Masuit.Tools.Files
 namespace Masuit.Tools.Files
 {
 {
@@ -77,6 +78,7 @@ namespace Masuit.Tools.Files
             {
             {
                 dir = Path.GetDirectoryName(rar);
                 dir = Path.GetDirectoryName(rar);
             }
             }
+
             using (var archive = RarArchive.Open(rar))
             using (var archive = RarArchive.Open(rar))
             {
             {
                 var entries = ignoreEmptyDir ? archive.Entries.Where(entry => !entry.IsDirectory) : archive.Entries;
                 var entries = ignoreEmptyDir ? archive.Entries.Where(entry => !entry.IsDirectory) : archive.Entries;
@@ -103,6 +105,7 @@ namespace Masuit.Tools.Files
             {
             {
                 dir = Path.GetDirectoryName(compressedFile);
                 dir = Path.GetDirectoryName(compressedFile);
             }
             }
+
             using (Stream stream = File.OpenRead(compressedFile))
             using (Stream stream = File.OpenRead(compressedFile))
             {
             {
                 using (var reader = ReaderFactory.Open(stream))
                 using (var reader = ReaderFactory.Open(stream))
@@ -143,19 +146,36 @@ namespace Masuit.Tools.Files
         {
         {
             var archive = ZipArchive.Create();
             var archive = ZipArchive.Create();
             var dic = GetFileEntryMaps(files);
             var dic = GetFileEntryMaps(files);
-            var remoteFiles = files.Where(s => s.StartsWith("http")).ToList();
+            var remoteUrls = files.Distinct().Where(s => s.StartsWith("http")).Select(s =>
+            {
+                try
+                {
+                    return new Uri(s);
+                }
+                catch (UriFormatException)
+                {
+                    return null;
+                }
+            }).Where(u => u != null).ToList();
             foreach (var fileEntry in dic)
             foreach (var fileEntry in dic)
             {
             {
                 archive.AddEntry(Path.Combine(rootdir, fileEntry.Value), fileEntry.Key);
                 archive.AddEntry(Path.Combine(rootdir, fileEntry.Value), fileEntry.Key);
             }
             }
-            if (remoteFiles.Any())
+
+            if (remoteUrls.Any())
             {
             {
-                //var paths = remoteFiles.Select(s => new Uri(s).PathAndQuery).ToList();
-                //string pathname = new string(paths.First().Substring(0, paths.Min(s => s.Length)).TakeWhile((c, i) => paths.All(s => s[i] == c)).ToArray());
-                //Dictionary<string, string> pathDic = paths.ToDictionary(s => s, s => s.Substring(pathname.Length));
+                //var dicList = remoteUrls.GroupBy(u => u.Authority).Select(g =>
+                //{
+                //    if (g.Count() > 1)
+                //    {
+                //        string pathname = new string(g.First().AbsolutePath.Substring(0, g.Min(s => s.AbsolutePath.Length)).TakeWhile((c, i) => g.All(s => s.AbsolutePath[i] == c)).ToArray());
+                //        return g.ToDictionary(s => s, s => HttpUtility.UrlDecode(s.AbsolutePath.Substring(pathname.Length)));
+                //    }
+                //    return g.ToDictionary(s => s, s => Path.GetFileName(HttpUtility.UrlDecode(s.AbsolutePath)));
+                //}).SelectMany(d => d).ToDictionary(x => x.Key, x => x.Value);
                 using (var httpClient = new HttpClient())
                 using (var httpClient = new HttpClient())
                 {
                 {
-                    Parallel.ForEach(remoteFiles, url =>
+                    Parallel.ForEach(remoteUrls, url =>
                     {
                     {
                         httpClient.GetAsync(url).ContinueWith(async t =>
                         httpClient.GetAsync(url).ContinueWith(async t =>
                         {
                         {
@@ -166,13 +186,14 @@ namespace Masuit.Tools.Files
                                 {
                                 {
                                     Stream stream = await res.Content.ReadAsStreamAsync();
                                     Stream stream = await res.Content.ReadAsStreamAsync();
                                     //archive.AddEntry(Path.Combine(rootdir, pathDic[new Uri(url).AbsolutePath.Trim('/')]), stream);
                                     //archive.AddEntry(Path.Combine(rootdir, pathDic[new Uri(url).AbsolutePath.Trim('/')]), stream);
-                                    archive.AddEntry(Path.Combine(rootdir, Path.GetFileName(new Uri(url).AbsolutePath.Trim('/'))), stream);
+                                    archive.AddEntry(Path.Combine(rootdir, Path.GetFileName(HttpUtility.UrlDecode(url.AbsolutePath))), stream);
                                 }
                                 }
                             }
                             }
                         }).Wait();
                         }).Wait();
                     });
                     });
                 }
                 }
             }
             }
+
             return archive;
             return archive;
         }
         }
 
 
@@ -215,11 +236,13 @@ namespace Masuit.Tools.Files
             {
             {
                 return new Dictionary<string, string>();
                 return new Dictionary<string, string>();
             }
             }
+
             string dirname = new string(fileList.First().Substring(0, fileList.Min(s => s.Length)).TakeWhile((c, i) => fileList.All(s => s[i] == c)).ToArray());
             string dirname = new string(fileList.First().Substring(0, fileList.Min(s => s.Length)).TakeWhile((c, i) => fileList.All(s => s[i] == c)).ToArray());
             Dictionary<string, string> dic = fileList.ToDictionary(s => s, s => s.Substring(dirname.Length));
             Dictionary<string, string> dic = fileList.ToDictionary(s => s, s => s.Substring(dirname.Length));
             return dic;
             return dic;
         }
         }
     }
     }
+
     /// <summary>
     /// <summary>
     /// SharpZip
     /// SharpZip
     /// </summary>
     /// </summary>
@@ -263,7 +286,6 @@ namespace Masuit.Tools.Files
         public static bool UnpackFiles(string file, string dir)
         public static bool UnpackFiles(string file, string dir)
         {
         {
             throw new NotImplementedException("该方法已过时,请使用SevenZipCompressor.Decompress方法替代");
             throw new NotImplementedException("该方法已过时,请使用SevenZipCompressor.Decompress方法替代");
-
         }
         }
 
 
         /// <summary>
         /// <summary>

文件差异内容过多而无法显示
+ 6 - 2
Test/Program.cs


+ 4 - 0
Test/Test.csproj

@@ -36,6 +36,10 @@
     <CodeAnalysisRuleSet>..\..\..\Company\XiLife.NDC.WebAPI\XiLife.NDC.WebAPI\CodeRules\XiLife.ITC.Rules.CSharp.ruleset</CodeAnalysisRuleSet>
     <CodeAnalysisRuleSet>..\..\..\Company\XiLife.NDC.WebAPI\XiLife.NDC.WebAPI\CodeRules\XiLife.ITC.Rules.CSharp.ruleset</CodeAnalysisRuleSet>
   </PropertyGroup>
   </PropertyGroup>
   <ItemGroup>
   <ItemGroup>
+    <Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\newtonsoft.json\9.0.1\lib\netstandard1.0\Newtonsoft.Json.dll</HintPath>
+    </Reference>
     <Reference Include="SharpCompress, Version=0.22.0.0, Culture=neutral, PublicKeyToken=afb0a02973931d96, processorArchitecture=MSIL">
     <Reference Include="SharpCompress, Version=0.22.0.0, Culture=neutral, PublicKeyToken=afb0a02973931d96, processorArchitecture=MSIL">
       <HintPath>..\packages\SharpCompress.0.22.0\lib\net45\SharpCompress.dll</HintPath>
       <HintPath>..\packages\SharpCompress.0.22.0\lib\net45\SharpCompress.dll</HintPath>
     </Reference>
     </Reference>

部分文件因为文件数量过多而无法显示