diff --git a/SharpCompressStudy.Core/CommonUtility.cs b/SharpCompressStudy.Core/CommonUtility.cs
index 6aa1498..3abe06e 100644
--- a/SharpCompressStudy.Core/CommonUtility.cs
+++ b/SharpCompressStudy.Core/CommonUtility.cs
@@ -19,6 +19,7 @@ using SharpCompress.Archives.Rar;
using SharpCompress.Archives.GZip;
using SharpCompress.Archives.Tar;
using SharpCompress.Archives.SevenZip;
+using SharpCompress.Readers;
namespace SharpCompressStudy.Core
{
@@ -62,17 +63,33 @@ namespace SharpCompressStudy.Core
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))
+ {
+ Directory.CreateDirectory(descDir);
+ }
+
try
{
- var extName = Path.GetExtension(fileName).Trim().ToLower();
- switch (extName)
+ using (Stream stream = File.OpenRead(fileName))
{
- case ".rar":
- break;
- default:
- break;
+ 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);
diff --git a/SharpCompressStudy.Core/Resource/å¦ä¹ 2.rar b/SharpCompressStudy.Core/Resource/å¦ä¹ 2.rar
new file mode 100644
index 0000000..fda1e43
Binary files /dev/null and b/SharpCompressStudy.Core/Resource/å¦ä¹ 2.rar differ
diff --git a/SharpCompressStudy.Core/SharpCompressStudy.Core.csproj b/SharpCompressStudy.Core/SharpCompressStudy.Core.csproj
index d239a34..c649f9d 100644
--- a/SharpCompressStudy.Core/SharpCompressStudy.Core.csproj
+++ b/SharpCompressStudy.Core/SharpCompressStudy.Core.csproj
@@ -17,6 +17,9 @@
PreserveNewest
+
+ PreserveNewest
+
diff --git a/SharpCompressStudy.Test/CommonUtilityTest.cs b/SharpCompressStudy.Test/CommonUtilityTest.cs
index c68ec2b..1e939e5 100644
--- a/SharpCompressStudy.Test/CommonUtilityTest.cs
+++ b/SharpCompressStudy.Test/CommonUtilityTest.cs
@@ -3,10 +3,17 @@ namespace SharpCompressStudy
public class CommonUtilityTest
{
[Fact]
- public void Test1()
+ public void Zip_Test()
{
var file = AppDomain.CurrentDomain.BaseDirectory + "/Resource/" + "Àû¸ÛÄ£ÐÍABÎļþ.zip";
CommonUtility.Decompress(file,false);
}
+
+ [Fact]
+ public void Rar_Test()
+ {
+ var file = AppDomain.CurrentDomain.BaseDirectory + "/Resource/" + "ѧϰ.rar";
+ CommonUtility.Decompress(file, false);
+ }
}
}
\ No newline at end of file
diff --git a/SharpCompressStudy.Test/WinRarFileTest.cs b/SharpCompressStudy.Test/WinRarFileTest.cs
index dfcac3b..e1d2e23 100644
--- a/SharpCompressStudy.Test/WinRarFileTest.cs
+++ b/SharpCompressStudy.Test/WinRarFileTest.cs
@@ -20,15 +20,41 @@ namespace SharpCompressStudy.Core
[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))
+ var rarFilePath = AppDomain.CurrentDomain.BaseDirectory + "Resource\\ѧϰ2.rar";
+
+ if (!File.Exists(rarFilePath))
{
- Directory.CreateDirectory(extractPath);
+ 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)