读取zip文件的内容,压缩包可包含多个文件。
public static void main(String[] args)throws Exception{
String filePath = "D:\\fileTest\\fileTest.zip";
InputStream inputStream = new BufferedInputStream(new FileInputStream(filePath));
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
if (zipEntry.isDirectory()) {
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (true) {
int bytes = zipInputStream.read();
if (bytes == -1) {
break;
}
baos.write(bytes);
}
baos.close();
System.out.println(String.format("Name:%s,Content:%s",zipEntry.getName(),new String(baos.toByteArray())));
}
}
zipInputStream.closeEntry();
zipInputStream.close();
}