添加项目

main
wanggaofeng 8 months ago
parent d698947698
commit 9578aa7fb8

@ -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
{
/// <summary>
/// 解压文件
/// </summary>
/// <param name="fileName">压缩文件包</param>
/// <param name="isDelete">解压后删除压缩文件</param>
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;
}
}
}
}

@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SharpCompress" Version="0.36.0" />
</ItemGroup>
<ItemGroup>
<None Update="Resource\利港模型AB文件.zip">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resource\学习.rar">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

@ -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);
}
}
}

@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SharpCompressStudy.Core\SharpCompressStudy.Core.csproj" />
</ItemGroup>
</Project>

@ -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;

@ -0,0 +1,65 @@
namespace SharpCompressStudy.Core
{
/// <summary>
/// 因为Rar压缩算法是私有的而解压文件算法是公开的。
/// 所以不能创建Rar压缩文件但能解压现有Rar文件。
/// 能解压和创建zip、tar、7z等压缩格式的文件
/// </summary>
public class WinRarFileTest:IDisposable
{
private readonly ITestOutputHelper testOutput;
public WinRarFileTest(ITestOutputHelper testOutputHelper)
{
this.testOutput = testOutputHelper;
}
/// <summary>
/// 解压Rar文件
/// 注意不能创建Rar压缩文件
/// </summary>
[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);
}
}
}

@ -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
Loading…
Cancel
Save