【问题标题】:Extract a .tar.gz file in java (JSP)在 java (JSP) 中提取 .tar.gz 文件
【发布时间】:2013-01-02 09:07:38
【问题描述】:

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

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

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

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

非常感谢任何帮助。

【问题讨论】:

    标签: java jsp extract tar apache-commons


    【解决方案1】:

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

    你可以使用类似的东西

     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 Repo

    【讨论】:

    • File f = currentEntry.getFile(); 不是您使用此 API AFAIK 的方式。
    • GZIPInputStream 应该全部大写
    【解决方案2】:

    好的,我终于想通了,这是我的代码,以防将来对任何人有所帮助。 它是用 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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多