Concetti
GZipinputstream è per i flussi (o file) ziped come gzip (estensione ".gz"). Non ha alcuna informazione di intestazione.
GZipInputStream is for [zippeddata]
Se si dispone di un file zip vero e proprio, è necessario all'utente ZipFile di aprire il file, per chiedere l'elenco dei file (uno nel tuo esempio) e chiedere il flusso di input decompresso.
ZipFile is for a file with [header information + zippeddata]
Il tuo metodo, se avete il file, sarebbe qualcosa di simile:
// 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);
}
Leggendo un InputStream con il contenuto di un file .zip
Ok, se avete un InputStream che puoi usare (come dice @cletus) ZipInputStream. Legge uno stream che include i dati di intestazione.
ZipInputStream is for a stream with [header information + zippeddata]
Importante: se avete il file nel vostro PC è possibile utilizzare ZipFile
classe per accedere a caso
Questo è un esempio di lettura di un file zip attraverso un InputStream:
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");
// this is where you start, with an InputStream containing the bytes from the zip file
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()
}
}
}
Cosa avete già provato? Si prega di aggiungere qualche esempio di codice. –