java 文件夹 tar文件_如何在Java中提取tar文件?

您可以使用Apache Commons Compress库执行此操作。您可以从http://mvnrepository.com/artifact/org.apache.commons/commons-compress/1.2下载1.2版本。

这里有两种方法:一种解压缩文件,另一种解压缩文件。因此,对于文件 tar.gz,您需要先将其解压缩,然后再将其解压缩。请注意,tar归档文件也可能包含文件夹,这种情况下需要在本地文件系统上创建它们。

请享用。

/** Untar an input file into an output file.

* The output file is created in the output folder, having the same name

* as the input file, minus the '.tar' extension.

*

* @param inputFile     the input .tar file

* @param outputDir     the output directory file.

* @throws IOException

* @throws FileNotFoundException

*

* @return  The {@link List} of {@link File}s with the untared content.

* @throws ArchiveException

*/

private static List unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException {

LOG.info(String.format("Untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));

final List untaredFiles = new LinkedList();

final InputStream is = new FileInputStream(inputFile);

final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);

TarArchiveEntry entry = null;

while ((entry = (TarArchiveEntry)debInputStream.getNextEntry()) != null) {

final File outputFile = new File(outputDir, entry.getName());

if (entry.isDirectory()) {

LOG.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));

if (!outputFile.exists()) {

LOG.info(String.format("Attempting to create output directory %s.", outputFile.getAbsolutePath()));

if (!outputFile.mkdirs()) {

throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));

}

}

} else {

LOG.info(String.format("Creating output file %s.", outputFile.getAbsolutePath()));

final OutputStream outputFileStream = new FileOutputStream(outputFile);

IOUtils.copy(debInputStream, outputFileStream);

outputFileStream.close();

}

untaredFiles.add(outputFile);

}

debInputStream.close();

return untaredFiles;

}

/**

* Ungzip an input file into an output file.

*

* The output file is created in the output folder, having the same name

* as the input file, minus the '.gz' extension.

*

* @param inputFile     the input .gz file

* @param outputDir     the output directory file.

* @throws IOException

* @throws FileNotFoundException

*

* @return  The {@File} with the ungzipped content.

*/

private static File unGzip(final File inputFile, final File outputDir) throws FileNotFoundException, IOException {

LOG.info(String.format("Ungzipping %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));

final File outputFile = new File(outputDir, inputFile.getName().substring(0, inputFile.getName().length() - 3));

final GZIPInputStream in = new GZIPInputStream(new FileInputStream(inputFile));

final FileOutputStream out = new FileOutputStream(outputFile);

IOUtils.copy(in, out);

in.close();

out.close();

return outputFile;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值