在java(JSP)中提取.tar.gz文件

时间:2013-01-18 15:52:40

标签: java jsp extract tar apache-commons

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

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

我尝试过Apache commons库,另一个叫做JTar。 Ice已导入,但我找不到任何如何使用它的例子?

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

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:16)

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

您可以使用类似

的内容
 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

答案 1 :(得分:5)

好的,我终于弄明白了,这是我的代码,以防将来有人帮助。 它用Java编写,使用apache commons io和compress librarys。

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