7

在我的网站上,我可以选择下载用户上传的所有图片。问题出在带有希伯来语名称的图像中(我需要文件的原始名称)。我试图解码文件名,但这没有帮助。这是一个代码:

using ICSharpCode.SharpZipLib.Zip;

Encoding iso = Encoding.GetEncoding("ISO-8859-1");
Encoding utf8 = Encoding.UTF8;
byte[] utfBytes = utf8.GetBytes(file.Name);
byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes);
string name = iso.GetString(isoBytes);

var entry = new ZipEntry(name + ".jpg");
zipStream.PutNextEntry(entry);
using (var reader = new System.IO.FileStream(file.Name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    byte[] buffer = new byte[ChunkSize];
    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);
    }
} 

经过 utf-8 编码后,我得到这样的希伯来文文件名:??????.jpg 我的错误在哪里?

4

3 回答 3

2

Unicode(UTF-8 是一种二进制编码)可以表示比其他 8 位编码更多的字符。此外,您没有进行正确的转换,而是进行了重新解释,这意味着您的文件名是垃圾。您真的应该阅读Joel 关于 Unicode的文章。

...

看了这篇文章,应该知道 in C#string 可以存储 unicode 数据,所以如果库不包含编码处理的 bug(file.NamethisZipEntry总是可能的)。

于 2012-12-20T08:29:32.247 回答
1

尝试使用

ZipStrings.UseUnicode = true;

它应该是 ICSharpCode.SharpZipLib.Zip 命名空间的一部分。

之后你可以使用类似的东西

var newZipEntry = new ZipEntry($"My ünicödë string.pdf");

并将条目正常添加到流中。在 C# 之前,您不需要对字符串进行任何转换。

于 2019-11-24T20:50:36.863 回答
0

您进行了错误的转换,因为 C# 中的字符串已经是 unicode。您使用什么工具来检查存档中的文件名?默认情况下,Windows ZIP 实现对文件名使用系统 DOS 编码,而其他实现可以使用其他编码。

于 2012-12-24T10:13:05.933 回答