2010-11-22 86 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);