7

我似乎无法导入所需的包或找到任何关于如何.tar.gz在 java 中提取文件的在线示例。

更糟糕的是我正在使用 JSP 页面并且在将包导入我的项目时遇到了麻烦。我将 .jar 复制到WebContent/WEB-INF/lib/然后右键单击项目并选择导入外部 jar 并导入它。有时包会解析,有时则不会。似乎也无法让 GZIP 导入。eclipse for jsp 中的导入不像在普通 Java 代码中那样直观,您可以在其中右键单击已识别的包并选择导入。

我已经尝试过 Apache 公共库、ice 和另一个名为 JTar 的库。Ice已经导入了,但是我找不到任何如何使用它的例子?

我想我需要先解压缩 gzipped 部分,然后用 tarstream 打开它?

任何帮助是极大的赞赏。

4

2 回答 2

18

接受的答案工作正常,但我认为写入文件操作是多余的。

你可以使用类似的东西

 TarArchiveInputStream tarInput = 
      new TarArchiveInputStream(new GZipInputStream(new FileInputStream("Your file name")));

 TarArchiveEntry currentEntry = tarInput.getNextTarEntry();
 while(currentEntry != null) {
      File f = currentEntry.getFile();
      // TODO write to file as usual
 }

希望这有帮助。

Maven 回购

于 2013-06-13T04:06:01.583 回答
5

好的,我终于想通了,这是我的代码,以防将来对任何人有所帮助。它是用 Java 编写的,使用 apache commons io 和 compress 库。

File dir = new File("directory/of/.tar.gz/files/here");
File listDir[] = dir.listFiles();
if (listDir.length!=0){
    for (File i:listDir){
        /*  Warning! this will try and extract all files in the directory
            if other files exist, a for loop needs to go here to check that
            the file (i) is an archive file before proceeding */
        if (i.isDirectory()){
            break;
        }
        String fileName = i.toString();
        String tarFileName = fileName +".tar";
        FileInputStream instream= new FileInputStream(fileName);
        GZIPInputStream ginstream =new GZIPInputStream(instream);
        FileOutputStream outstream = new FileOutputStream(tarFileName);
        byte[] buf = new byte[1024]; 
        int len;
        while ((len = ginstream.read(buf)) > 0) 
        {
            outstream.write(buf, 0, len);
        }
        ginstream.close();
        outstream.close();
        //There should now be tar files in the directory
        //extract specific files from tar
        TarArchiveInputStream myTarFile=new TarArchiveInputStream(new FileInputStream(tarFileName));
        TarArchiveEntry entry = null;
        int offset;
        FileOutputStream outputFile=null;
        //read every single entry in TAR file
        while ((entry = myTarFile.getNextTarEntry()) != null) {
            //the following two lines remove the .tar.gz extension for the folder name
            String fileName = i.getName().substring(0, i.getName().lastIndexOf('.'));
            fileName = fileName.substring(0, fileName.lastIndexOf('.'));
            File outputDir =  new File(i.getParent() + "/" + fileName + "/" + entry.getName());
            if(! outputDir.getParentFile().exists()){ 
                outputDir.getParentFile().mkdirs();
            }
            //if the entry in the tar is a directory, it needs to be created, only files can be extracted
            if(entry.isDirectory){
                outputDir.mkdirs();
            }else{
                byte[] content = new byte[(int) entry.getSize()];
                offset=0;
                myTarFile.read(content, offset, content.length - offset);
                outputFile=new FileOutputStream(outputDir);
                IOUtils.write(content,outputFile);  
                outputFile.close();
            }
        }
        //close and delete the tar files, leaving the original .tar.gz and the extracted folders
        myTarFile.close();
        File tarFile =  new File(tarFileName);
        tarFile.delete();
    }
}
于 2013-01-20T17:41:40.767 回答