使用Linux上的Apache Commons Compression压缩文件时编码错误

我正在使用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):我所做的更改是在不同平台上始终保持相同的存档。 然后在它上计算的哈希是相同的。

我找到了解决问题的方法。

出于某种原因,java不尊重我的环境的编码,并将其更改为cp1252。

之后我解压缩文件,我只需输入文件夹,然后运行以下命令:

 convmv --notest -f cp1252 -t utf8 * -r 

它将递归转换为UTF-8。

问题解决了,伙计们。

关于linux中的编码问题的更多信息。

谢谢大家的帮助。

仅供参考,上面的代码中有一个错误解释: apache commons压缩的tar问题

基本上,您需要关闭FileInputStream。 IOUtils.copy()不会为你做。