Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用java(JSP)提取.tar.gz文件_Java_Jsp_Extract_Tar_Apache Commons - Fatal编程技术网

用java(JSP)提取.tar.gz文件

用java(JSP)提取.tar.gz文件,java,jsp,extract,tar,apache-commons,Java,Jsp,Extract,Tar,Apache Commons,我似乎无法导入所需的软件包,也找不到如何在java中提取.tar.gz文件的在线示例 更糟糕的是,我正在使用JSP页面,无法将包导入到我的项目中。我将.jar复制到WebContent/WEB-INF/lib/中,然后右键单击项目,选择Importexternaljar并导入它。有时包可以解决问题,有时则无法解决。似乎也无法让GZIP导入。eclipse for jsp中的导入不像普通Java代码中那样直观,您可以右键单击已识别的包并选择导入 我试过ApacheCommons库、ice和另一个叫

我似乎无法导入所需的软件包,也找不到如何在java中提取
.tar.gz
文件的在线示例

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

我试过ApacheCommons库、ice和另一个叫做JTar的库。冰是进口的,但我找不到任何使用它的例子

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


非常感谢您的帮助。

好的,我终于明白了,这是我的代码,以防将来对任何人有所帮助。 它是用Java编写的,使用ApacheCommonsIO和CompressLibrary

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();
    }
}

接受的答案很好,但我认为写文件操作是多余的

你可以用像这样的东西

 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
 }
希望这有帮助


文件f=currentEntry.getFile()不是您使用此API的方式AFAIK.GZIPInputStream应全部大写