Java读取zip文件流并解压

场景

近日测试一个下载接口,该接口返回字节数组形式的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,"表格內容为空");
    }

特点

不生成临时文件,仅在内存层面进行操作

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是Java实现zip文件上传并的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; @WebServlet("/upload") @MultipartConfig // 必须加上此注才能处理文件上传 public class UploadServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取上传的文件 Part filePart = request.getPart("file"); // 获取文件名 String fileName = filePart.getSubmittedFileName(); // 获取文件输入 FileInputStream fileInputStream = (FileInputStream) filePart.getInputStream(); // 创建目标文件 File targetFile = new File(fileName); FileOutputStream fileOutputStream = new FileOutputStream(targetFile); // 将上传的文件写入到目标文件中 byte[] buffer = new byte[1024]; int len; while ((len = fileInputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, len); } fileInputStream.close(); fileOutputStream.close(); // 文件 unzip(targetFile); // 输出成功信息 response.getWriter().println("文件上传并成功!"); } private void unzip(File zipFile) throws IOException { // 创建目录 String unzipDir = zipFile.getName().substring(0, zipFile.getName().lastIndexOf('.')); File unzipDirFile = new File(unzipDir); unzipDirFile.mkdirs(); // 创建ZipInputStream ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFile)); // 循环读取ZipEntry ZipEntry zipEntry = zipInputStream.getNextEntry(); while (zipEntry != null) { String entryName = zipEntry.getName(); String entryPath = unzipDir + File.separator + entryName; if (zipEntry.isDirectory()) { // 如果是目录,则创建目录 new File(entryPath).mkdirs(); } else { // 如果是文件,则写入文件 FileOutputStream fileOutputStream = new FileOutputStream(entryPath); byte[] buffer = new byte[1024]; int len; while ((len = zipInputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, len); } fileOutputStream.close(); } zipEntry = zipInputStream.getNextEntry(); } zipInputStream.closeEntry(); zipInputStream.close(); } } ``` 该代码使用了Servlet 3.0规范的`@MultipartConfig`注来处理文件上传,通过`request.getPart("file")`方法获取上传的文件。然后将文件写入到目标文件中,并调用`unzip()`方法文件。`unzip()`方法使用`ZipInputStream`读取zip文件中的每个`ZipEntry`,如果是目录则创建目录,如果是文件则写入文件完成后,输出成功信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值