In this article we will go through some examples using Apache commons compress for TAR, GZip, BZip2, XZ, Snappy, Deflate.
Examples in this article:
- Simple TAR with files, directory & sub directory or sub folders.
- Use above TAR & compress further using GZip, BZip2, XZ, Snappy, Deflate.
- UnGZip and UnTar files/folders.
Dependency (maven, gradle, ivy etc.)
https://mvnrepository.com/artifact/org.apache.commons/commons-compress
Project structure (Basic maven project)
- compress-me –> Folder to compress. Files range from 5 MB to 12 MB.
- output –> output folder
1 2 3 4 |
07/22/2019 11:11 PM 4,823,568 file-to-compress-1.txt 07/22/2019 11:12 PM 5,358,150 file-to-compress-2.txt 07/24/2019 05:34 PM 12,690,822 some-file-1.txt 07/24/2019 05:33 PM 10,716,300 some-file-2.txt |
Create TAR file from folders, sub folders
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
public static void createTarFile() { // Create TAR file stream. try (TarArchiveOutputStream archive = new TarArchiveOutputStream(new FileOutputStream("output/sample.tar"))) { File folderToTar = new File("compress-me"); // Walk through files, folders & sub-folders. Files.walk(folderToTar.toPath()).forEach(p -> { File file = p.toFile(); // Directory is not streamed, but its files are streamed into TAR file with // folder in it's path if (!file.isDirectory()) { System.out.println("Taring file - " + file); TarArchiveEntry entry_1 = new TarArchiveEntry(file, file.toString()); try (FileInputStream fis = new FileInputStream(file)) { archive.putArchiveEntry(entry_1); IOUtils.copy(fis, archive); archive.closeArchiveEntry(); } catch (IOException e) { e.printStackTrace(); } } }); // Complete archive entry addition. archive.finish(); } catch (IOException e) { e.printStackTrace(); } } |
1 2 3 4 |
Taring file - compress-me\file-to-compress-1.txt Taring file - compress-me\file-to-compress-2.txt Taring file - compress-me\some_folder\some-file-1.txt Taring file - compress-me\some_folder\some-file-2.txt |
GZip from existing TAR file
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public static void createTarGZipFile() { try (GzipCompressorOutputStream gzipOutput = new GzipCompressorOutputStream( new FileOutputStream("output/sample.tar.gz"))) { File tarFile = new File("output/sample.tar"); gzipOutput.write(Files.readAllBytes(tarFile.toPath())); gzipOutput.finish(); } catch (IOException e) { e.printStackTrace(); } } |
BZip2 from existing TAR file
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public static void createTarBZip2File() { try (BZip2CompressorOutputStream gzipOutput = new BZip2CompressorOutputStream( new FileOutputStream("output/sample-bzip2.tar.gz"))) { File tarFile = new File("output/sample.tar"); gzipOutput.write(Files.readAllBytes(tarFile.toPath())); gzipOutput.finish(); } catch (IOException e) { e.printStackTrace(); } } |
XZ from existing TAR file
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public static void createTarXZFile() { try (XZCompressorOutputStream gzipOutput = new XZCompressorOutputStream( new FileOutputStream("output/sample.tar.xz"))) { File tarFile = new File("output/sample.tar"); gzipOutput.write(Files.readAllBytes(tarFile.toPath())); gzipOutput.finish(); } catch (IOException e) { e.printStackTrace(); } } |
Snappy from existing TAR file
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public static void createTarSnappyFile() { try (FramedSnappyCompressorOutputStream gzipOutput = new FramedSnappyCompressorOutputStream( new FileOutputStream("output/sample.tar.sz"))) { File tarFile = new File("output/sample.tar"); gzipOutput.write(Files.readAllBytes(tarFile.toPath())); gzipOutput.finish(); } catch (IOException e) { e.printStackTrace(); } } |
Deflate existing TAR file
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public static void createTarDeflateFile() { try (DeflateCompressorOutputStream gzipOutput = new DeflateCompressorOutputStream( new FileOutputStream("output/sample.tar.deflate"))) { File tarFile = new File("output/sample.tar"); gzipOutput.write(Files.readAllBytes(tarFile.toPath())); gzipOutput.finish(); } catch (IOException e) { e.printStackTrace(); } } |
Here are the files created through above programs.
Here is the GZIP file opened in compression software.
UnGZipping and UnTaring
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
public static void unGZipUnTarFile() { /* * Un GZip file to extract TAR file. */ try (GzipCompressorInputStream archive = new GzipCompressorInputStream( new BufferedInputStream(new FileInputStream("output/sample.tar.gz")))) { OutputStream out = Files.newOutputStream(Paths.get("output/un-gzipped.tar")); IOUtils.copy(archive, out); } catch (IOException e) { e.printStackTrace(); } /* * Untar extracted TAR file */ try (TarArchiveInputStream archive = new TarArchiveInputStream( new BufferedInputStream(new FileInputStream("output/un-gzipped.tar")))) { TarArchiveEntry entry; while ((entry = archive.getNextTarEntry()) != null) { File file = new File("output/" + entry.getName()); System.out.println("Untaring - " + file); // Create directory before streaming files. String dir = file.toPath().toString().substring(0, file.toPath().toString().lastIndexOf("\\")); Files.createDirectories(new File(dir).toPath()); // Stream file content IOUtils.copy(archive, new FileOutputStream(file)); } } catch (IOException e) { e.printStackTrace(); } } |
Below are the ungzipped and untared content.
Further Reading
Apache commons compress | Simplest zip, zip with directory, compression level, unzip
Apache commons compress | SevenZip unSevenZip Examples (.7z)