我在SUN网站(http://java.sun.com/developer/technicalArticles/Programming/compression/)上找到了示例,但它返回了BufferedOutputStream。但我想将ZipEntry文件作为InputStream,然后处理下一个文件。那可能吗?我的程序无法访问硬盘,因此甚至无法临时保存文件。
import java.io.*;
import java.util.zip.*;
public class UnZip {
final int BUFFER = 2048;
public static void main (String argv[]) {
try {
BufferedOutputStream dest = null;
FileInputStream fis = new
FileInputStream(argv[0]);
ZipInputStream zis = new
ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
while((entry = zis.getNextEntry()) != null) {
System.out.println("Extracting: " +entry);
int count;
byte data[] = new byte[BUFFER];
// write the files to the disk
FileOutputStream fos = new
FileOutputStream(entry.getName());
dest = new
BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER))
!= -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}
zis.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:13)
好吧,只需将写入文件的部分更改为您想要对数据执行的操作。
while((entry = zis.getNextEntry()) != null) {
System.out.println("Extracting: " + entry);
int count;
byte data[] = new byte[BUFFER];
string filename = entry.getName();
System.out.println("Filename: " + filename);
while ((count = zis.read(data, 0, BUFFER)) != -1) {
// Do whatever you want with the data variable
System.out.println(data);
}
}