JSP解压ZIP压缩文件

来源:互联网 发布:安妮宝贝老公 知乎 编辑:程序博客网 时间:2024/06/08 09:06
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <%@ page import="java.io.*,java.util.zip.*"%>  
  3. <html>  
  4.     <head>  
  5.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  6.         <title>Insert title here</title>  
  7.     </head>  
  8.     <body>  
  9.         <%  
  10.             try {  
  11.                 ZipInputStream in = new ZipInputStream(new FileInputStream(application.getRealPath("/") + "1.zip"));  
  12.                 ZipEntry entry = null;  
  13.                 while ((entry = in.getNextEntry()) != null) {  
  14.                     String entryName = entry.getName();  
  15.                     if (entry.isDirectory()) {  
  16.                         File file = new File(application.getRealPath("/") + entryName);  
  17.                         file.mkdirs();  
  18.                         System.out.println("创建文件夹" + entryName);  
  19.                     } else {  
  20.                         FileOutputStream os = new FileOutputStream(application.getRealPath("/") + entryName);  
  21.                         // Transfer bytes from the ZIP file to the output file  
  22.                         byte[] buf = new byte[1024];  
  23.                         int len;  
  24.                         while ((len = in.read(buf)) > 0) {  
  25.                             os.write(buf, 0, len);  
  26.                         }  
  27.                         os.close();  
  28.                         in.closeEntry();  
  29.                     }  
  30.                 }  
  31.             } catch (IOException e) {  
  32.             }  
  33.             out.println("解压文件成功!");  
  34.         %>  
  35.     </body>  
  36. </html>  
原创粉丝点击