I want to extract a zip file and it should check if the zip file has a folder in it. If yes it should extract to the folder called same as the folder in it. and if not it should take the zipfolder name.
My problem is that i always get an exception that a part of a path could not be find but if i open the zip the file is in there.
I despair!
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); }
}
Update: the exception
System.IO.DirectoryNotFoundException was unhandled HResult=-2147024893
Message=Could not find a part of the path 'P:\Documents_UBS_AM\Projekte\DataCompare\New\package4\AssetPerformance.txt'.
Source=mscorlib
FileInfoto check for Directory when you should be usingDirectoryInfo- Nkosi