diff --git a/SharpCompressStudy.Core/CommonUtility.cs b/SharpCompressStudy.Core/CommonUtility.cs new file mode 100644 index 0000000..6aa1498 --- /dev/null +++ b/SharpCompressStudy.Core/CommonUtility.cs @@ -0,0 +1,171 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO.Compression; +using System.Linq; +using System.Text; +using System.Text.Unicode; +using System.Threading.Tasks; + + +using SharpCompress.Common; +using SharpCompress.Factories; +using SharpCompress.Writers; +using SharpCompress.Compressors; + +using SharpCompress.Archives; +using SharpCompress.Archives.Zip; +using SharpCompress.Archives.Rar; +using SharpCompress.Archives.GZip; +using SharpCompress.Archives.Tar; +using SharpCompress.Archives.SevenZip; + +namespace SharpCompressStudy.Core +{ + public static class CommonUtility + { + /// + /// 解压文件 + /// + /// 压缩文件包 + /// 解压后删除压缩文件 + public static void Decompress(string fileName, bool isDelete) + { + var extName = Path.GetExtension(fileName).Trim().ToLower(); + switch (extName) + { + case ".rar": + DecompressRarFile(fileName, isDelete); + break; + + case ".zip": + DecompressZipFile(fileName, isDelete); + break; + + case ".gzip": + DecompressGzipFile(fileName, isDelete); + break; + + case ".gz": + DecompressGzFile(fileName, isDelete); + break; + + default: + break; + } + + if (isDelete) + { + File.Delete(fileName); + } + } + + private static void DecompressRarFile(string fileName, bool isDelete) + { + try + { + var extName = Path.GetExtension(fileName).Trim().ToLower(); + switch (extName) + { + case ".rar": + break; + default: + break; + } + + if (isDelete) + { + File.Delete(fileName); + } + + } + catch (Exception ex) + { + Console.Write($"解压报错,错误:{ex}"); + throw; + } + } + + private static void DecompressZipFile(string fileName, bool isDelete) + { + try + { + ExtractionOptions options = new ExtractionOptions(); + + //抽取顶级目录 + var extractPath = Directory.GetParent(fileName)?.FullName + "22"; + if (!Directory.Exists(extractPath)) + { + Directory.CreateDirectory(extractPath); + } + + + var archive = ArchiveFactory.Open(fileName); + foreach (var entry in archive.Entries) + { + //加密的 + if (entry.IsEncrypted) + { + continue; + } + + if (entry.IsDirectory) + { + + } + else + { + entry.WriteToDirectory(extractPath, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }); + } + } + + + + if (isDelete) + { + File.Delete(fileName); + } + + } + catch (Exception ex) + { + Console.Write($"解压报错,错误:{ex.Message}"); + throw; + } + } + + private static void DecompressGzipFile(string fileName, bool isDelete) + { + try + { + + if (isDelete) + { + File.Delete(fileName); + } + + } + catch (Exception ex) + { + Console.Write($"解压报错,错误:{ex}"); + throw; + } + } + + private static void DecompressGzFile(string fileName, bool isDelete) + { + try + { + if (isDelete) + { + File.Delete(fileName); + } + } + catch (Exception ex) + { + Console.Write($"解压报错,错误:{ex}"); + throw; + } + } + } +} diff --git a/SharpCompressStudy.Core/Resource/利港模型AB文件.zip b/SharpCompressStudy.Core/Resource/利港模型AB文件.zip new file mode 100644 index 0000000..c1a5f62 Binary files /dev/null and b/SharpCompressStudy.Core/Resource/利港模型AB文件.zip differ diff --git a/SharpCompressStudy.Core/Resource/学习.rar b/SharpCompressStudy.Core/Resource/学习.rar new file mode 100644 index 0000000..bc10319 Binary files /dev/null and b/SharpCompressStudy.Core/Resource/学习.rar differ diff --git a/SharpCompressStudy.Core/SharpCompressStudy.Core.csproj b/SharpCompressStudy.Core/SharpCompressStudy.Core.csproj new file mode 100644 index 0000000..d239a34 --- /dev/null +++ b/SharpCompressStudy.Core/SharpCompressStudy.Core.csproj @@ -0,0 +1,22 @@ + + + + net6.0 + enable + enable + + + + + + + + + PreserveNewest + + + PreserveNewest + + + + diff --git a/SharpCompressStudy.Test/CommonUtilityTest.cs b/SharpCompressStudy.Test/CommonUtilityTest.cs new file mode 100644 index 0000000..c68ec2b --- /dev/null +++ b/SharpCompressStudy.Test/CommonUtilityTest.cs @@ -0,0 +1,12 @@ +namespace SharpCompressStudy +{ + public class CommonUtilityTest + { + [Fact] + public void Test1() + { + var file = AppDomain.CurrentDomain.BaseDirectory + "/Resource/" + "ģABļ.zip"; + CommonUtility.Decompress(file,false); + } + } +} \ No newline at end of file diff --git a/SharpCompressStudy.Test/SharpCompressStudy.Test.csproj b/SharpCompressStudy.Test/SharpCompressStudy.Test.csproj new file mode 100644 index 0000000..66178e5 --- /dev/null +++ b/SharpCompressStudy.Test/SharpCompressStudy.Test.csproj @@ -0,0 +1,28 @@ + + + + net6.0 + enable + enable + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/SharpCompressStudy.Test/Usings.cs b/SharpCompressStudy.Test/Usings.cs new file mode 100644 index 0000000..7f06712 --- /dev/null +++ b/SharpCompressStudy.Test/Usings.cs @@ -0,0 +1,20 @@ +global using Xunit; +global using Xunit.Sdk; +global using Xunit.Extensions; +global using Xunit.Abstractions; + +global using SharpCompress; +global using SharpCompress.Common; +global using SharpCompress.Compressors; +global using SharpCompress.Factories; +global using SharpCompress.IO; +global using SharpCompress.Readers; +global using SharpCompress.Writers; +global using SharpCompress.Archives; +global using SharpCompress.Archives.Rar; +global using SharpCompress.Archives.Tar; +global using SharpCompress.Archives.Zip; +global using SharpCompress.Archives.GZip; +global using SharpCompress.Archives.SevenZip; + +global using SharpCompressStudy.Core; \ No newline at end of file diff --git a/SharpCompressStudy.Test/WinRarFileTest.cs b/SharpCompressStudy.Test/WinRarFileTest.cs new file mode 100644 index 0000000..dfcac3b --- /dev/null +++ b/SharpCompressStudy.Test/WinRarFileTest.cs @@ -0,0 +1,65 @@ +namespace SharpCompressStudy.Core +{ + /// + /// ΪRarѹ㷨˽еģѹļ㷨ǹġ + /// ԣܴRarѹļܽѹRarļ + /// ܽѹʹziptar7zѹʽļ + /// + public class WinRarFileTest:IDisposable + { + private readonly ITestOutputHelper testOutput; + public WinRarFileTest(ITestOutputHelper testOutputHelper) + { + this.testOutput = testOutputHelper; + } + + /// + /// ѹRarļ + /// ע⣺ܴRarѹļ + /// + [Fact] + public void ExtractFromRar_Test() + { + var rarFilePath = AppDomain.CurrentDomain.BaseDirectory + "Resource\\ѧϰ.rar"; + + var extractPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resource\\", Guid.NewGuid().ToString() + "\\"); + if (!Directory.Exists(extractPath)) + { + Directory.CreateDirectory(extractPath); + } + using (var archive = RarArchive.Open(rarFilePath)) + { + foreach (var entry in archive.Entries) + { + if (!entry.IsDirectory) + { + entry.WriteToDirectory(extractPath, new ExtractionOptions { ExtractFullPath = true, Overwrite = true }); + } + } + } + + testOutput.WriteLine($"ļѹĿ¼{extractPath}"); + } + + [Fact] + public void CompressToZip_Test() + { + string filesPath = AppDomain.CurrentDomain.BaseDirectory + "Resource\\ѧϰ"; + var extractPathFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resource\\", Guid.NewGuid().ToString() + "\\"); + var extractPathFile = Path.Combine(extractPathFolder, "ѧϰ.zip"); + if (!Directory.Exists(extractPathFolder)) + { + Directory.CreateDirectory(extractPathFolder); + } + + using var zip = File.OpenWrite(extractPathFile); + using var zipWriter = WriterFactory.Open(zip, ArchiveType.Zip, CompressionType.Deflate); + zipWriter.WriteAll(filesPath, "*", SearchOption.AllDirectories); + } + + public void Dispose() + { + GC.SuppressFinalize(this); + } + } +} \ No newline at end of file diff --git a/SharpCompressStudy.sln b/SharpCompressStudy.sln new file mode 100644 index 0000000..96f4524 --- /dev/null +++ b/SharpCompressStudy.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34322.80 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpCompressStudy.Test", "SharpCompressStudy.Test\SharpCompressStudy.Test.csproj", "{3B9D118C-CB99-4063-8F7C-736738438646}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpCompressStudy.Core", "SharpCompressStudy.Core\SharpCompressStudy.Core.csproj", "{4F8EBAE8-BEBB-49EB-B2D4-1F90DAFACFCE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3B9D118C-CB99-4063-8F7C-736738438646}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3B9D118C-CB99-4063-8F7C-736738438646}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3B9D118C-CB99-4063-8F7C-736738438646}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3B9D118C-CB99-4063-8F7C-736738438646}.Release|Any CPU.Build.0 = Release|Any CPU + {4F8EBAE8-BEBB-49EB-B2D4-1F90DAFACFCE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4F8EBAE8-BEBB-49EB-B2D4-1F90DAFACFCE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4F8EBAE8-BEBB-49EB-B2D4-1F90DAFACFCE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4F8EBAE8-BEBB-49EB-B2D4-1F90DAFACFCE}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {EC626062-77C9-4194-8236-3681DEA85895} + EndGlobalSection +EndGlobal