Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ICSharpCode.SharpZipLib.Zip.FastZip不压缩文件名中包含特殊字符的文件_C#_.net_Sharpziplib - Fatal编程技术网

C# ICSharpCode.SharpZipLib.Zip.FastZip不压缩文件名中包含特殊字符的文件

C# ICSharpCode.SharpZipLib.Zip.FastZip不压缩文件名中包含特殊字符的文件,c#,.net,sharpziplib,C#,.net,Sharpziplib,我正在使用ICSharpCode.SharpZipLib.Zip.FastZip压缩文件,但我遇到了一个问题: 当我试图压缩文件名中包含特殊字符的文件时,它不起作用。当文件名中没有特殊字符时,它会工作。可能性1:您正在将文件名传递给正则表达式文件筛选器 可能性2:zip文件中不允许使用这些字符(或者至少SharpZipLib这么认为)我认为您不能使用FastZip。您需要迭代文件并添加自己指定的条目: entry.IsUnicodeText=true 告诉SharpZipLib条目是unicod

我正在使用
ICSharpCode.SharpZipLib.Zip.FastZip
压缩文件,但我遇到了一个问题:


当我试图压缩文件名中包含特殊字符的文件时,它不起作用。当文件名中没有特殊字符时,它会工作。

可能性1:您正在将文件名传递给正则表达式文件筛选器


可能性2:zip文件中不允许使用这些字符(或者至少SharpZipLib这么认为)

我认为您不能使用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();
 }

尝试从文件名中删除特殊字符,即替换它。
您的
文件名。替换(“&;”、“&”)

您必须下载并编译最新版本的SharpZipLib库,以便使用

entry.IsUnicodeText = true;
以下是您的代码片段(稍加修改):


如果愿意,您可以继续使用
FastZip
,但需要为其提供一个
ZipEntryFactory
,该工厂使用
IsUnicodeText=true
创建
ZipEntry
s

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

当你说它不起作用时,什么不起作用?你能发布你的代码和你收到的错误信息吗?它不是压缩文件,我也没有收到任何错误。没有特殊字符它工作吗?什么特殊字符?你能举个例子吗?HI-ASCII字符示例€…我想你们不知道特殊字符是什么意思:(+1000,Ey vaaaal,太棒了。这正是我想要的。非常感谢。@M_Mogharabi如果其中一个答案确实解决了你的问题,你必须将其标记为已接受。例如,我发布了与Paaland相同的解决方案(只需向上滚动),除了我更快:)2012年12月20日比2011年3月26日早多少?为什么要重新打开这篇古老的帖子?这是一个完全有效的解决方案,不需要手动输入邮政编码:)
var zfe = new ZipEntryFactory { IsUnicodeText = true };
var fz = new FastZip { EntryFactory = zfe };
fz.CreateZip("out.zip", "C:\in", true, null);