Search code examples
javatruezip

Convert .tar.gz file to .zip using TrueZip?


When I invoke:

File input = new File("cmake.tar.gz");
TFile sourceFile = new TFile(input);
TFile targetFile = new TFile(File.createTempFile("cmake", ".zip"));
try
{
    TFile.cp_rp(sourceFile, targetFile, TArchiveDetector.NULL);
}
finally
{
    TFile.umount(targetFile);
}

I get:

java.io.IOException: C:\Users\Gili\AppData\Local\Temp\cmake4527983120069708378.zip (not a directory)
        at de.schlichtherle.truezip.file.TBIO.cp_r0(TBIO.java:163)
        at de.schlichtherle.truezip.file.TBIO.cp_r(TBIO.java:142)
        at de.schlichtherle.truezip.file.TFile.cp_rp(TFile.java:3364)
        at com.googlecode.cmakemavenproject.GetBinariesMojo.download(GetBinariesMojo.java:275)

How can I instruct TrueZip to create a new .zip file containing the contents of the .tar.gz file?


Solution

  • The issue is that the target archive file already exists as an empty file once you've called File.createTempFile(*), which will be treated as a false positive archive file by the TrueZIP Kernel. According to this logic, your subsequent call to TFile.cp_rp(*) tries to recursively copy a virtual directory to a plain file, which cannot work.

    To make your code work, simply call File.delete() on the object returned by File.createTempFile(*). The remainder of your code should then work.