java 文件zip打包
2016-05-18 00:00
567 查看
public static boolean zip(String zipFileName, File... files) {
log.info("压缩: "+zipFileName);
ZipOutputStream out = null;
BufferedOutputStream bo = null;
try {
createDir(zipFileName);
out = new ZipOutputStream(new FileOutputStream(zipFileName));
for (int i = 0; i < files.length; i++) {
if (null != files[i]) {
zip(out, files[i], files[i].getName());
}
}
out.close(); // 输出流关闭
log.info("压缩完成");
return true;
} catch (Exception e) {
log.error(e.getMessage());
}
return false;
}
/**
* 执行压缩
* @param out ZIP输入流
* @param f 被压缩的文件
* @param base 被压缩的文件名
*/
private static void zip(ZipOutputStream out, File f, String base) { // 方法重载
try {
if (f.isDirectory()) {//压缩目录
try {
File[] fl = f.listFiles();
if (fl.length == 0) {
out.putNextEntry(new ZipEntry(base + "/")); // 创建zip实体
log.info(base + "/");
}
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + "/" + fl[i].getName()); // 递归遍历子文件夹
}
} catch (IOException e) {
log.error(e.getMessage());
e.printStackTrace();
}
}else{ //压缩单个文件
log.info(base);
out.putNextEntry(new ZipEntry(base)); // 创建zip实体
FileInputStream in = new FileInputStream(f);
BufferedInputStream bi = new BufferedInputStream(in);
int b;
while ((b = bi.read()) != -1) {
out.write(b); // 将字节流写入当前zip目录
}
out.closeEntry(); //关闭zip实体
in.close(); // 输入流关闭
}
} catch (IOException e) {
log.error(e.getMessage());
}
}
/**
* 目录不存在时,先创建目录
* @param zipFileName
*/
private static void createDir(String zipFileName){
String filePath = StringUtils.substringBeforeLast(zipFileName, "/");
File targetFile = new File(filePath);
if (!targetFile.exists()) {//目录不存在时,先创建目录
targetFile.mkdirs();
}
}
log.info("压缩: "+zipFileName);
ZipOutputStream out = null;
BufferedOutputStream bo = null;
try {
createDir(zipFileName);
out = new ZipOutputStream(new FileOutputStream(zipFileName));
for (int i = 0; i < files.length; i++) {
if (null != files[i]) {
zip(out, files[i], files[i].getName());
}
}
out.close(); // 输出流关闭
log.info("压缩完成");
return true;
} catch (Exception e) {
log.error(e.getMessage());
}
return false;
}
/**
* 执行压缩
* @param out ZIP输入流
* @param f 被压缩的文件
* @param base 被压缩的文件名
*/
private static void zip(ZipOutputStream out, File f, String base) { // 方法重载
try {
if (f.isDirectory()) {//压缩目录
try {
File[] fl = f.listFiles();
if (fl.length == 0) {
out.putNextEntry(new ZipEntry(base + "/")); // 创建zip实体
log.info(base + "/");
}
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + "/" + fl[i].getName()); // 递归遍历子文件夹
}
} catch (IOException e) {
log.error(e.getMessage());
e.printStackTrace();
}
}else{ //压缩单个文件
log.info(base);
out.putNextEntry(new ZipEntry(base)); // 创建zip实体
FileInputStream in = new FileInputStream(f);
BufferedInputStream bi = new BufferedInputStream(in);
int b;
while ((b = bi.read()) != -1) {
out.write(b); // 将字节流写入当前zip目录
}
out.closeEntry(); //关闭zip实体
in.close(); // 输入流关闭
}
} catch (IOException e) {
log.error(e.getMessage());
}
}
/**
* 目录不存在时,先创建目录
* @param zipFileName
*/
private static void createDir(String zipFileName){
String filePath = StringUtils.substringBeforeLast(zipFileName, "/");
File targetFile = new File(filePath);
if (!targetFile.exists()) {//目录不存在时,先创建目录
targetFile.mkdirs();
}
}
相关文章推荐
- JAVA HashSet简单代码示例
- JAVA Map简单代码示例
- java 电话归属地
- java,过滤html标签
- 使用SpringBoot,优质快速开发Java项目
- struts2+spring3+hibernate4
- webService(CXF开发SOAP应用,CXF整合spring)未完待续
- 临时设置jdk环境变量
- Java是如何处理别名(aliasing)的
- Java设计模式之代理模式
- Java Map集合知多少?
- Java编程思想第四版第六章学习——访问权限设置
- eclipse自动部署问题
- Java变量名命名规则
- java 接口 & 抽象类
- Exception in thread "main" java.lang.IllegalStateException: EJBCLIENT000025: No EJB receiver availab
- Java字符串中常见的10个问题
- java 解析excle和生成excle文档
- spring一步一步学习
- Spring MVC HTTP Status 406 - 解决方法