小编典典

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

jsp

我似乎无法导入所需的程序包,也找不到任何有关如何.tar.gz在Java中提取文件的在线示例。

更糟糕的是,我使用的是JSP页面,无法将包导入到我的项目中。我将.jar文件复制到其中WebContent/WEB- INF/lib/,然后右键单击该项目,然后选择导入外部jar文件并将其导入。有时程序包可以解决,而其他时候则不能。似乎也无法导入GZIP。eclipse中用于jsp的导入不像常规Java代码中那样直观,您可以在其中右键单击已识别的包并选择import。

我尝试过Apache公共库,ice库和另一个名为JTar的库。冰已导入,但我找不到任何使用方法的示例?

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

任何帮助是极大的赞赏。


阅读 316

收藏
2020-06-08

共1个答案

小编典典

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