Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Dec 17 '09 at 1:24

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

marked as duplicate by Gnoupi, Diago 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
5  
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: superuser.com/questions/45650/… –  Gnoupi Jan 17 '10 at 11:20

3 Answers 3

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.

share|improve this answer

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

share|improve this answer
    
Thanks lot, I will try these command through my program. –  Thillakan Dec 14 '09 at 5:37
    
for "gunzip -c test.tgz > test.tar" command : program hangs when it trying to exec command –  Thillakan Dec 14 '09 at 8:26
1  
@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 :) . –  Thillakan Dec 15 '09 at 1:06

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
share|improve this answer
    
This class handles PK-Zip format, not gzip format. –  Kevin Panko Dec 18 '09 at 20:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.