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