Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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()];
            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
                    - ".tgz".length())
                    + ".tar";
            String gzipCommand = "gzip -c -d " + archiveName + " > "
                    + tarFilnme;
            InputStream is = ExtractTGZFileTest.executeCommand(gzipCommand,
        } 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,
        } else {
            System.err.println("Usage: command <file1> ");
    } catch (Exception ex) {

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

share|improve this question

migrated from Dec 17 '09 at 1:24

This question came from our site for professional and enthusiast programmers.

marked as duplicate by Gnoupi, BinaryMisfit Jan 17 '10 at 11:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Looks like a post for SuperUser – monksy Dec 14 '09 at 5:25
Au contraire (it does not belong on SU): a relevant part of handling open source programs is knowing how to handle compressed tar files - a part of programming. – Jonathan Leffler Dec 14 '09 at 5:29
If we take away the programming part, this is a duplicate in here:… – Gnoupi Jan 17 '10 at 11:20


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.)

share|improve this answer
Thanks lot, I will try these command through my program. – T.S Dec 14 '09 at 5:37
for "gunzip -c test.tgz > test.tar" command : program hangs when it trying to exec command – T.S Dec 14 '09 at 8:26
@Thillakan: That's because you cannot call "exec" with a redirect (the > symbol). You could either capture the output of the gunzip call (using popen or setting up pipes yourself) or use one of the tar commands instead. If you don't care about when/if the program exists, you can use system("gunzip -c test.tgz > test.tar") since the system function accepts the redirect symbol. – Isak Savo Dec 14 '09 at 11:55
@Isak, Thanks to clear up the issue. I am going to stick with my current approach. It is easy for me :) . – T.S Dec 15 '09 at 1:06

If you're trying to decompress a GZipped file through Java, you may wish to consider using the 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
share|improve this answer
This class handles PK-Zip format, not gzip format. – Kevin Panko Dec 18 '09 at 20:53

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 implements a tar archive io package, and it is possible to combine this package with the package to handle .tar.gz files. You can either download directly, or find it in which is now part of ant.jar.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged .