Monday, 22 May 2017

Creating tar file and GZipping multiple files - Java Program

If you want to GZIP multiple files that can’t be done directly as you can only compress a single file using GZIP.

For that you will have to archive multiple files into a tar and then compress it to create a .tar.gz compressed file.

Using Apache Commons Compress

Here I am posting a Java program to create a tar file using Apache Commons Compress library. You can download it from here – https://commons.apache.org/proper/commons-compress/download_compress.cgi

Make sure to add commons-compress-xxx.jar in your application’s class path. I have used commons-compress-1.13 version.

Steps for creating tar files

Steps for creating tar files are as follows -

  1. Create a FileOutputStream to the output file (.tar.gz) file.
  2. Create a GZIPOutputStream which will wrap the FileOutputStream object.
  3. Create a TarArchiveOutputStream which will wrap the GZIPOutputStream object.
  4. Then you need to read all the files in a folder.
  5. If it is a directory then just add it to the TarArchiveEntry.
  6. If it is a file then add it to the TarArchiveEntry and also write the content of the file to the TarArchiveOutputStream.

Folder Structure used

Here is a folder structure used in this post to read the files. Test, Test1 and Test2 are directories here and then you have files with in those directories. Your Java code should walk through the whole folder structure and create a tar file with all the entries for the directories and files and then compress it.

Test
  abc.txt
  Test1
     test.txt
     test1.txt
  Test2
     xyz.txt

Example code

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;

public class TarGZIPDemo {

 public static void main(String[] args) {
   String SOURCE_FOLDER = "G:\\Test";
   TarGZIPDemo tGzipDemo = new TarGZIPDemo();
   tGzipDemo.createTarFile(SOURCE_FOLDER);
 }
 
 /**
  * 
  * @param source
  */
 private void createTarFile(String source){
  TarArchiveOutputStream tarOs = null;
  try {
    // Using input name to create output name
    FileOutputStream fos = new FileOutputStream(source.concat(".tar.gz"));
    GZIPOutputStream gos = new GZIPOutputStream(new BufferedOutputStream(fos));
    tarOs = new TarArchiveOutputStream(gos);
    File folder = new File(source);
    File[] fileNames = folder.listFiles();
    for(File file : fileNames){
      System.out.println("PATH " + file.getAbsolutePath());
      System.out.println("File name " + file.getName());
      addFilesToTarGZ(file.getAbsolutePath(), file, tarOs);
    }
   
  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }finally{
    try {
     tarOs.close();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
 }
 /**
  * 
  * @param source
  * @param file
  * @param tos
  * @throws IOException
  */
 private void addFilesToTarGZ(String source, File file, TarArchiveOutputStream tos) 
                   throws IOException{
   // New TarArchiveEntry
   tos.putArchiveEntry(new TarArchiveEntry(file, source));
   if(file.isFile()){
     FileInputStream fis = new FileInputStream(file);
     BufferedInputStream bis = new BufferedInputStream(fis);
     // Write content of the file
     IOUtils.copy(bis, tos);
     tos.closeArchiveEntry();
     fis.close();
   }else if(file.isDirectory()){
     // no need to copy any content since it is
     // a directory, just close the outputstream
     tos.closeArchiveEntry();
     for(File cFile : file.listFiles()){
       // recursively call the method for all the subfolders
       addFilesToTarGZ(cFile.getAbsolutePath(), cFile, tos);
    
     }
   }
  
 }
}

That's all for this topic Creating tar file and GZipping multiple files - Java Program. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Zipping files in Java
  2. Unzipping files in Java
  3. Compressing and Decompressing File in GZIP Format
  4. How to convert a file to byte array

You may also like -

>>>Go to Java Programs page

No comments:

Post a Comment