手上一个项目要实现这个功能,上网上搜了一下还真找到一篇这样的文章,记下来备忘,看代码都几乎是固定格式的^_^原文链接http://blog.csdn.net/zhangjcn/archive/2009/02/14/3891130.aspx
想不到java还有这样的类,很强大! 一开始我还以为要手动调用系统里的winRAR呢,一下是代码,没什么好说的了:
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> 37

38

39
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhangjcn/archive/2009/02/14/3891130.aspx