【问题标题】:Using Ionic Zip to extract a folder inside a zip, to a root of another directory (without the contained folder in the zip)使用 Ionic Zip 将 zip 中的文件夹提取到另一个目录的根目录(没有 zip 中包含的文件夹)
【发布时间】:2013-11-12 18:24:04
【问题描述】:

我试图让 Ionic zip 将 zip 文件中的文件夹提取到指定的根目录中。我的问题是里面的 zip 文件是“zipfile.zip\some_folder\”。我想将“some_folder”中的所有文件提取到另一个目录根目录。

这就是我所做的

using (ZipFile zip = ZipFile.Read("zipfile.zip"))
{
var selection = (from cf in zip.Entries
                 where (cf.FileName).StartsWith("some_folder/")
                 select cf);

foreach (var cf in selection)
    {
    try
        {
            cf.Extract(destinationPath, ExtractExistingFileAction.OverwriteSilently);
            progress.Value = progress.Value + 1;
        }
        catch (Exception ex)
        {
            installProblems = true;
        }

    }
}

上述代码的问题在于它也将自己的文件夹提取到“destinationPath\some_folder”中。我希望将“zipfile.zip\some_folder\”中的文件提取到“destinationPath\”中

希望您能提供帮助!

编辑:

所以我尝试了一些东西,并想出了一些可行的方法,但我不喜欢它:

using (ZipFile zip = ZipFile.Read("zipfile.zip"))
{
    var selection = (from cf in zip.Entries
                                 where (cf.FileName).StartsWith("some_folder/")
                                 select cf);

    selection.ToList().ForEach(entry =>
    {
        try
        {
            entry.FileName = entry.FileName.Substring(12);
            entry.Extract(minecraftPathVar, ExtractExistingFileAction.OverwriteSilently);
        }
        catch
        {
            installProblems = true;
        }

    });
}

上述方法有效,但我不认为这是一个好习惯,即使名称“some_folder”在任何时候都没有更改,我只是不喜欢这种方法..

【问题讨论】:

    标签: zip extract root subdirectory


    【解决方案1】:

    在您的第一行之后:使用 (ZipFile zip = ZipFile.Read("zipfile.zip"))

    尝试这样做: zip.FlattenFoldersOnExtract = true

    【讨论】:

    • 谢谢你,我正在寻找的东西
    猜你喜欢
    • 2013-10-22
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    相关资源
    最近更新 更多