JTar is a simple Java Tar library, that provides an easy way to create and read tar files using IO streams. The API is very simple to use and similar to the java.util.zip package.
Go to file
2024-03-14 16:37:14 -07:00
gradle/wrapper Update gradle 2024-03-14 16:11:38 -07:00
src/main/java/org/kamranzafar/jtar Support entries larger than 8GiB 2024-03-14 16:37:14 -07:00
.gitignore Update gradle 2024-03-14 16:11:38 -07:00
build.gradle Update gradle 2024-03-14 16:11:38 -07:00
gradlew Update gradle 2024-03-14 16:11:38 -07:00
gradlew.bat Update gradle 2024-03-14 16:11:38 -07:00
LICENSE.txt first v2.0 commit 2012-05-01 16:29:50 +01:00
README.md Update README.md 2019-04-02 01:36:28 -04:00
settings.gradle Convert to gradle project 2019-04-01 23:03:38 -04:00

Overview

JTar is a simple Java Tar library, that provides an easy way to create and read tar files using IO streams. The API is very simple to use and similar to the java.util.zip package. This repo is forked from kamranzafar/jtar with modifications to build on Android and switch to use GNU USTAR tar header.

Usage

JTar is available on Jitpack and can be added as a dependency.

repositories {
    ...
    maven { url 'https://jitpack.io' }
}
dependencies {
    implementation 'com.github.topjohnwu:jtar:1.0.0'
}

Below are some examples of using jtar in applications

Tar example - using TarOutputStream

  // Output file stream
  FileOutputStream dest = new FileOutputStream( "c:/test/test.tar" );
  
  // Create a TarOutputStream
  TarOutputStream out = new TarOutputStream( new BufferedOutputStream( dest ) );
  
  // Files to tar
  File[] filesToTar=new File[2];
  filesToTar[0]=new File("c:/test/myfile1.txt");
  filesToTar[1]=new File("c:/test/myfile2.txt");
  
  for(File f:filesToTar){
     out.putNextEntry(new TarEntry(f, f.getName()));
     BufferedInputStream origin = new BufferedInputStream(new FileInputStream( f ));
     int count;
     byte data[] = new byte[2048];
  
     while((count = origin.read(data)) != -1) {
        out.write(data, 0, count);
     }
  
     out.flush();
     origin.close();
  }
  
  out.close();

Untar example - using TarInputStream

  String tarFile = "c:/test/test.tar";
  String destFolder = "c:/test/myfiles";
  
  // Create a TarInputStream
  TarInputStream tis = new TarInputStream(new BufferedInputStream(new FileInputStream(tarFile)));
  TarEntry entry;
  
  while((entry = tis.getNextEntry()) != null) {
     int count;
     byte data[] = new byte[2048];
     FileOutputStream fos = new FileOutputStream(destFolder + "/" + entry.getName());
     BufferedOutputStream dest = new BufferedOutputStream(fos);
  
     while((count = tis.read(data)) != -1) {
        dest.write(data, 0, count);
     }
  
     dest.flush();
     dest.close();
  }
  
  tis.close();

Tip: Always use buffered streams with jtar to speed up IO.