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

发布于 2024-10-03 22:30:42 字数 118 浏览 7 评论 0原文

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

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

I am using ICSharpCode.SharpZipLib.Zip.FastZip to zip files but I'm stuck on a problem:

When I try to zip a file with special characters in its file name, it does not work. It works when there are no special characters in the file name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光礼记 2024-10-10 22:30:42

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

I think you cannot use FastZip. You need to iterate the files and add the entries yourself specifying:

entry.IsUnicodeText = true;

To tell SharpZipLib the entry is 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();
 }
萌化 2024-10-10 22:30:42

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

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

You can continue using FastZip if you would like, but you need to give it a ZipEntryFactory that creates ZipEntrys with IsUnicodeText = true.

var zfe = new ZipEntryFactory { IsUnicodeText = true };
var fz = new FastZip { EntryFactory = zfe };
fz.CreateZip("out.zip", "C:\in", true, null);
回首观望 2024-10-10 22:30:42

您必须下载并编译最新版本的 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);
            }
        }
    }
}

You have to download and compile the latest version of SharpZipLib library so you can use

entry.IsUnicodeText = true;

here is your snippet (slightly modified):

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);
            }
        }
    }
}
标点 2024-10-10 22:30:42

可能性 1:您将文件名传递给正则表达式文件过滤器。

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

Possibility 1: you are passing a filename to the regex file filter.

Possibility 2: those characters are not allowed in zip files (or at least SharpZipLib thinks so)

农村范ル 2024-10-10 22:30:42

尝试从文件名中取出特殊字符,即替换它。
你的Filename.Replace("&", "&");

try to take out the special character from the file name, i,e replace it.
your Filename.Replace("&", "&");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文