當然,像這樣的東西應該工作,如果我明白你需要什麼。
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();
}
你想提取一個zip並保持目錄結構? –
好吧,有點...壓縮文件夾中的內容應該被提取到實際文件夾中的文件夾。所以在我的例子中,mod.zip/blueprints/fileA必須被提取到Live/blueprints/override/fileA並且mod.zip /particles/fileB必須被提取到Live/particles/override/fileB –