我想提取一個zip文件,它應該檢查zip文件中是否有文件夾。如果是,它應該提取到與其中的文件夾相同的文件夾。如果沒有,它應該採用zipfolder名稱。提取文件夾中的Zip文件
我的問題是,我總是得到一個異常,路徑的一部分找不到,但如果我打開zip文件在那裏。
我絕望!
private void CreateZipContentFolder(string zipsPath, string destinationPath) {
Zips = Directory.GetFiles(zipsPath, "*.zip", SearchOption.TopDirectoryOnly).ToList();
if (Zips.Count != 0) {
MyLog.WriteToLog("Creating Folder of ZipFiles... From: " + zipsPath + " To: " + destinationPath, MyLog.Messages.Info);
foreach (string zip in Zips) {
FileInfo fileInfo = new FileInfo(zip);
string dirName = destinationPath + "\\" + fileInfo.Name.Substring(0, fileInfo.Name.Length - 4);
using (ZipArchive archive = ZipFile.OpenRead(zip)) {
foreach (ZipArchiveEntry entry in archive.Entries) {
if (entry.FullName.EndsWith("/")) {
try {
ZipFile.ExtractToDirectory(zip, destinationPath);
} catch (IOException e) {
MyLog.WriteToLog(e.Message, MyLog.Messages.Error);
}
break;
} else if (new FileInfo(dirName).Exists == false) {
try {
Directory.CreateDirectory(dirName);
ZipFile.ExtractToDirectory(zip, dirName);
} catch (IOException e) {
MyLog.WriteToLog(e.Message, MyLog.Messages.Error);
}
break;
}
}
}
}
MyLog.WriteToLog("Created Temporary Folders", MyLog.Messages.Info);
} else { MyLog.WriteToLog("No Zips Found in: " + zipsPath, MyLog.Messages.Warning); }
}
更新:異常
System.IO.DirectoryNotFoundException了未處理的HResult = -2147024893
消息=找不到路徑的一部分「P:\ Documents_UBS_AM \ Projekte \ DataCompare \新建\包4 \ AssetPerformance.txt」。
源= mscorlib程序
您正在使用'FileInfo'來檢查目錄,當你應該使用'DirectoryInfo' – Nkosi