【问题标题】:Encoding errors when compressing files with Apache Commons Compression on Linux在 Linux 上使用 Apache Commons Compression 压缩文件时出现编码错误
【发布时间】:2011-07-19 18:43:12
【问题描述】:

我正在使用 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 + "/");
                }
            }
        }
    }
}

(我抑制了main方法;)

编辑(monkeyjluffy):我所做的更改是在不同平台上始终拥有相同的存档。那么在它上面计算出来的hash也是一样的。

【问题讨论】:

  • 你的意思是解压的时候文件和原来不一样了?请显示您使用的确切代码。
  • 这可能与 CR o LF 在 Windows 和 Linux 中的表示方式有关吗??
  • @jon-skeet 我编辑了问题,添加了代码和一些信息..
  • @caarlos0:好的,这就是压缩部分......和解压缩?您如何查看“坏”文件?
  • @jon-skeet 我正在用“tar xzvf file.tar.gz”解压...

标签: java encoding gzip apache-commons tar


【解决方案1】:

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

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

解压后,进入文件夹,运行如下命令:

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

它会将所有内容递归地转换为 UTF-8。

问题解决了,伙计们。

有关 linux 中编码问题的更多信息here

感谢大家的帮助。

【讨论】:

    猜你喜欢
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多