Make tar file by Java

卢声远 Shengyuan Lu picture 卢声远 Shengyuan Lu · Aug 9, 2011 · Viewed 45.8k times · Source

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!

Answer

nicholas.hauschild picture nicholas.hauschild · Aug 9, 2011

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();