在 Java 中创建包含国家字符的 tar 存档

发布于 2024-08-05 15:17:28 字数 717 浏览 2 评论 0原文

您是否知道 Java 中的某些库/方法可以使用适当的 Windows 国家代码页(例如 cp1250 )中的文件名生成 tar 存档。

我尝试使用 Java tar,示例代码:

final TarEntry entry = new TarEntry( files[i] );
String filename = files[i].getPath().replaceAll( baseDir, "" );
entry.setName( new String( filename.getBytes(), "Cp1250" ) );
out.putNextEntry( entry );
...

它不起作用。我在 Windows 中提取 tar 时,国家字符被破坏。 我还发现了一件奇怪的事情,在 Linux 下,只有当我使用 ISO-8859-1 时,波兰语国家字符才能正确显示:

entry.setName( new String( filename.getBytes(), "ISO-8859-1" ) );

尽管正确的波兰语代码页是 ISO-8859-2,但它也不起作用。 我也尝试过 Windows 下的 Cp852,没有效果。

我知道 tar 格式的局限性,但更改它不是一个选择。

感谢您的建议,

Do you know some library/way in Java to generate tar archive with file names in proper windows national codepage ( for example cp1250 ).

I tried with Java tar, example code:

final TarEntry entry = new TarEntry( files[i] );
String filename = files[i].getPath().replaceAll( baseDir, "" );
entry.setName( new String( filename.getBytes(), "Cp1250" ) );
out.putNextEntry( entry );
...

It doesn't work. National characters are broken where I extract tar in windows.
I've also found a strange thing, under Linux Polish national characters are shown correctly only when I used ISO-8859-1:

entry.setName( new String( filename.getBytes(), "ISO-8859-1" ) );

Despite the fact that proper Polish codepage is ISO-8859-2, which doesn't work too.
I've also tried Cp852 for windows, no effect.

I know the limitations of tar format, but changing it is not an option.

Thanks for suggestions,

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

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

发布评论

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

评论(2

雨后咖啡店 2024-08-12 15:17:28

官方规定,TAR 不支持标头中的非 ASCII。不过,我可以在 Linux 上使用 UTF-8 编码的文件名。

你应该尝试这个,

String filename = files[i].getName();
byte[] bytes = filename.getBytes("Cp1250")
entry.setName(new String(bytes, "ISO-8859-1"));
out.putNextEntry( entry );

这至少保留了 TAR 头中 Cp1250 中的字节。

Officially, TAR doesn't support non-ASCII in headers. However, I was able to use UTF-8 encoded filenames on Linux.

You should try this,

String filename = files[i].getName();
byte[] bytes = filename.getBytes("Cp1250")
entry.setName(new String(bytes, "ISO-8859-1"));
out.putNextEntry( entry );

This at least preserves the bytes in Cp1250 in TAR headers.

叹倦 2024-08-12 15:17:28

tar 不允许在其标头中使用非 ASCII 值。如果您尝试不同的编码,结果可能取决于目标平台决定如何处理这些字节值。听起来你的目标平台的 tar 程序正在将字节解释为 ISO-8859-1,这就是它“有效”的原因。

看看扩展属性? http://www.freebsd.org/cgi/man.cgi?query=tar&sektion=5&manpath=FreeBSD+8-current" freebsd.org/cgi/man.cgi?query=tar&sektion=5&manpath=FreeBSD+8-current

我不是这里的专家,但这似乎是放置任何非 ASCII 值的唯一官方方法在 tar 文件头中。

tar doesn't allow for non-ASCII values in its headers. If you try a different encoding, the result is probably up to what the target platform decides to do with those byte values. It kind of sounds like your target platform's tar program is interpreting the bytes as ISO-8859-1, which is why that 'works'.

Have a look at extended attributes? http://www.freebsd.org/cgi/man.cgi?query=tar&sektion=5&manpath=FreeBSD+8-current

I am no expert here but this seems to be the only official way to put any non-ASCII values in a tar file header.

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