1

Possible Duplicate:
How do you gunzip a file and keep the .gz file ?

I decompress the file using following command:

gunzip -f  test.TGZ

This gave me test.tar file but I lost the test.TGZ file. Is there anyway to keep the original file?

EDIT & Update : I am calling the decompress commands through a Java program. The TGZ file contains at least 1 image file, 1 text file and 1 video file.

Java method : execute the command

private static InputStream executeCommand(String command, File workingDir)
        throws Exception {
    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec(command, null, workingDir);
    int exitValue = -1;
    try {
        exitValue = process.waitFor();
    } catch (InterruptedException ignore) {
    }

    if (exitValue != 0) {
        InputStream errStream = process.getErrorStream();
        String errMessage = null;
        if (errStream.available() > 0) {
            byte[] errOutput = new byte[errStream.available()];
            errStream.read(errOutput);
            errMessage = new String(errOutput);
        }

        throw new Exception(
                "Error in ExtractTGZFileTest.executeCommand(command=\""
                        + command + "\") - " + errMessage);
    }

    return process.getInputStream();
}

public static void main(String[] args) {
    try {
        if (args.length == 2 && args[0].equalsIgnoreCase("tgz")) {
            String archiveName = args[1];
            String tarFilnme = archiveName.substring(0, archiveName
                    .length()
                    - ".tgz".length())
                    + ".tar";
            String gzipCommand = "gzip -c -d " + archiveName + " > "
                    + tarFilnme;
            InputStream is = ExtractTGZFileTest.executeCommand(gzipCommand,
                    null);
        } else if (args.length == 2 && args[0].equalsIgnoreCase("tgz1")) {
            String archiveName = args[1];
            String gzipCommand = "gzip  --decompress --name --verbose "
                    + archiveName;
            InputStream is = ExtractTGZFileTest.executeCommand(gzipCommand,
                    null);
        } else {
            System.err.println("Usage: command <file1> ");
            System.exit(1);
        }
        System.out.println("DONE");
    } catch (Exception ex) {
        ex.printStackTrace();
        System.exit(2);
    }
}

Solution : Currently, I copy the TGZ file to temporary location and working with that file.

nayakam
  • 111

3 Answers3

12

Use:

gzip -c -d test.tgz > test.tar

The '-c' option means write to standard output (rather than modifying the input file); the '-d' option means 'decompress'.

You can also use:

gunzip -c test.tgz > test.tar

If you have a modern enough version of GNU 'tar', you can simply use:

tar -xf test.tgz

If you have a slightly older version, you need to specify the compression program:

tar -xzf test.tgz

On those versions, you can use 'bzip2' too:

tar -xf test.tar.bz2 --use-compress-program=bzip2

(On more modern versions, option '-j' can be used to create a bzip2-compressed tar file; the unwrap code determines the correct decompressor automatically.)

3

If you're trying to decompress a GZipped file through Java, you may wish to consider using the java.util.zip.ZipFile class. If you pass this class a File reference to a zip file, you can iterate through your entries and access them as InputStreams, like so:

String archiveName = args[1];
ZipFile zf = new ZipFile(archiveName);
Enumeration<? extends ZipEntry> entries = zf.entries();
ZipEntry nextElement = entries.nextElement();       
InputStream inputStream = zf.getInputStream(nextElement);
// use it: read data, serialise back to file, whatever
1

I agree with Jonathan Leffler, this is a java programmer asking how to access a compressed archive from his/her java program. The fact that it's a novice programmer, trying to do it the wrong way (through the OS), doesn't change that fact that this is a programming question that belongs on StackOverflow.

To answer the original question, doctorruss is correct in suggesting the use of a class. To access a .tar, .tar.gz or .tgz file, you should use a library capable of accessing tar archives. The package com.ice.tar implements a tar archive io package, and it is possible to combine this package with the java.util.zip package to handle .tar.gz files. You can either download directly, or find it in org.apache.tools.tar which is now part of ant.jar.

dan
  • 11