ZipUtils

package com.pcss.util;

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;
import org.apache.commons.lang.StringUtils;

import com.ioif.util.Logger;

public class ZipUtils {

private ZipUtils(){
}

public static void doCompress(String srcFile, String newName, String zipFile) throws IOException {
doCompress(new File(srcFile), newName, new File(zipFile));
}

public static void doCompress(String srcFile, String newName, ZipOutputStream out, String dir) throws IOException {
doCompress(new File(srcFile), newName, out, dir);
}

/**
* 文件压缩
* @param srcFile 目录或者单个文件
* @param zipFile 压缩后的ZIP文件
*/
public static void doCompress(File srcFile, String newName, File zipFile) throws IOException {
ZipOutputStream out = null;
try {
out = new ZipOutputStream(new FileOutputStream(zipFile));
doCompress(srcFile, newName, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
out.close();//记得关闭资源
}
}

public static void doCompress(String filelName, String newName, ZipOutputStream out) throws IOException{
doCompress(new File(filelName), newName, out);
}

public static void doCompress(File file, String newName, ZipOutputStream out) throws IOException{
doCompress(file, newName, out, "");
}

public static void doCompress(File inFile, String newName, ZipOutputStream out, String dir) throws IOException {
if ( inFile.isDirectory() ) {
File[] files = inFile.listFiles();
if (files!=null && files.length>0) {
for (File file : files) {
String name = inFile.getName();
if (!"".equals(dir)) {
name = dir + "/" + name;
}
ZipUtils.doCompress(file, newName, out, name);
}
}
} else {
ZipUtils.doZip(inFile, newName, out, dir);
}
}

public static void doZip(File inFile, String newName, ZipOutputStream out, String dir) throws IOException {
String entryName = null;
if (!"".equals(dir)) {
if(StringUtils.isNotBlank(newName)){
entryName = dir + "/" + newName;
}else{
entryName = dir + "/" + inFile.getName();
}
} else {
if(StringUtils.isNotBlank(newName)){
entryName = newName;
}else{
entryName = inFile.getName();
}
}

entryName = java.net.URLDecoder.decode(entryName, "UTF-8"); // 需要转换两次
entryName = java.net.URLDecoder.decode(entryName, "UTF-8");
ZipEntry entry = new ZipEntry(entryName);
out.putNextEntry(entry);
out.setEncoding("gbk");

int len = 0 ;
byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(inFile);
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
out.flush();
}
inFile.getCanonicalPath();
inFile.getAbsolutePath();

Logger.getLogger().info("----------文件压缩完成,文件地址:"+inFile.getAbsolutePath()+" 文件名称:"+entryName);
out.closeEntry();
fis.close();
}

public static void main(String[] args) throws IOException {
//doCompress("F:/secret/","", "F:/secret.zip");

ZipOutputStream out = new ZipOutputStream(new FileOutputStream("D:\\resin-4.0.59-maven\\webapps\\pcfile\\lawDoc\\ABC_20200710 09;39;08.zip"));
doCompress("D:\\resin-4.0.59-maven\\webapps\\pcfile\\lawDoc\\3_20200703102023.txt", "", out, "");
out.close();
}

}
posted @ 2021-08-10 18:11  Zz~  阅读(86)  评论(0编辑  收藏  举报