Java不解压读取.zip中的文件内容
作者:互联网
最近项目中需要拿到.zip文件中的文件内容,之前的做法是先解压到某个目录然后在对里面的文件进行处理,后面发现其实可以不用这么做,jdk中自带的包就可以解决这个问题。示例如下:
public static void main(String[] args) throws IOException {
//获取文件输入流
FileInputStream input = new FileInputStream("C:\\Users\\admin\\Desktop\\test.zip");
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(input), Charset.forName("GBK"));
ZipEntry ze = null;
//循环遍历
while ((ze = zipInputStream.getNextEntry()) != null) {
System.out.println("文件名:" + ze.getName() + " 文件大小:" + ze.getSize() + " bytes");
System.out.println("文件内容:");
//读取
BufferedReader br = new BufferedReader(new InputStreamReader(zipInputStream,Charset.forName("GBK")));
String line;
//内容不为空,输出
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
//一定记得关闭流
zipInputStream.closeEntry();
input.close();
}
标签:解压,zipInputStream,Java,zip,文件,ze,new,null 来源: https://blog.csdn.net/Grady00/article/details/115081342