JSP解压ZIP压缩文件
2014-06-25 00:00
435 查看
手上一个项目要实现这个功能,上网上搜了一下还真找到一篇这样的文章,记下来备忘,看代码都几乎是固定格式的^_^原文链接http://blog.csdn.net/zhangjcn/archive/2009/02/14/3891130.aspx
想不到java还有这样的类,很强大! 一开始我还以为要手动调用系统里的winRAR呢,一下是代码,没什么好说的了:
Code
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
想不到java还有这样的类,很强大! 一开始我还以为要手动调用系统里的winRAR呢,一下是代码,没什么好说的了:
Code
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
相关文章推荐
- JSP解压ZIP压缩文件
- JSP解压ZIP压缩文件
- JSP解压ZIP压缩文件
- java.util.zip 的解压和压缩文件夹
- 7-Zip 是一款免费的文件压缩、解压软件,拥有极高的压缩率
- Zip压缩文件 与 解压
- 使用apache的工具类进行zip(rar等压缩文件)的解压
- php对zip文件解压和压缩--改进
- C#多级文件夹及文件的zip压缩解压
- 使用ZLIB生成DLL文件,并进行有中文的ZIP文件压缩与解压操作!支持目录压与解!
- C#压缩解压zip 文件
- 使用 ZLib 压缩/解压 ZIP 文件
- 使用zipfile解压一个压缩了N次的zip文件
- java实现将ZIP压缩文件解压的工具类
- C#压缩解压zip 文件
- Zip压缩文件 与 解压 (MFC 文件操作 四 )
- SharpZipLib 解压zip压缩文件 (2011/03/28)
- java zip和rar压缩文件的解压
- 使用InfoZip压缩解压zip文件
- 在服务器上的用ZIP压缩或用GZIP压缩的文件,在客户端解压来读