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
Kamran 70d0b56fce Merge pull request #20 from zamblauskas/preserve_file_permissions
Read and preserve actual file permissions mode in `TarHeader`.
2015-09-06 23:18:07 +01:00
src Check if OS isPosix, if it is - use PosixFilePermission, otherwise use standard Java file methods to check permissions, e.g. canExecute. 2015-09-02 23:36:41 +03:00
.gitignore first v2.0 commit 2012-05-01 16:29:50 +01:00
.travis.yml Create .travis.yml 2013-10-17 13:15:59 +01:00
LICENSE.txt first v2.0 commit 2012-05-01 16:29:50 +01:00
pom.xml Allow TarOutputStream to append to an existing tar file. This is 2014-01-23 12:16:07 +01:00
README.md Update README.md 2013-10-17 13:37:27 +01: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 and also supports UStar format.

Build Status

Usage

JTar is available in maven central and can be added as a dependency in the maven project.

  <dependency>
    <groupId>org.kamranzafar</groupId>
    <artifactId>jtar</artifactId>
    <version>2.2</version>
  </dependency>

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.

Examples and resources

  • See JTarTest class, provided with the source, for more detailed examples.
  • Visit the wiki page for more details on Tar format

JTar is available in Maven Central