解压缩文件后删除目录

时间:2019-06-21 10:02:55

标签: java file tar delete-file

我有一个.tar.gz文件,我想解压缩该文件。

为此,我在Java中具有以下功能:

private void unTarFile(String tarFile, File destFile) {
    TarArchiveInputStream tis = null;
    FileInputStream fis = null;
    GZIPInputStream gzipInputStream = null;
    BufferedInputStream bis = null;
    try {
        fis = new FileInputStream(tarFile);
        bis = new BufferedInputStream(fis);
        // .gz
        gzipInputStream = new GZIPInputStream(bis);
        // .tar.gz
        tis = new TarArchiveInputStream(gzipInputStream);
        TarArchiveEntry tarEntry = null;
        while ((tarEntry = tis.getNextTarEntry()) != null) {
            System.out.println(" tar entry- " + tarEntry.getName());
            if (tarEntry.isDirectory()) {
                continue;
            } else {
                // In case entry is for file ensure parent directory is in place
                // and write file content to Output Stream
                File outputFile = new File(destFile + File.separator + tarEntry.getName());
                outputFile.getParentFile().mkdirs();
                IOUtils.copy(tis, new FileOutputStream(outputFile));
            }
        }
    } catch (IOException ex) {
        System.out.println("Error while untarring a file- " + ex.getMessage());
    } finally {
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (gzipInputStream != null) {
            try {
                gzipInputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (tis != null) {
            try {
                tis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}

这有效,我可以成功解压缩.tar.gz。 我可以将未压缩目录的文件编辑到其中,但是当我尝试删除目录时却无法:

enter image description here

我忘了关闭东西吗?

0 个答案:

没有答案