tags:

views:

29

answers:

2
int BUFFER_SIZE = 4096;
    byte[] buffer = new byte[BUFFER_SIZE];
    InputStream input = new GZIPInputStream(new FileInputStream("a_gunzipped_file.gz"));
    OutputStream output = new FileOutputStream("current_output_name");
    int n = input.read(buffer, 0, BUFFER_SIZE);
    while (n >= 0) {
        output.write(buffer, 0, n);
        n = input.read(buffer, 0, BUFFER_SIZE);
    }

    }catch(IOException e){
            System.out.println("error: \n\t" + e.getMessage());
    }

Using the above code I can succesfully extract a gzip's contents although the extracted file's filenames are, as expected, will always be current_output_name (I know its because I declared it to be that way in the code). My problem is I dont know how to get the file's filename when it is still inside the archive.

Though, java.util.zip provides a ZipEntry, I couldn't use it on gzip files. Any alternatives?

A: 

GZipped files don't contain any meta information, so the filename is not stored inside the file. That's why most gzipped files have the original extension in their name: name.original_extension.gz

FRotthowe
+1  A: 

Gzip is purely compression. There is no archive, it's just the file's data, compressed.

The convention is for gzip to append .gz to the filename, and for gunzip to remove that extension. So, logfile.txt becomes logfile.txt.gz when compressed, and again logfile.txt when it's decompressed. If you rename the file, the name information is lost.

Michael Borgwardt
oh sorry about my dumbness, my bad i didn't read about gz first. thanks for pointing this out
lock