2012-02-18 32 views
1

作爲Convert .tar.gz file to .zip using TrueZip?的後續措施,如何使用TrueZip將一個壓縮文件的內容複製到另一個文件中?當我調用:如何使用TrueZip TPath複製文件?

TPath sourceFile = new TPath("c:/foo.zip"); 
TPath targetFile = new TPath("c:/bar.jar"); 
Files.copy(sourceFile, targetFile, StandardCopyOption.COPY_ATTRIBUTES); 

我得到:

java.io.FileNotFoundException: C:\foo.zip (expected FILE - is DIRECTORY) 
    at de.schlichtherle.truezip.nio.file.TFileSystemProvider.copy(TFileSystemProvider.java:397) 
    at de.schlichtherle.truezip.nio.file.TFileSystemProvider.copy(TFileSystemProvider.java:364) 
    at java.nio.file.Files.copy(Files.java:1219) 

的事情是,我知道一個事實,即c:/foo.zip是現有文件,而不是一個目錄。

  1. 爲什麼我得到這個錯誤?
  2. 我該如何將ZIP文件轉換爲使用TPath的JAR文件?
  3. TPath是否需要像TFile那樣卸載?

回答

2

回答我的問題...請注意,我想通了這一點通過試驗和錯誤,所以我可能是錯上的一些要點:

  1. 檔案被視爲目錄。 Files.copy(archive, archive)本質上是試圖將一個目錄複製到另一個目錄。 Files.copy(Path, Path)用於複製單個文件,而不是用於遞歸複製目錄。

  2. 現在我們知道檔案被當作目錄,我們簡單地從一個目錄中的文件複製到另一個:

Files.walkFileTree(sourceFile, new SimpleFileVisitor<Path>() 
{ 
    @Override 
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException 
    { 
     Files.copy(file, targetFile.resolve(sourceFile.relativize(file)), 
      StandardCopyOption.COPY_ATTRIBUTES); 
     return super.visitFile(file, attrs); 
    } 
}); 

是的,你可以使用:targetFile.getFileSystem().sync(FsSyncOptions.UMOUNT);