ZoRo
ZoRo

Reputation: 421

c# zip file - Extract file last

Quick question: I need to extract zip file and have a certain file extract last.

More info: I know how to extract a zip file with c# (fw 4.5). 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.

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.

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?

Thanks.

Upvotes: 1

Views: 526

Answers (1)

oOo
oOo

Reputation: 130

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.

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);
        }
    }

Upvotes: 1

Related Questions