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

大憨熊 提交于 2019-12-11 07:56:32

问题


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?


回答1:


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.




回答2:


I've not used TrueZip, but a quick scan of the API docs leads me to conclude that you can't use TFile in this way. A TFile object represents a single member of an archive. If you want to copy all members you must iterate over the input members yourself and copy each one.

There may be an API to handle entire archives, but I didn't see it.



来源:https://stackoverflow.com/questions/9266974/convert-tar-gz-file-to-zip-using-truezip

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!