Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Compress/Decompress tar.gz files in java

Can anyone show me the correct way to compress and decompress tar.gzip files in java i've been searching but the most i can find is either zip or gzip(alone).

like image 505
kdgwill Avatar asked Aug 19 '11 22:08

kdgwill


People also ask

Can you compress a tar gz file?

Tar and Gzip are two of the most common utilities for archiving and compressing files. More specificly, tar is used for archiving and gzip is used for compression, however the two are most often used in conjunction.

How do I compress a gzip file in Java?

Create a FileOutputStream to the destination file, that is the file path to the output compressed file. Create a GZIPOutputStream to the above FileOutputStream . Create a FileInputStream to the file you want to compress. Read the bytes from the source file and compress them using GZIPOutputStream .


1 Answers

I've written a wrapper for commons-compress called jarchivelib that makes it easy to extract or compress from and into File objects.

Example code would look like this:

File archive = new File("/home/thrau/archive.tar.gz"); File destination = new File("/home/thrau/archive/");  Archiver archiver = ArchiverFactory.createArchiver("tar", "gz"); archiver.extract(archive, destination); 
like image 110
thrau Avatar answered Oct 04 '22 20:10

thrau