解压/压缩zip包

压缩/解压采用org.apache.tools.zip包,相对其他压缩/解压包,这个支持中文比较好

1.解压zip文件

//解压文件
public synchronized void unzip(String zipFileName, String extPlace,
boolean whether) throws Exception {
try {
(new File(extPlace)).mkdirs();
File f = new File(zipFileName);
ZipFile zipFile = new org.apache.tools.zip.ZipFile(zipFileName);
//zipFile = new ZipFile(zipFileName);
if ( (!f.exists()) && (f.length() <= 0)) {
throw new Exception("要解压的文件不存在!");
}
String strPath, gbkPath, strtemp;
File tempFile = new File(extPlace);
strPath = tempFile.getAbsolutePath();
java.util.Enumeration e = zipFile.getEntries();
while (e.hasMoreElements()) {
org.apache.tools.zip.ZipEntry zipEnt = (org.apache.tools.zip.ZipEntry)
e.nextElement();
gbkPath = zipEnt.getName();
if (zipEnt.isDirectory()) {
strtemp = strPath + File.separator + gbkPath;
File dir = new File(strtemp);
dir.mkdirs();
continue;
}
else {
//读写文件
InputStream is = zipFile.getInputStream(zipEnt);
BufferedInputStream bis = new BufferedInputStream(is);
gbkPath = zipEnt.getName();
strtemp = strPath + File.separator + gbkPath;

//建目录
String strsubdir = gbkPath;
for (int i = 0; i < strsubdir.length(); i++) {
if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {
String temp = strPath + File.separator + strsubdir.substring(0, i);
File subdir = new File(temp);
if (!subdir.exists()) {
subdir.mkdir();
}
}
}
FileOutputStream fos = new FileOutputStream(strtemp);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int c;
while ( (c = bis.read()) != -1) {
bos.write( (byte) c);
}
bos.close();
fos.close();
}
}
}
catch (Exception e) {
e.printStackTrace();
throw e;
}
}

2.压缩,直接从数据库读出文件列表,动态加入zip文件包中下载,无需服务器端保留文件

<%@page contentType="text/html;charset=GBK" import="com.jspsmart.upload.*"%>
<%@page import="com.tiantium.contractsub.util.*"%>
<%@page import="java.sql.*,java.util.*,java.io.*,oracle.sql.*,java.util.Vector"%>
<%@page import="org.apache.tools.zip.*"%>
<%
Connection conn = null;
PreparedStatement pre = null;
ResultSet rs = null;
String fileName="download.zip";

try {
com.tiantium.contractsub.util.DBUtil dbUtil = new com.tiantium.contractsub.util.DBUtil();
conn = dbUtil.getConnection();

String sql = "select file_name,file_content from contsub_upload_file";

pre = conn.prepareStatement(sql);
// 查询BLOB对象
rs = pre.executeQuery();
//建立压缩输出流
response.setContentType( "application/x-zip-compressed" );
response.setHeader( "Content-Disposition" , "inline; filename="+fileName);
ServletOutputStream sos=response.getOutputStream();
ZipOutputStream outZip=new ZipOutputStream(new BufferedOutputStream(sos));

while (rs.next()) {
//添加文件到zip压缩包
outZip.putNextEntry(new ZipEntry(rs.getString("file_name")));
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("file_content");
InputStream is = blob.getBinaryStream();
byte[] buffer = new byte[4000];
int length;
while ((length = is.read(buffer)) != -1){
outZip.write(buffer, 0, length);
}
is.close();
}
outZip.close();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
DBAssistant.closeRSC(rs, pre, conn);
}

%>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值