2013-04-03 32 views
3

我在Mac上使用以下代碼使用Mono解壓zip文件。該zip文件包含目錄下的條目(例如foo/bar.txt)。但是,在解壓縮的目錄中,FastZip創建文件foo\bar.txt而不是使用文件bar.txt創建目錄foo。我如何解決這個問題?SharpZipLib的FastZip不在Mac上解壓縮目錄

FastZip fz = new FastZip(); 
string filePath = @"path\to\myfile.zip"; 

fz.ExtractZip(filePath, @"path\to\unzip\to", null); 

這就造成了path\to\unzip\to文件foo\bar.txt

回答

1

顯然不能對這種情況,所以我最後寫我自己的解壓機制使用FastZip

string filePath = @"path\to\myfile.zip"; 
string unzipDir = @"path\to\unzip\to"; 

using (var zipFile = new ZipFile(filePath)) 
{ 
    foreach (var zipEntry in zipFile.OfType<ZipEntry>()) 
    { 
     var unzipPath = Path.Combine(unzipDir, zipEntry.Name); 
     var directoryPath = Path.GetDirectoryName(unzipPath); 

     // create directory if needed 
     if (directoryPath.Length > 0) 
     { 
      Directory.CreateDirectory(directoryPath); 
     } 

     // unzip the file 
     var zipStream = zipFile.GetInputStream(zipEntry); 
     var buffer = new byte[4096]; 

     using (var unzippedFileStream = File.Create(unzipPath)) 
     { 
      StreamUtils.Copy(zipStream, unzippedFileStream, buffer); 
     } 
    } 
} 
-1

使用正斜槓分隔文件夾創建ZIP

+0

我們不控制拉鍊創作時。 – sohum

+0

你並不需要爲此降級......它可以幫助其他人控制zip文件。 –