2011-07-19 50 views
3

我使用Apache Commons API Compression壓縮文件。 Windows 7工作正常,但在Linux(ubuntu 10.10 - UTF8)中,文件名和文件夾名稱中的字符(例如「º」)被替換爲「?」。在Linux上使用Apache Commons壓縮文件時編碼錯誤

當壓縮或者解壓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 + "/"); 
       } 
      } 
     } 
    } 
} 

(我壓抑的主要方法;)

EDIT(monkeyjluffy):我所做的修改將有總是不同的平臺上同一存檔。然後在它上面計算的哈希值是相同的。

+0

你的意思是說,當你解壓時,文件是不一樣的?請顯示您使用的確切代碼。 –

+0

它可能與Windows或Linux中如何表示CR o LF有關? – Gevorg

+0

@ jon-skeet我編輯了問題,添加了代碼和一些信息.. – caarlos0

回答

0

我找到了解決我麻煩的解決方法。

由於某些原因,java不尊重我的環境的編碼,並將其更改爲cp1252。

之後,我解壓縮文件,我只是在其中輸入文件夾,運行這個命令:

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

它遞歸轉換一切UTF-8。

問題解決了,夥計們。

更多關於linux編碼問題的信息here

謝謝大家的幫助。

1

僅供參考,有一個在上面的代碼中的錯誤解釋在這裏:Tar problem with apache commons compress

基本上,你需要關閉的FileInputStream。 IOUtils.copy()不會爲你做。

+0

謝謝你,不要修復這個bug,但是很高興知道:) – caarlos0

相關問題