简体   繁体   English

C#压缩文件-最后提取文件

[英]c# zip file - Extract file last

Quick question: I need to extract zip file and have a certain file extract last. 快速提问:我需要提取zip文件,最后提取某个文件。

More info: I know how to extract a zip file with c# (fw 4.5). 更多信息:我知道如何使用c#(fw 4.5)提取一个zip文件。 The problem I'm having now is that I have a zip file and inside it there is always a file name (for example) "myFlag.xml" and a few more files. 我现在遇到的问题是我有一个zip文件,并且里面始终有一个文件名(例如)“ myFlag.xml”和其他一些文件。

Since I need to support some old applications that listen to the folder I'm extracting to, I want to make sure that the XML file will always be extract the last. 由于我需要支持一些老应用程序,这些应用程序会监听我要提取到的文件夹,因此我想确保XML文件始终是最后提取的文件。

Is there some thing like "exclude" for the zip function that can extract all but a certain file so I can do that and then extract only the file alone? zip函数是否有诸如“ exclude”之类的东西,可以提取除某个文件以外的所有文件,所以我可以这样做,然后仅提取文件?

Thanks. 谢谢。

You could probably try a foreach loop on the ZipArchive , and exclude everything that doesn't match your parameters, then, after the loop is done, extract the last file. 您可能会在ZipArchive上尝试一个foreach循环,并排除所有与参数不匹配的内容,然后在循环完成后提取最后一个文件。

Something like this: 像这样:

    private void TestUnzip_Foreach()
    {
        using (ZipArchive z = ZipFile.Open("zipfile.zip", ZipArchiveMode.Read))
        {
            string LastFile = "lastFileName.ext";

            int curPos = 0;
            int lastFilePosition = 0;
            foreach (ZipArchiveEntry entry in z.Entries)
            {
                if (entry.Name != LastFile)
                {
                    entry.ExtractToFile(@"C:\somewhere\" + entry.FullName);
                }
                else
                {
                    lastFilePosition = curPos;
                }
                curPos++;
            }
            z.Entries[lastFilePosition].ExtractToFile(@"C:\somewhere_else\" + LastFile);
        }
    }

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

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