kamranzafar / jtar

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.
https://kamranzafar.org
Apache License 2.0
145 stars 45 forks source link

Suggestion: Support for GNU tar format #9

Open feichh opened 11 years ago

feichh commented 11 years ago

Hello, As mentioned in https://github.com/kamranzafar/jtar/issues/6, jtar is not fully compatible with the GNU tar format. As gnu tar finds use on popular linux distributions such as ubuntu, you may want to consider supporting it in the future. It is possible to create ustar format archives with the gnu tar tool, though it is not the default.

cheers

tux-mind commented 10 years ago

:+1:

tar formats limitations

shawn11ZX commented 8 years ago

Yeah, I met the problem of having my file name truncated to at most 100 characters. This article explains some history background: http://ftp.gnu.org/old-gnu/Manuals/tar/html_mono/tar.html#SEC119

After some searching, I found another alternative that works fine with long file names: Commons-Compress The API interface is pretty much the same with jtar. Below is some code snippet extracted from the internet:

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 org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;

public class UnTarFile {
    final static int BUFFER = 2048;

    /**
     * Command line arguments : argv[0]-----> Source tar.gz file. argv[1]----->
     * DestarInation directory.
     **/
    public static void main(String[] args) throws IOException {

        /** create a TarArchiveInputStream object. **/

        FileInputStream fin = new FileInputStream(args[0]);
        BufferedInputStream in = new BufferedInputStream(fin);
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);

        TarArchiveEntry entry = null;

        /** Read the tar entries using the getNextEntry method **/

        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {

            System.out.println("Extracting: " + entry.getName());

            /** If the entry is a directory, create the directory. **/

            if (entry.isDirectory()) {

                File f = new File(args[1] + entry.getName());
                f.mkdirs();
            }
            /**
             * If the entry is a file,write the decompressed file to the disk
             * and close destination stream.
             **/
            else {
                int count;
                byte data[] = new byte[BUFFER];

                FileOutputStream fos = new FileOutputStream(args[1]
                        + entry.getName());
                BufferedOutputStream dest = new BufferedOutputStream(fos,
                        BUFFER);
                while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.close();
            }
        }

        /** Close the input stream **/

        tarIn.close();
        System.out.println("untar completed successfully!!");
    }

}