How to extract tar file in Java?

How to extract tar file (or tar.gz or tar.bz2) in Java?

+56
java tar archive
Nov 24 '08 at 21:48
source share
7 answers

Note. This feature was later published through a separate project, Apache Commons Compress, as described in another answer. This answer is deprecated.




I did not use the tar API directly, but tar and bzip2 are implemented in Ant; you could borrow their implementation or perhaps use Ant to do what you need.

Gzip is part of Java SE (and I assume that the Ant implementation follows the same model).

GZIPInputStream is just an InputStream decorator. You can wrap, for example, FileInputStream in GZIPInputStream and use it the same way you would use any InputStream :

 InputStream is = new GZIPInputStream(new FileInputStream(file)); 

(Note that GZIPInputStream has its own internal buffer, so wrapping the FileInputStream in a BufferedInputStream is likely to slow performance.)

+18
Nov 24 '08 at 22:00
source share

You can do this using the Apache Commons Compress library. You can download version 1.2 from http://mvnrepository.com/artifact/org.apache.commons/commons-compress/1.2 .

Here are two methods: one that unpacks the file, and the other - it. So, for the file <file_name> tar.gz you need to unzip it first and then unlock it. Please note that the tar archive may also contain folders in which they must be created on the local file system.

Enjoy.

 /** 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<File> 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<File> untaredFiles = new LinkedList<File>(); 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. * <p> * 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; } 
+59
Sep 26 '11 at 14:05
source share

Apache Commons VFS supports tar as a virtual file system that supports URLs like this tar:gz: http://anyhost/dir/mytar.tar.gz!/mytar.tar!/path/in/tar/README.txt

TrueZip or its successor TrueVFS does the same ... it is also available from Maven Central.

+11
Nov 12 '10 at 13:30
source share
 Archiver archiver = ArchiverFactory.createArchiver("tar", "gz"); archiver.extract(archiveFile, destDir); 

Dependence:

  <dependency> <groupId>org.rauschig</groupId> <artifactId>jarchivelib</artifactId> <version>0.5.0</version> </dependency> 
+9
Mar 18 '14 at 2:04
source share

I just tried a bunch of suggested libs (TrueZip, Apache Compress), but no luck.

Here is an example with Apache Commons VFS:

 FileSystemManager fsManager = VFS.getManager(); FileObject archive = fsManager.resolveFile("tgz:file://" + fileName); // List the children of the archive file FileObject[] children = archive.getChildren(); System.out.println("Children of " + archive.getName().getURI()+" are "); for (int i = 0; i < children.length; i++) { FileObject fo = children[i]; System.out.println(fo.getName().getBaseName()); if (fo.isReadable() && fo.getType() == FileType.FILE && fo.getName().getExtension().equals("nxml")) { FileContent fc = fo.getContent(); InputStream is = fc.getInputStream(); } } 

And the maven dependency:

  <dependency> <groupId>commons-vfs</groupId> <artifactId>commons-vfs</artifactId> <version>1.0</version> </dependency> 
+6
Sep 23 2018-11-21T00:
source share

In addition to gzip and bzip2, the Apache Commons Compress API also supports tar based on the ICE Engineering Java Tar Package , which is both an API and a standalone tool.

+5
Nov 12 2018-10-12
source share

How about using this API for tar files, is this the other one included inside Ant for BZIP2 and the standard one for GZIP?

+4
Nov 24 '08 at 21:55
source share



All Articles