Java读取zip文件内容

public static void main(String[] args)throws Exception{
    String filePath = "D:\\test\\test.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();
}

 

posted @ 2021-06-22 19:49  愿无违  阅读(869)  评论(0编辑  收藏  举报