Các khái niệm
GZipinputstream là dành cho dòng (hoặc file) ziped như gzip ("gz" mở rộng). Nó không có bất kỳ thông tin tiêu đề nào.
GZipInputStream is for [zippeddata]
Nếu bạn có tệp zip thực, bạn phải sử dụng tệp ZipFile để mở tệp, yêu cầu danh sách tệp (một ví dụ) và yêu cầu luồng nhập giải nén.
ZipFile is for a file with [header information + zippeddata]
phương pháp của bạn, nếu bạn có các tập tin, sẽ là một cái gì đó như:
// 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);
}
Đọc một InputStream với nội dung của một tập tin .zip
Ok, nếu bạn có một InputStream bạn có thể sử dụng (như @cletus nói) ZipInputStream. Nó đọc một luồng bao gồm dữ liệu tiêu đề.
ZipInputStream is for a stream with [header information + zippeddata]
Quan trọng: nếu bạn có các tập tin trong máy tính của bạn, bạn có thể sử dụng ZipFile
lớp để truy cập nó một cách ngẫu nhiên
Đây là một mẫu của việc đọc một zip-file thông qua một 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()
}
}
}
gì đã bạn đã thử? Vui lòng thêm một số ví dụ về mã. –