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

时间:2015-06-23 16:25:09

标签: c#

我,或者更好,我们有一个大问题! 我们最喜欢的游戏Trove改变了它的结构,使其更具可修改性。现在我们坐在我们的ModLoader上,不知道如何更新它。它应该保留旧结构,但它需要将文件夹应用于拉链根目录中的每个现有文件夹。让我告诉你:

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

它必须被提取到每个Live / blueprints / override / ...和Live /粒子/覆盖/ ...(这只是一个例子......在一个真正的mod中,有更多的文件夹和更多的文件)

我该怎么做? Ionic可以吗?或者我是否必须使用其他库?

1 个答案:

答案 0 :(得分:0)

当然,如果我明白你的需要,这样的事情应该有用。

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