diff --git a/SharpCompressStudy.Core/CommonUtility.cs b/SharpCompressStudy.Core/CommonUtility.cs
index 3abe06e..a96a243 100644
--- a/SharpCompressStudy.Core/CommonUtility.cs
+++ b/SharpCompressStudy.Core/CommonUtility.cs
@@ -1,13 +1,12 @@
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;
using SharpCompress.Common;
using SharpCompress.Factories;
using SharpCompress.Writers;
@@ -21,168 +20,133 @@ using SharpCompress.Archives.Tar;
using SharpCompress.Archives.SevenZip;
using SharpCompress.Readers;
+
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);
+ if (string.IsNullOrWhiteSpace(fileName))
+ {
+ throw new ArgumentNullException($"{fileName}");
}
- }
-
- private static void DecompressRarFile(string fileName, bool isDelete)
- {
- ArgumentNullException.ThrowIfNull(fileName);
-
- var extName = Path.GetExtension(fileName);
- var descDir = Path.Combine(Path.GetDirectoryName(fileName)!, new FileInfo(fileName).Name.Replace(extName,"") + "\\");
-
- if (!Directory.Exists(descDir))
+ if (!File.Exists(fileName))
{
- Directory.CreateDirectory(descDir);
+ throw new FileNotFoundException($"{fileName} not found");
}
try
{
- using (Stream stream = File.OpenRead(fileName))
- {
- var reader = ReaderFactory.Open(stream);
- while (reader.MoveToNextEntry())
- {
- if (!reader.Entry.IsDirectory)
- {
- Console.WriteLine(reader.Entry.Key);
- reader.WriteEntryToDirectory(descDir, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true });
- }
- }
- }
-
-
- if (isDelete)
- {
- File.Delete(fileName);
- }
-
- }
- catch (Exception ex)
- {
- Console.Write($"解压报错,错误:{ex}");
- throw;
- }
- }
+ //扩展名
+ var extName = Path.GetExtension(fileName).Trim().ToLower();
- private static void DecompressZipFile(string fileName, bool isDelete)
- {
- try
- {
- ExtractionOptions options = new ExtractionOptions();
+ //文件名
+ var rarFileName = Path.GetFileName(fileName);
- //抽取顶级目录
- var extractPath = Directory.GetParent(fileName)?.FullName + "22";
- if (!Directory.Exists(extractPath))
- {
- Directory.CreateDirectory(extractPath);
- }
+ //不带扩展名的文件名
+ var rarFileNameWithoutExt = Path.GetFileNameWithoutExtension(fileName);
+ //解压根目录
+ string extractPath = Path.GetDirectoryName(fileName) ?? "";
- var archive = ArchiveFactory.Open(fileName);
- foreach (var entry in archive.Entries)
+ //解压文件
+ using (var archive = GetArchiveByExtName(fileName))
{
- //加密的
- if (entry.IsEncrypted)
+ //创建解压目录
+ if (!Directory.Exists(extractPath))
{
- continue;
+ Directory.CreateDirectory(extractPath);
}
- if (entry.IsDirectory)
+ //解压所有文件到指定目录
+ foreach (var entry in archive.Entries)
{
-
+ if (!entry.IsDirectory)
+ {
+ entry.WriteToDirectory(extractPath, new ExtractionOptions { ExtractFullPath = true, Overwrite = true });
+ }
}
- else
+
+ //如果整体解压到一个同名目录,则把同名目录内容提取到当前目录,并删除同名目录
+ if (archive.Entries.Where(f => f.IsDirectory && f.Key == rarFileNameWithoutExt).Count() == 1)
{
- entry.WriteToDirectory(extractPath, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true });
+ var dir = new DirectoryInfo(Path.Combine(extractPath, rarFileNameWithoutExt));
+ dir.GetDirectories().ForEach(f =>
+ {
+ var toDir = Path.Combine(extractPath, f.Name);
+ if (Directory.Exists(toDir))
+ {
+ Directory.Delete(toDir, true);
+ }
+ f.MoveTo(toDir);
+ });
+
+ dir.GetFiles().ForEach(f =>
+ {
+ f.MoveTo(Path.Combine(extractPath, f.Name), true);
+ });
+
+ dir.Delete();
}
}
-
-
- if (isDelete)
+ //删除压缩文件
+ if (isDelete && File.Exists(fileName))
{
File.Delete(fileName);
}
-
}
catch (Exception ex)
{
- Console.Write($"解压报错,错误:{ex.Message}");
+ Console.Write($"解压报错,错误:{ex}");
throw;
}
}
- private static void DecompressGzipFile(string fileName, bool isDelete)
+ ///
+ /// 按扩展名获取IArchive对象
+ ///
+ /// 压缩文件
+ /// IArchive对象
+ /// 压缩文件类型不支持异常
+ private static IArchive GetArchiveByExtName(string compressFileName)
{
- try
+ IArchive archive;
+ var extName = Path.GetExtension(compressFileName).Trim().ToLower();
+ switch (extName)
{
+ case ".rar":
+ archive = RarArchive.Open(compressFileName);
+ break;
- if (isDelete)
- {
- File.Delete(fileName);
- }
+ case ".zip":
+ archive = ZipArchive.Open(compressFileName);
+ break;
- }
- catch (Exception ex)
- {
- Console.Write($"解压报错,错误:{ex}");
- throw;
- }
- }
+ case ".7z":
+ archive = SevenZipArchive.Open(compressFileName);
+ break;
- private static void DecompressGzFile(string fileName, bool isDelete)
- {
- try
- {
- if (isDelete)
- {
- File.Delete(fileName);
- }
- }
- catch (Exception ex)
- {
- Console.Write($"解压报错,错误:{ex}");
- throw;
+ case ".gz":
+ archive = GZipArchive.Open(compressFileName);
+ break;
+ case ".tar":
+ archive = TarArchive.Open(compressFileName);
+ break;
+
+ default:
+ throw new ArchiveException($"不支持的压缩文件格式[{compressFileName}]");
}
+
+ return archive;
}
}
}
diff --git a/SharpCompressStudy.Core/Resource/利港模型AB文件.rar b/SharpCompressStudy.Core/Resource/利港模型AB文件.rar
new file mode 100644
index 0000000..00c2aa3
Binary files /dev/null and b/SharpCompressStudy.Core/Resource/利港模型AB文件.rar differ
diff --git a/SharpCompressStudy.Core/Resource/学习.rar b/SharpCompressStudy.Core/Resource/学习.rar
index bc10319..4057b5b 100644
Binary files a/SharpCompressStudy.Core/Resource/学习.rar and b/SharpCompressStudy.Core/Resource/学习.rar differ
diff --git a/SharpCompressStudy.Core/SharpCompressStudy.Core.csproj b/SharpCompressStudy.Core/SharpCompressStudy.Core.csproj
index c649f9d..0591245 100644
--- a/SharpCompressStudy.Core/SharpCompressStudy.Core.csproj
+++ b/SharpCompressStudy.Core/SharpCompressStudy.Core.csproj
@@ -11,6 +11,9 @@
+
+ PreserveNewest
+
PreserveNewest
diff --git a/SharpCompressStudy.Test/CommonUtilityTest.cs b/SharpCompressStudy.Test/CommonUtilityTest.cs
index 1e939e5..6627e03 100644
--- a/SharpCompressStudy.Test/CommonUtilityTest.cs
+++ b/SharpCompressStudy.Test/CommonUtilityTest.cs
@@ -6,7 +6,7 @@ namespace SharpCompressStudy
public void Zip_Test()
{
var file = AppDomain.CurrentDomain.BaseDirectory + "/Resource/" + "ģABļ.zip";
- CommonUtility.Decompress(file,false);
+ CommonUtility.Decompress(file,true);
}
[Fact]
diff --git a/SharpCompressStudy.Test/WinRarFileTest.cs b/SharpCompressStudy.Test/WinRarFileTest.cs
deleted file mode 100644
index e1d2e23..0000000
--- a/SharpCompressStudy.Test/WinRarFileTest.cs
+++ /dev/null
@@ -1,91 +0,0 @@
-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\\ѧϰ2.rar";
-
- if (!File.Exists(rarFilePath))
- {
- throw new FileNotFoundException("Rarļ");
- }
-
- //չ
- var extName = Path.GetExtension(rarFilePath);
-
- //ļ
- var rarFileName = Path.GetFileName(rarFilePath).Replace(extName, "");
-
- //չļ
- var rarFileNameWithoutExt = Path.GetFileNameWithoutExtension(rarFilePath);
-
- //ѹĿ¼
- var extractPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resource\\models\\temp\\", Guid.NewGuid().ToString() + "\\");
-
- using (var archive = RarArchive.Open(rarFilePath))
- {
- //ѹļǷͬĿ¼(abc.rarǷһΪabcĸĿ¼)
- if (archive.Entries.Where(f => f.IsDirectory && f.Key == rarFileNameWithoutExt).Count() != 1)
- {
- extractPath = Path.Combine(extractPath, rarFileNameWithoutExt);
- }
-
- //ѹĿ¼
- if (!Directory.Exists(extractPath))
- {
- Directory.CreateDirectory(extractPath);
- }
-
- //ѹļָĿ¼
- 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