Link to home
Start Free TrialLog in
Avatar of Michael Lam
Michael Lam

asked on

How to create a tar.gz file from a list of files

I have a List<String> files of file paths that I want to create a tar.gz from these files.  Can someone help me?  Thanks.
Avatar of sachiek
sachiek
Flag of Singapore image

One option
import java.io.FileInputStream;

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

public class Taread {
    public static void main(String[] args) {
        try {
            TarArchiveInputStream tarInput = new TarArchiveInputStream(new FileInputStream(args[0]));
            TarArchiveEntry entry;
            while (null!=(entry=tarInput.getNextTarEntry())) {
                System.out.println(entry.getName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Open in new window

Using Zipinputstream

TarInputStream tis = new TarInputStream(new FileInputStream("myfile.tar"));
try
{
    TarEntry entry;
    do
    {
        entry = tis.getNextEntry();

        //Do something with the entry
    }
    while (entry != null);
}
finally
{
    tis.close();
}

Open in new window

Avatar of Michael Lam
Michael Lam

ASKER

Sorry, I want to tar/compress a list of files, and then gzip it, not the other way around.
Also I am using jdk 1.7.
Here you go..use this bit of code.. You will be able to create tar.gz

protected void makeCompress(String srcFile) throws IOException {  
    File outFile = new File(srcFile + ".tar");  
    List<File> list = new ArrayList<>(1);  
    File file = new File(srcFile);  
    list.add(file);  
    compressFiles(list, outFile);         
}  
   
private void compressFiles(List<File> list, File outFile) throws IOException {  
    try (TarArchiveOutputStream taos = new TarArchiveOutputStream(
            new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(outFile))))) {
 
        taos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);  
        taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);  
           
        for (File f : list) {
            System.out.println("send to cmpress: " + f.getName());  
            addFileToCompression(taos, f, ".");  
        }  
    } // all streams are automatically closed here, whether an exception occurs or not
}  
   
private void addFileToCompression(TarArchiveOutputStream taos, File f, String dir) throws IOException {  
    System.out.println(f.getName() + " dir : " + dir);  
    TarArchiveEntry tae = new TarArchiveEntry(f, dir + "/");          
    taos.putArchiveEntry(tae);  
    if (f.isDirectory()) {  
        /*TarArchiveEntry tae = new TarArchiveEntry(f, dir + "/");       
        taos.putArchiveEntry(tae);*/ 
        System.out.println("is a directory");  
        taos.closeArchiveEntry();  
        for (File childFile : f.listFiles()) {  
            System.out.println("child: " + childFile.getName());  
            addFileToCompression(taos, childFile, dir + "/" + f.getName() + "/" + childFile.getName());  
        }  
    } else {  
        System.out.println("is a file " + f.getName());  
        try (FileInputStream fis = new FileInputStream(f)) {
            IOUtils.copy(fis, taos);  
        }  
        System.out.println("file added");  
        taos.flush();  
        taos.closeArchiveEntry();  
    }
}

Open in new window

Line no.2..update with this below code.

File outFile = new File(srcFile + ".tar.gz");

Open in new window

the way the TarArchiveEntry is created - without the "/" added to the entry name. With the "/" I get a .tar.gz file with only folders, not files.
makeCompress() is not being used at all.  I am trying this:

 public static void main( String[] args ) throws IOException, InterruptedException
    {
    		List<File> fileList = new ArrayList<File>();
    		fileList.add(new File("/Users/212353126/Documents/Renewables/pub/appdata/data/temp/ba_11600002_1446535324358.csv"));
    		fileList.add(new File("/Users/212353126/Documents/Renewables/pub/appdata/data/temp/mk_11600025_1446535324358.csv"));

    		TarGZDemo demo = new TarGZDemo();
    		demo.compressFiles(fileList, new File("/Users/212353126/Documents/Renewables/pub/appdata/data/temp/test.tar.gz"));
    }

Open in new window


I do get a .tar.gz file, but when I do tar -zxvf test.tar.gz I get an empty error.
ASKER CERTIFIED SOLUTION
Avatar of sachiek
sachiek
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial