Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Support for GNU tar format #9

Open
feichh opened this issue Jun 6, 2013 · 2 comments
Open

Suggestion: Support for GNU tar format #9

feichh opened this issue Jun 6, 2013 · 2 comments

Comments

@feichh
Copy link

feichh commented Jun 6, 2013

Hello,
As mentioned in #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
Copy link

👍

tar formats limitations

@shawn11ZX
Copy link

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!!");
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants