压缩/解压采用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);
}
%>
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);
}
%>