Java压缩文件
来源:互联网 发布:京东商城与淘宝的区别 编辑:程序博客网 时间:2024/05/17 00:51
public class FileService
{
/**
* 压缩文件
* @param strFilePath
*/
public static void doZipFile(String idKey,String sourceFilePath,String targetFilePath,String zipName)throws IOException,FileNotFoundException{
//定义压缩后的文件名称
if(StringUtils.isEmpty(sourceFilePath)|| StringUtils.isEmpty(targetFilePath) || StringUtils.isEmpty(zipName)){
return;
}
ZipOutputStream zos = null;
try {
File resourcefile = new File(sourceFilePath+"\\"+idKey);
File targetfile = null;
if("type1.zip".equals(zipName)){
targetfile = new File(targetFilePath+"\\type1\\"+idKey);
}else if("type2.zip".equals(zipName)){
targetfile = new File(targetFilePath+"\\type2\\"+idKey);
}
if(!resourcefile.exists()){
resourcefile.mkdirs();
}
if(!targetfile.exists()){
targetfile.mkdirs();
}
FileOutputStream fos = new FileOutputStream(targetfile+"\\"+zipName);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
zos.setEncoding("GBK");
createZipFile(zos,resourcefile,"");
} catch (FileNotFoundException e) {
EMPLog.log("FileService->压缩文件", EMPLog.ERROR, 0, "失败: "+e.getMessage());
}finally{
if(zos!=null){
try {
zos.close();
} catch (IOException e) {
EMPLog.log("FileService->压缩文件", EMPLog.ERROR, 0, "失败: "+e.getMessage());
}
}
}
EMPLog.log("FileService->压缩文件", EMPLog.INFO, 0, "成功");
}
public static void createZipFile(ZipOutputStream out,File file,String dir) throws IOException,
FileNotFoundException{
//如果当前的是文件夹,则进行进一步处理
if(file.isDirectory()){
//得到文件列表信息
File[] files = file.listFiles();
try {
out.putNextEntry(new ZipEntry(dir+"/"));
dir = dir.length()==0?"":dir+"/";
//循环将文件夹中的文件打包
for(int i=0;i<files.length;i++){
createZipFile(out,files[i],new String((dir+files[i].getName())));
}
} catch (IOException e) {
EMPLog.log("FileService->压缩文件", EMPLog.ERROR, 0, "失败: "+e.getMessage());
}
}else{//当前的是文件,打包处理
//文件输入流
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
out.putNextEntry(new ZipEntry(dir));
//进行写操作
int n=0;
byte[] buffer = new byte[1024];
while((n=fis.read(buffer))>0){
out.write(buffer, 0,n);
}
} catch (FileNotFoundException e) {
EMPLog.log("FileService->压缩文件", EMPLog.ERROR, 0, "失败: "+e.getMessage());
}catch (IOException e) {
EMPLog.log("FileService->压缩文件", EMPLog.ERROR, 0, "失败: "+e.getMessage());
}finally{
//关闭输入流
if(null != fis){
try {
fis.close();
} catch (IOException e) {
EMPLog.log("FileService->压缩文件", EMPLog.ERROR, 0, "失败: "+e.getMessage());
}
}
}
}
}
public static void main(String args[]){
try {
FileService.doZipFile("140","D:\\upload\\type1", "D:\\upload", "type1.zip");
} catch (FileNotFoundException e) {
System.out.println("压缩失败...");
} catch (IOException e) {
System.out.println("压缩失败...");
}
}
}
- Java压缩文件和解压缩文件
- java 压缩文件
- java压缩文件
- java压缩文件
- java 压缩文件
- java 压缩文件
- JAVA压缩文件
- java压缩文件
- JAVA 压缩文件
- java压缩文件
- java 压缩文件
- java压缩文件
- java 压缩文件
- java压缩文件
- Java压缩文件
- java压缩文件
- Java 压缩文件
- java压缩文件
- 位移运算 特点
- MYSQL C API 入门教程
- PIE 二分加贪心
- 【C++】类 与继承
- win10 安装sqlserver 2008R2 出现问题:性能计数器注册表配置单元一致性
- Java压缩文件
- Linux C 第八课
- win+Apache+mod_wsgi配置mapproxy心得(二)
- (OK) How To Install Wine 1.9.9 (Development Release) on CentOS, RHEL & Fedora
- 使用SQLite本地数据库实现注册登陆功能
- iOS UIAlertController弹窗效果
- tjut 4937
- Y2K Accounting Bug
- 【工具软件合集】