文件名中的 ruby​​zip 和 unicode 字符

发布于 2025-01-06 12:44:20 字数 178 浏览 6 评论 0原文

我正在使用 rubyzip gem 和 Zip::ZipOutputStream 类创建 zip 存档,并且遇到了 unicode 字母(西里尔字母)的问题 - 在存档中它们显示为问号,例如 <代码>???? ???? ??.doc。 rubyzip 支持 unicode 吗?

I am creating zip archive with rubyzip gem and Zip::ZipOutputStream class and have got a problem with unicode letters (cyrillic) - in archive they are presented as question marks like ????? ???? ??.doc. Does rubyzip support unicode?

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

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

发布评论

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

评论(4

追星践月 2025-01-13 12:44:20

我查看了 ruby​​zip 方法,似乎 ruby​​zip 不能更改语言。它可能使用您计算机的默认代码页。您可以使用 chilkat zip 代替示例,除非您有特定要求chilkat 无法解决这个问题。

I looked at rubyzip methods and it doesn't seem that rubyzip can change the language. It probably uses your computer's default code page. You could use chilkat zip instead as in this example unless you have specific requirements that cannot be addressed by chilkat.

撕心裂肺的伤痛 2025-01-13 12:44:20

您可以使用以下代码片段将 UTF-8 转换为 CP437 ,其中涵盖了一些 unicode 字符(只是很少)。 Windows 7 及更早版本假定文件名以 CP437 编码。

# first normalize the string
normalized_filename = input.mb_chars.normalize.to_s
# then encode in cp437
filename_for_zip = normalized_filename.encode("cp437")
# add file to zip
zipfile.add(filename_for_zip, pdf_file)

You can use the following snippet to convert UTF-8 to CP437 which cover some unicode chars (just a few). Windows 7 and older assume that filenames are encoded in CP437.

# first normalize the string
normalized_filename = input.mb_chars.normalize.to_s
# then encode in cp437
filename_for_zip = normalized_filename.encode("cp437")
# add file to zip
zipfile.add(filename_for_zip, pdf_file)
不忘初心 2025-01-13 12:44:20

你可以直接运行 zip 。

`cd yourfolder; zip archivename file1 file2`

注意具体报价。在 Ubuntu 上为我工作,处理西里尔文文件名,而 ruby​​zip 则生成具有不可读文件名的存档。

You may just run zip directly.

`cd yourfolder; zip archivename file1 file2`

Notice specific quotes. Worked for me on Ubuntu for Cyrillic file names, while rubyzip was generating archive with non-readable file names.

回忆凄美了谁 2025-01-13 12:44:20

这对我有用。在创建 Zip 文件之前设置此文件

Zip.force_entry_names_encoding = 'UTF-8'

我从 https://github.com/rubyzip 获得此文件/rubyzip#non-ascii-names

Here's what worked for me. Set this before creating the Zip file

Zip.force_entry_names_encoding = 'UTF-8'

I got this from https://github.com/rubyzip/rubyzip#non-ascii-names

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