package com.ilucky.zip;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/**
* @author IluckySi
* @date 20140531
*/
public class ZipUtil {
public String code;
public int buffer;
public String srcPath;
public String dstPath;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public int getBuffer() {
return buffer;
}
public void setBuffer(int buffer) {
this.buffer = buffer;
}
public String getSrcPath() {
return srcPath;
}
public void setSrcPath(String srcPath) {
this.srcPath = srcPath;
}
public String getDstPath() {
return dstPath;
}
public void setDstPath(String dstPath) {
this.dstPath = dstPath;
}
public void startZip () {
boolean next = true;
if(new File(srcPath).exists() == false) {
try {
next = false;
throw new Exception("源路径 " + srcPath + "不存在!");
} catch (Exception e) {
e.printStackTrace();
}
}
String dstDir = dstPath.substring(0, dstPath.lastIndexOf("/"));
if(new File(dstDir).exists() == false) {
try {
next = false;
throw new Exception("目标路径" + dstDir + "不存在!");
} catch (Exception e) {
e.printStackTrace();
}
}
if(next == false) {
return;
}
File zip = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
ZipOutputStream zos = null;
try {
zip = new File(dstPath);
fos = new FileOutputStream(zip);
bos = new BufferedOutputStream(fos);
zos = new ZipOutputStream(bos);
zos.setEncoding(code);
generateZip(srcPath, zos);
} catch (Exception e) {
System.out.println(e.toString());
} finally {
try {
if(zos != null) {
zos.closeEntry();
zos.close();
zos = null;
}
if(bos != null) {
bos.close();
bos = null;
}
if(fos != null) {
fos.close();
fos = null;
}
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
public void generateZip(String filePath, ZipOutputStream zos) {
File file = new File(filePath);
File[] files = null;
try {
if (file.isDirectory()) {
files = file.listFiles();
} else if (file.isFile()) {
files = new File[1];
files[0] = file;
}
for (int i = 0; files != null && i < files.length; i++) {
//关键:zip节点文件名称.
String fileName = files[i].getPath().substring(srcPath.lastIndexOf("/") + 1);
if (files[i].isDirectory()) {
System.out.println("开始压缩目录" + files[i].getPath());
zos.putNextEntry(new ZipEntry(fileName + "/"));
generateZip(files[i].getPath(), zos);
} else {
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
System.out.println("开始压缩文件" + files[i].getPath());
zos.putNextEntry(new ZipEntry(fileName));
fis = new FileInputStream(files[i]);
bis = new BufferedInputStream(fis);
byte[] bytes = new byte[buffer];
int length = 0;
while ((length = bis.read(bytes, 0, buffer)) != -1) {
zos.write(bytes, 0, length);
zos.flush();
}
} catch (Exception e) {
System.out.println(e.toString());
} finally {
try {
if(bis != null) {
bis.close();
bis = null;
}
if(fis != null) {
fis.close();
fis = null;
}
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
使用ant实现zip压缩和解压缩 一
需积分: 50 170 浏览量
2014-06-01
15:42:03
上传
评论
收藏 2.8MB RAR 举报
IluckySi
- 粉丝: 70
- 资源: 24
最新资源
- 基于matlab实现多车辆车辆路径问题,用遗传算法编程,保证可用.rar
- 基于matlab实现多层极限学习机实现手写体识别,准确率超过99%.rar
- 基于matlab实现电力系统最优潮流程序,可以应用于电力市场下的最优潮流计算 适合电力系统专业的同仁使用.rar
- 串行通信的通信协议串口协议串行通信的通信协议串口协议
- Vue构建交互式的单页面应用程序Vue构建交互式的单页面应用程序
- Spring Boot快速搭建和部署应用程序
- Redis开源的高性键值存储系统广泛应用于缓存、实时消息传递
- Qt跨平台的应用程序开发框架Qt跨平台的应用程序开发框架
- 单片机是集成处理器、存储器和输入输出设备
- 深度学习机器习方法模仿人脑神经网络深度学习机器习方法模仿人脑神经网络
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈