场景
近日测试一个下载接口,该接口返回字节数组形式的zip文件,于是想利用该字节数组进行zip解压并对其中的文件做进一步解析操作
实现
public void zipParse(byte[] content) throws IOException{
//将包含压缩包信息的字节数组转化为zipInputStream
ZipInputStream zipInputStream= new ZipInputStream(new ByteArrayInputStream(content,0,content.length));
//获得压缩包中第一个文件入口
zipInputStream.getNextEntry();
//读取第一个文件的字节数组
byte[] excelByteArray=zipInputStream.readAllBytes();
zipInputStream.close();
//将第一个文件的字节数组转化为输入流
InputStream input=new ByteArrayInputStream(excelByteArray,0,excelByteArray.length);
//表格解析
Workbook book = WorkbookFactory.create(input);
input.close();
Assert.assertNotNull(book,"表格內容为空");
}
特点
不生成临时文件,仅在内存层面进行操作