简体   繁体   English

提取文件夹中的Zip文件

[英]Extracting Zip File in a Folder

I want to extract a zip file and it should check if the zip file has a folder in it. 我想提取一个zip文件,它应该检查zip文件中是否有文件夹。 If yes it should extract to the folder called same as the folder in it. 如果是,它将解压缩到与其中的文件夹相同的文件夹。 and if not it should take the zipfolder name. 如果不是,则应使用zipfolder名称。

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. 我的问题是,我总是会遇到一个例外,即找不到路径的一部分,但是如果我打开zip文件,则文件在那里。

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 未处理System.IO.DirectoryNotFoundException HResult = -2147024893

Message=Could not find a part of the path 'P:\\Documents_UBS_AM\\Projekte\\DataCompare\\New\\package4\\AssetPerformance.txt'. 消息=找不到路径'P:\\ Documents_UBS_AM \\ Projekte \\ DataCompare \\ New \\ package4 \\ AssetPerformance.txt'的一部分。

Source=mscorlib 来源= mscorlib

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? 如果它对您没有帮助,您还可以提供“文件树”之类的内容吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM