2010-11-22 84 views
9

我使用ICSharpCode.SharpZipLib.Zip.FastZip zip文件的文件,但我卡在了一個問題:ICSharpCode.SharpZipLib.Zip.FastZip不荏苒在他們的文件名中的特殊字符

當我嘗試拉鍊有特殊字符的文件在它的文件名中,它不起作用。它在文件名中沒有特殊字符時起作用。

+0

當你說它不起作用,_what_不起作用?你可以請你發佈你的代碼和錯誤信息嗎? – 2010-11-22 13:10:49

+0

它不是壓縮該文件,我也沒有得到任何錯誤 – BreakHead 2010-11-22 13:12:40

+0

它沒有特殊字符的工作嗎? – 2010-11-22 13:14:47

回答

0

可能性1:您正在向正則表達式文件過濾器傳遞文件名。

可能性2:這些字符不是在zip文件允許的(或至少SharpZipLib是這樣認爲的)

6

我認爲你不能使用FastZip。您需要遍歷文件和自己添加條目規定:

entry.IsUnicodeText = true;

告訴SharpZipLib項是unicode。

string[] filenames = Directory.GetFiles(sTargetFolderPath); 

// Zip up the files - From SharpZipLib Demo Code 
using (ZipOutputStream s = new 
    ZipOutputStream(File.Create("MyZipFile.zip"))) 
{ 
    s.SetLevel(9); // 0-9, 9 being the highest compression 

    byte[] buffer = new byte[4096]; 

    foreach (string file in filenames) 
    { 
     ZipEntry entry = new ZipEntry(Path.GetFileName(file)); 

     entry.DateTime = DateTime.Now; 
     entry.IsUnicodeText = true; 
     s.PutNextEntry(entry); 

     using (FileStream fs = File.OpenRead(file)) 
     { 
      int sourceBytes; 
      do 
      { 
       sourceBytes = fs.Read(buffer, 0, buffer.Length); 

       s.Write(buffer, 0, sourceBytes); 

      } while (sourceBytes > 0); 
     } 
    } 
    s.Finish(); 
    s.Close(); 
} 
+0

+1000,Ey vaaaal,太好了,這正是我想要的,非常感謝。 – 2013-01-09 07:17:43

+0

@M_Mogharrabi如果其中一個答案已經真正解決了你的問題,你必須將其標記爲已接受。與Paaland一樣的解決方案(只是向上滾動),除了我更快:) – Salaros 2016-12-10 20:59:14

+0

2012年12月20日比2011年3月26日早嗎?爲什麼要重開這個古老的帖子? – Paaland 2016-12-11 09:24:33

0

嘗試從文件名中取出特殊字符,我將其替換。 您Filename.Replace("&", "&");

1

你必須下載並編譯最新版本SharpZipLib庫中的所以你可以使用

entry.IsUnicodeText = true; 

這裏是你的代碼段(略作修改):

FileInfo file = new FileInfo("input.ext"); 
using(var sw = new FileStream("output.zip", FileMode.OpenOrCreate, FileAccess.ReadWrite)) 
{ 
    using(var zipStream = new ZipOutputStream(sw)) 
    { 
     var entry = new ZipEntry(file.Name); 
     entry.IsUnicodeText = true; 
     zipStream.PutNextEntry(entry); 

     using (var reader = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 
     { 
      byte[] buffer = new byte[4096]; 
      int bytesRead; 
      while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0) 
      { 
       byte[] actual = new byte[bytesRead]; 
       Buffer.BlockCopy(buffer, 0, actual, 0, bytesRead); 
       zipStream.Write(actual, 0, actual.Length); 
      } 
     } 
    } 
} 
2

您可以繼續使用FastZip如果你願意,但你需要給它一個ZipEntryFactory創建ZipEntry s與IsUnicodeText = true

var zfe = new ZipEntryFactory { IsUnicodeText = true }; 
var fz = new FastZip { EntryFactory = zfe }; 
fz.CreateZip("out.zip", "C:\in", true, null);