我使用ICSharpCode.SharpZipLib.Zip.FastZip
zip文件的文件,但我卡在了一个问题:ICSharpCode.SharpZipLib.Zip.FastZip不荏苒在他们的文件名中的特殊字符
当我尝试拉链有特殊字符的文件在它的文件名中,它不起作用。它在文件名中没有特殊字符时起作用。
我使用ICSharpCode.SharpZipLib.Zip.FastZip
zip文件的文件,但我卡在了一个问题: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();
}
尝试从文件名中取出特殊字符,我将其替换。 您Filename.Replace("&", "&");
你必须下载并编译最新版本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);
}
}
}
}
您可以继续使用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);
当你说它不起作用,_what_不起作用?你可以请你发布你的代码和错误信息吗? – 2010-11-22 13:10:49
它不是压缩该文件,我也没有得到任何错误 – BreakHead 2010-11-22 13:12:40
它没有特殊字符的工作吗? – 2010-11-22 13:14:47