Auto Extract a zip file

。_饼干妹妹 提交于 2019-12-11 18:49:24

问题


I'm trying make a program that extracts a specific zip file everytime the program launches.

this is my code to create the zip file:

//creating the file
ZipFile File = new ZipFile(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ABCD.zip");

//Adding files

File.AddFile(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ab.dat", "");
File.AddFile(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\cd.dat", "");

//Save the file
File.Save();

I want to Extract the files ab.dat and cd.dat from ABCD.zip to the .exe file directory automatically.

Thanks for helping.


回答1:


Taken mostly from the DotNetZip documentation:

private void Extract()
{
    //Zip Location
    string zipToUnpack = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ABCD.zip";
    // .EXE Directory
    string unpackDirectory = System.IO.Path.GetDirectoryName(
        System.Reflection.Assembly.GetExecutingAssembly().Location);

    using (ZipFile zip = ZipFile.Read(zipToUnpack))
    {
        foreach (ZipEntry e in zip)
        {
             //If filename matches
             if (e.FileName == "ab.dat" || e.FileName == "cd.dat")
                 e.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
        }
    }
}

You can also filter the results using ExtractSelectEntries by selecting the files there:

zip.ExtractSelectedEntries("name = 'ab.dat' OR name = 'cd.dat'", "\", unpackDirectory, ExtractExistingFileAction.OverwriteSilently)

Or selecting all .dat files with a wildcard

zip.ExtractSelectedEntries("name = '*.dat'", "\", unpackDirectory, ExtractExistingFileAction.OverwriteSilently)

Use each ZipEntry's FileName property to see if it has the name you would like to extract.



来源:https://stackoverflow.com/questions/24982165/auto-extract-a-zip-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!