Wednesday, 24 May 2017

How to untar a file - Java Program

This post shows how to untar a tar file. It has both the steps to first uncompress a .tar.gz file and later untar it.

Using Apache Commons Compress

Apache Commons Compress library is used in the code for untarring a file. 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.

Example Code

This Java program has two methods deCompressGZipFile() method is used to uncompress a .tar.gz file to get a .tar file. Using unTarFile() method this .tar file is untarred.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

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

public class UnTarDemo {

    public static void main(String[] args) {
        // Path to input file, which is a 
        // tar file compressed to create gzip file
        String INPUT_FILE = "G:\\Test.tar.gz";
        // This folder should exist, that's where
        // .tar file will go
        String TAR_FOLDER = "G:\\TarFile";
        // After untar files will go to this folder
        String DESTINATION_FOLDER = "G:\\Temp";
        UnTarDemo unTarDemo = new UnTarDemo();
        try {
            File inputFile = new File(INPUT_FILE);
            String outputFile = getFileName(inputFile, TAR_FOLDER);
            System.out.println("outputFile " + outputFile);
            File tarFile = new File(outputFile);
            // Calling method to decompress file
            tarFile = unTarDemo.deCompressGZipFile(inputFile, tarFile);
            File destFile = new File(DESTINATION_FOLDER);
            if(!destFile.exists()){
                 destFile.mkdir();
            }
            // Calling method to untar file
            unTarDemo.unTarFile(tarFile, destFile);
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    /**
     * 
     * @param tarFile
     * @param destFile
     * @throws IOException
     */
    private void unTarFile(File tarFile, File destFile) throws IOException{
        FileInputStream fis = new FileInputStream(tarFile);
        TarArchiveInputStream tis = new TarArchiveInputStream(fis);
        TarArchiveEntry tarEntry = null;
        
        // tarIn is a TarArchiveInputStream
        while ((tarEntry = tis.getNextTarEntry()) != null) {
            File outputFile = new File(destFile + File.separator + tarEntry.getName());
            
            if(tarEntry.isDirectory()){
                
                System.out.println("outputFile Directory ---- " 
                    + outputFile.getAbsolutePath());
                if(!outputFile.exists()){
                    outputFile.mkdirs();
                }
            }else{
                //File outputFile = new File(destFile + File.separator + tarEntry.getName());
                System.out.println("outputFile File ---- " + outputFile.getAbsolutePath());
                outputFile.getParentFile().mkdirs();
                //outputFile.createNewFile();
                FileOutputStream fos = new FileOutputStream(outputFile); 
                IOUtils.copy(tis, fos);
                fos.close();
            }
        }
        tis.close();
    }
    
    /**
     * Method to decompress a gzip file
     * @param gZippedFile
     * @param newFile
     * @throws IOException
     */
    private File deCompressGZipFile(File gZippedFile, File tarFile) throws IOException{
        FileInputStream fis = new FileInputStream(gZippedFile);
        GZIPInputStream gZIPInputStream = new GZIPInputStream(fis);
        
        FileOutputStream fos = new FileOutputStream(tarFile);
        byte[] buffer = new byte[1024];
        int len;
        while((len = gZIPInputStream.read(buffer)) > 0){
            fos.write(buffer, 0, len);
        }
        
        fos.close();
        gZIPInputStream.close();
        return tarFile;
                
    }
    
    /**
     * This method is used to get the tar file name from the gz file
     * by removing the .gz part from the input file
     * @param inputFile
     * @param outputFolder
     * @return
     */
    private static String getFileName(File inputFile, String outputFolder){
        return outputFolder + File.separator + 
                inputFile.getName().substring(0, inputFile.getName().lastIndexOf('.'));
    }
}

That's all for this topic How to untar a file - Java Program. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Creating tar file and GZipping multiple files - Java Program
  2. Zipping files in Java
  3. Unzipping files in Java
  4. Compressing and Decompressing File in GZIP Format
  5. How to read file from the last line in Java

You may also like -

>>>Go to Java Programs page

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

Monday, 1 May 2017

Compressing and Decompressing File in GZIP Format - Java Program

You will mainly use GZIP tool to compress and decompress files in Unix systems. Here note that you can only compress a single file using GZIP not multiple files residing in a folder.

Steps to compress a file

In order to compress a file using GZIP the steps are as follows -

  1. For reading the source file (file which has to be GZIPped) create a FileInputStream.
  2. Create a FileOutputStream to the target file (output GZIPped file).
  3. Create a GZIPOutputStream wrapping the FileOutputStream.
  4. Then you just need to read from the input stream and write to the output stream.

Steps to decompress a file

In order to decompress a file using GZIP the steps are as follows -

  1. For reading the compressed file create a FileInputStream.
  2. Wrap that FileInputStream with in a GZIPInputStream.
  3. Create a FileOutputStream to the new file (created on decompressing GZIP file).
  4. Then you just need to read from the input stream and write to the output stream.

Example code for compressing and decompressing file in GZIP format

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZipDemo {

 public static void main(String[] args) {
  // Path to file which is gzipped
  String SOURCE_FILE = "G:\\Test\\abc.txt";
  // Path to gzipped output files
  String GZIP_OUTPUT_FILE = "G:\\Test\\abc.gz";
  // File you get after decompressings
  String GZIP_NEW_FILE = "G:\\Test\\newabc.txt";
  
  GZipDemo gZipDemo = new GZipDemo();
  try {
   // Compressing a file
   gZipDemo.compressGZipFile(SOURCE_FILE, GZIP_OUTPUT_FILE);
   
   // decompressing a file
   gZipDemo.deCompressGZipFile(GZIP_OUTPUT_FILE, GZIP_NEW_FILE);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }
 /**
  * Method to gzip a file
  * @param sourceFile
  * @param outputFile
  * @throws IOException
  */
 public void compressGZipFile(String sourceFile, String outputFile) 
    throws IOException{
  FileInputStream fis = new FileInputStream(sourceFile);
  FileOutputStream fos = new FileOutputStream(outputFile);
  GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(fos); 
  byte[] buffer = new byte[1024];
  int len;
  while((len = fis.read(buffer)) > 0){
   gZIPOutputStream.write(buffer, 0, len);
  }
  
  fis.close();
  gZIPOutputStream.close();
  
 }
 
 /**
  * Method to decompress a gzip file
  * @param gZippedFile
  * @param newFile
  * @throws IOException
  */
 public void deCompressGZipFile(String gZippedFile, String newFile) 
    throws IOException{
  FileInputStream fis = new FileInputStream(gZippedFile);
  GZIPInputStream gZIPInputStream = new GZIPInputStream(fis);
  FileOutputStream fos = new FileOutputStream(newFile);
  byte[] buffer = new byte[1024];
  int len;
  while((len = gZIPInputStream.read(buffer)) > 0){
   fos.write(buffer, 0, len);
  }
  
  fos.close();
  gZIPInputStream.close();
 }

}

That's all for this topic Compressing and Decompressing File in GZIP Format - 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. Reading all files in a folder - Java Program
  4. Writing file in Java

You may also like -

>>>Go to Java Programs page