在java(JSP)中提取.tar.gz文件 [英] Extract a .tar.gz file in java (JSP)

查看:152
本文介绍了在java(JSP)中提取.tar.gz文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can't seem to import the packages needed or find any online examples of how to extract a .tar.gz file in java.

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

What makes it worse is I'm using JSP pages and am having trouble importing packages into my project. I'm copying the .jar's into WebContent/WEB-INF/lib/ and then right clicking on the project and selecting import external jar and importing it. Sometimes the packages resolve, other times they don't. Can't seem to get GZIP to import either. The imports in eclipse for jsp aren't intuitive like they are in normal Java code where you can right click a recognized package and select import.

我试过Apache公共图书馆,冰和另一个叫做JTar的人。 Ice已导入,但我找不到任何如何使用它的例子?

I've tried the Apache commons library, the ice and another one called JTar. Ice has imported, but I can't find any examples of how to use it?

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

I guess I need to uncompress the gzipped part first, then open it with the tarstream?

非常感谢任何帮助。

推荐答案

好的,我最后想出来了,这是我的代码,以防万一将来有所帮助。
用Java编写,使用apache commons io和compress librarys。

Ok, i finally figured this out, here is my code in case this helps anyone in the future. Its written in Java, using the apache commons io and 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();
    }
}

这篇关于在java(JSP)中提取.tar.gz文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆