简体   繁体   English

Ionic Zip:通过添加文件夹提取zip文件

[英]Ionic Zip: Extract zip file with folder addition

I, or better, we have a big problem! 我,或者更好,我们有一个大问题! Our favorite game, Trove, has changed it's structure to be more moddable. 我们最喜欢的游戏Trove已将其结构更改为可修改的。 Now we sit on our ModLoader and don't know how to update it. 现在我们坐在ModLoader上,不知道如何更新它。 It should keep the old structure but it needs to apply a folder to each existing folder in the zips root. 它应该保留旧的结构,但是需要将一个文件夹应用于zips根目录中的每个现有文件夹。 Let me show you: 让我演示给你看:

mod.zip
|-blueprints
| |-fileA
|
|-particles
| |-fileB

It has to be extracted to each Live/blueprints/override/... and Live/particles/override/... (this is just an example... in a real mod, there are more folders and more files) 必须将其提取到每个Live / blueprints / override / ...和Live / particles / override / ...(这只是一个示例……在实际的mod中,有更多的文件夹和更多的文件)

How can I do that? 我怎样才能做到这一点? Is it possible with Ionic? Ionic有可能吗? Or do I have to use an other library? 还是我必须使用其他图书馆?

Sure, something like this should work if I understood what you need. 当然,如果我了解您的需求,类似的方法应该可以使用。

private const string ZIP_FILE = @"c:\temp\mod.zip";

static void Main()
{
    // delete if already there
    File.Delete(ZIP_FILE);

    // create example zip file
    using(ZipFile zipToPack = new ZipFile(ZIP_FILE))
    {
        File.WriteAllText(@"c:\temp\fileA.txt", "This is file A");
        zipToPack.AddFile(@"c:\temp\fileA.txt", "blueprints");
        File.WriteAllText(@"c:\temp\fileB.txt", "This is file B");
        zipToPack.AddFile(@"c:\temp\fileB.txt", "particles");
        zipToPack.Save();
    }

    // extract inserting extra directory
    string baseExtractDir = @"c:\temp\LIVE";
    string overrideDir = "override";

    using (ZipFile zipToUnpack = new ZipFile(ZIP_FILE))
    {
        // loop through each entry in the zip file
        foreach (var zipEntry in zipToUnpack)
        {                    
            if (zipEntry.IsDirectory) continue;
            string zipDirectoryPath = Path.GetDirectoryName(zipEntry.FileName);
            // create the target file path as follows:
            // ("c:\temp\LIVE") + zip file path ("blueprints") + our extra path ("override") + zip file name ("fileA.txt")
            string targetExtractFile = Path.Combine(baseExtractDir, zipDirectoryPath, overrideDir, Path.GetFileName(zipEntry.FileName));
            // create the directory path if needed
            string targetExtractDir = Path.GetDirectoryName(targetExtractFile);
            if (!Directory.Exists(targetExtractDir)) Directory.CreateDirectory(targetExtractDir);
            // extract the zip file to our target file name
            using (FileStream stream = new FileStream(targetExtractFile, FileMode.Create))
                zipEntry.Extract(stream);
        }
    }

    Console.ReadKey();
}

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

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