Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make tar file by Java

Tags:

I want to use Java to compress a folder to a tar file (in programmatic way). I think there must be an open source or library to do it. However, I cannot find such method.

Alternatively, could I make a zip file and rename its extended name as .tar?

Anyone could suggest a library to do it? Thanks!

like image 226
卢声远 Shengyuan Lu Avatar asked Aug 09 '11 14:08

卢声远 Shengyuan Lu


People also ask

Can 7zip create tar gz?

The trick is that 7-Zip will only gzip a single file. So creating a tar. gz is a two step process. First create the tar archive, then use 7-Zip to select the tar and you will get an option to gzip it.


1 Answers

I would look at Apache Commons Compress.

There is an example part way down this examples page, which shows off a tar example.

TarArchiveEntry entry = new TarArchiveEntry(name); entry.setSize(size); tarOutput.putArchiveEntry(entry); tarOutput.write(contentOfEntry); tarOutput.closeArchiveEntry(); 
like image 125
nicholas.hauschild Avatar answered Oct 24 '22 17:10

nicholas.hauschild