使用 Java 中的国家字符创建 tar 存档 [英] Creating tar archive with national characters in Java

查看:31
本文介绍了使用 Java 中的国家字符创建 tar 存档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您是否知道 Java 中的一些库/方法来生成具有正确 Windows 国家代码页(例如 cp1250 )中的文件名的 tar 存档.

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

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

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 );
...

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

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" ) );

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

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.

我知道 tar 格式的局限性,但不能更改它.

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

感谢您的建议,

推荐答案

正式地,TAR 不支持标头中的非 ASCII.但是,我可以在 Linux 上使用 UTF-8 编码的文件名.

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

你应该试试这个,

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

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

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

这篇关于使用 Java 中的国家字符创建 tar 存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆