我使用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):我所做的修改將有總是不同的平臺上同一存檔。然後在它上面計算的哈希值是相同的。
你的意思是說,當你解壓時,文件是不一樣的?請顯示您使用的確切代碼。 –
它可能與Windows或Linux中如何表示CR o LF有關? – Gevorg
@ jon-skeet我編輯了問題,添加了代碼和一些信息.. – caarlos0