📄 utilzip.java
字号:
package com.zte.webfile.tool;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import com.zte.webfile.dto.FileDTO;
/**
* 实现压缩功能
*
* @author kwatt
*
*/
public class UtilZip {
// 用于访问数据库
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/*
* inputFileName为要压缩的文件的绝对路径名 outputFileName为压缩后文件的绝对路径名
*
*/
public void zip(String inputFileName, String outputFileName) {
try {
// 压缩输出文件流
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
outputFileName));
// 设置输出文件编码
out.setEncoding("gbk");
// 用于接受输入文件
File f = new File(inputFileName);
// 输入文件名
String filename = f.getName();
// 调用递归压缩方法
doZip(out, f, filename);
// 压缩完后关闭输出流
// try {
// out.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
System.out.println("压缩完成!");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 递归压缩方法
*
* @param out
* 压缩输出文件流
* @param f
* 要进行压缩的文件
* @param filename
* 要进行压缩的文件名称
*/
public void doZip(ZipOutputStream out, File f, String filename) {
if (f.isDirectory()) { // 压缩的是文件夹
System.out.println("压缩的是文件夹!");
try {
filename = filename + "\\";
out.putNextEntry(new ZipEntry(filename));
String fileRealPath = f.getAbsolutePath();
fileRealPath = fileRealPath.replace("\\", "/");
// SQL,获得此文件夹的id通过文件夹路径
String sql1 = "select id from f_file where filepath=?";
String id = (String) jdbcTemplate.queryForObject(sql1,
new Object[] { fileRealPath }, String.class);
// 用于存放取出的数据
final List list = new ArrayList();
// SQL,获得此文件夹下的所有文件信息
String sql = "select filepath from f_file where fileparent=?";
jdbcTemplate.query(sql, new Object[] { id },
new RowCallbackHandler() {
public void processRow(ResultSet rs)
throws SQLException {
list.add(rs.getString(1));
}
});
// 遍历此文件夹下的所有文件信息
Iterator it = list.iterator();
while (it.hasNext()) {
File fl = new File((String) it.next());
String fn = fl.getName();
// 调用递归方法压缩文件
doZip(out, fl, filename + fn);
}
} catch (IOException e) {
e.printStackTrace();
}
} else { // 压缩的是文件
System.out.println("现在正在压缩的文件是=====>" + f.getName());
try {
out.putNextEntry(new ZipEntry(filename));
} catch (IOException e) {
e.printStackTrace();
}
try {
FileInputStream in = new FileInputStream(f);
int b;
try {
while ((b = in.read()) != -1) {
out.write(b);
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
/**
* 递归压缩方法
* @param fileList 要压缩的文件集合
* @param outputFileName 输出zip文件路径
*/
public void zip(ArrayList fileList, String outputFileName) {
try {
// 压缩输出文件流
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
outputFileName));
// 设置输出文件编码
out.setEncoding("gbk");
// 用于接受输入文件
Iterator it = fileList.iterator();
while (it.hasNext()) {
FileDTO dto = (FileDTO) it.next();
File file = new File(dto.getFilePath());
doZip(out, file, file.getName());
}
// 压缩完后关闭输出流
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("压缩完成!");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -