我正在使用 Apache Commons API Compression 来压缩文件。 Windows 7 工作正常,但在 Linux (ubuntu 10.10 - UTF8) 中,文件名和文件夹名称中的字符(例如“°”)会被“?”替换。
压缩时或解压缩 tar 时是否应该传递给 API 的任何参数?
我使用 tar.gz 格式,遵循 API 示例。
我尝试压缩的文件是在 Windows 中创建的...有什么问题吗?
代码:
public class TarGzTest
{
public static void createTarGzOfDirectory(String directoryPath, String tarGzPath) throws IOException
{
System.out.println("Criando tar.gz da pasta " + directoryPath + " em " + tarGzPath);
FileOutputStream fOut = null;
BufferedOutputStream bOut = null;
GzipCompressorOutputStream gzOut = null;
TarArchiveOutputStream tOut = null;
try
{
fOut = new FileOutputStream(new File(tarGzPath));
bOut = new BufferedOutputStream(fOut);
gzOut = new GzipCompressorOutputStream(bOut);
tOut = new TarArchiveOutputStream(gzOut);
addFileToTarGz(tOut, directoryPath, "");
}
finally
{
tOut.finish();
tOut.close();
gzOut.close();
bOut.close();
fOut.close();
}
System.out.println("Processo concluído.");
}
private static void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base) throws IOException
{
System.out.println("addFileToTarGz()::"+path);
File f = new File(path);
String entryName = base + f.getName();
TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
if(f.isFile())
{
tOut.putArchiveEntry(tarEntry);
IOUtils.copy(new FileInputStream(f), tOut);
tOut.closeArchiveEntry();
}
else
{
File[] children = f.listFiles();
if(children != null)
{
for(File child : children)
{
addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/");
}
}
}
}
}
(我抑制了主要方法;)
编辑(monkeyjluffy):我所做的更改是在不同平台上始终具有相同的存档。那么对其计算出的hash也是一样的。
我找到了解决我的麻烦的方法。
由于某种原因,java不尊重我的环境的编码,并将其更改为cp1252。
之后,我解压缩该文件,只需进入它的文件夹,然后运行以下命令:
convmv --notest -f cp1252 -t utf8 * -r
它将所有内容递归地转换为 UTF-8。
问题解决了,伙计们。
有关 Linux 中编码问题的更多信息here http://karussell.wordpress.com/2008/09/04/encoding-issues-solutions-for-linux-and-within-java-apps/.
感谢大家的帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)