tags:

views:

827

answers:

3

I am trying to write a function which will accept an InputStream with Zip file data and would return another InputStream with unzipped data.

The Zip file will only contain a single file and thus there is no requirement of creating directories, etc.

I tried looking at ZipInputStream and others but I am confused by so many different types of streams in Java.

A: 

Unless I'm missing something, you should absolutely try and get ZipInputStream to work and there's no reason it shouldn't (I've certainly used it on several occasions).

What you should do is try and get ZipInputStream to work and if you can't, post the code and we'll help you with whatever problems you're having.

Whatever you do though, don't try and reinvent its functionality.

cletus
he seems to have tried that and didn't get how to used it.
Bozho
To be fair, `java.util.zip` is a pretty unpleasant API
skaffman
+6  A: 

GZipinputstream is for streams (or files) ziped as gzip (".gz" extension). It doesn't have any header information.

If you have a real zip file, you have to user ZipFile to open the file, ask for the list of files (one in your example) and ask for the decompressed input stream.

Your method would be something like:

// ITS PSEUDOCODE!!

private InputStream extractOnlyFile(String path) {
   ZipFile zf = new ZipFile(path);
   Enumeration e = zf.entries();
   ZipEntry entry = (ZipEntry) e.nextElement(); // your only file
   return zf.getInputStream(entry);
}

EDIT:

Ok, if you have an inputstream you can use (as cletus says) ZipInputStream. The code would be something like:

// ITS PSEUDOCODE!!

private InputStream extractOnlyFile(InputStream input) {
   ZipInputStream zipinput = new ZipInputStream(input);
   ZipEntry entry = zipinput.getNextEntry(); // reads the header info and position in the begining of the file
   return zipinput;
}

EDIT: working example

Important: if you have the file in your PC you can use ZipFile class to access it randomly

This is a test I've made now in my PC:

import java.io.FileInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;


public class Main {
    public static void main(String[] args) throws Exception
    {
        FileInputStream fis = new FileInputStream("c:/inas400.zip");
        ZipInputStream zis = new ZipInputStream(fis);
        ZipEntry entry;
            // while there are entries I process them
        while ((entry = zis.getNextEntry()) != null)
        {
            System.out.println("entry: " + entry.getName() + ", " + entry.getSize());
                    // consume all the data from this entry
            while (zis.available() > 0)
                zis.read();
                    // I could close the entry, but getNextEntry does it automatically
                    // zis.closeEntry()
        }
    }
}
helios
I corrected the code, the ZipInputStream had to wrap the original input stream :). Thanx!
helios
Helios: zipinput.getNextEntry() will return a ZipEntry object. How do I convert it into a stream?
Baishampayan Ghose
zipinputstream represents an inputstream of the unzipped data of the file. That's why I'm returning "zipinput". But it has to read the headers and position at the beginning of the current zipped data to start. That's why I first call "getnextentry". To make the zipinputstream read that header and prepare to unzip its entry (and of course, to know the zipped filename :).
helios
I've added an example :)
helios
Helios: Thanks for your input so far. I have a question, when you just do a `zis.read()` where does the data go? My zip file will contain only one file in it and I just want to return a stream of the uncompressed file data.
Baishampayan Ghose
Oh, ok. zis.read() (as any InputStream.read) returns (and move forward) one byte. The other read functions work the same way reading more bytes at once. In you case you only have to: 1) get the first entry (it is... don't use a while loop) 2) return the very "zis" object: because it IS the uncompressed input stream you need. The code that works for you is the second block (the first EDIT)
helios
+1  A: 

If you can change the input data I would suggested you to use GZIPInputStream.

GZipInputStream is different from ZipInputStream since you only have one data inside it. So the whole input stream represents the whole file. In ZipInputStream the whole stream contains also the structure of the file(s) inside it, which can be many.

nanda
The file is not in my control. It's a file that I download from a server. I used to save it to disk and then unzip it, but now I am thinking about unzipping it in memory.
Baishampayan Ghose