0
votes

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

1
You are using FileInfo to check for Directory when you should be using DirectoryInfo - Nkosi

1 Answers

1
votes

I've made some tests for your code, but before - I've made needed refactoring. Your code really needs it. I've removed all "log" lines, but I think - you'll understand how to use it. It should work, but you used some very bad solutions for doing simple things and it could be the reason of your exception.

    private void CreateZipContentFolder(List<String> zips, string destinationPath)
    {
        if (zips.Any())
        {
            foreach (string zip in zips)
            {
                string dirName = Path.Combine(destinationPath, Path.GetFileNameWithoutExtension(zip));

                using (ZipArchive archive = ZipFile.OpenRead(zip))
                {
                    foreach (ZipArchiveEntry entry in archive.Entries)
                    {
                        if (entry.FullName.EndsWith("/"))
                        {
                            ZipFile.ExtractToDirectory(zip, destinationPath);
                            break;
                        }
                        else if (!Directory.Exists(dirName))
                        {
                            Directory.CreateDirectory(dirName);
                            ZipFile.ExtractToDirectory(zip, dirName);
                            break;
                        }
                    }
                }
            }
        }
    }

And also could you please provide something like "files' tree" if it does not start to work for you?