ZIP打包工具类
/**
* 开始打包指定文件成指定格式
* @param sourcePath被zip原文件路径(如:f:)
* @param sourceFileName 被zip原文件名(如:a.txt)
* @param zipPath zipPath后生成的文件(如:d:)
* @param zipName zip生成文件名称(如:bb.zip)
* @throws Exception
*/
public static void startZipFile(String sourcePath,String sourceFileName, String zipPath,String zipName){
logger.info("startZipFile sourcePath:"+sourcePath+";sourceFileName:"+sourceFileName+";zipPath:"+zipPath+";zipName:"+zipName);
FileOutputStream fos = null;
ZipOutputStream zos = null;
try {
//存储原文件路径包括文静名称如:F:\\a.txt
String sourceFile="";
//存储打包ZIP文件路径
String zipFile="";
//编码后的原文件路径
String sourceNowFileName="";
//编码后
String zipNowFileName="";
if(sourcePath!=null && !"".equals(sourceFileName)){//源文件路径不为空
if(sourceFileName!=null && !"".equals(sourceFileName)){//源文件名称不为空
if(zipPath!=null && !"".equals(zipPath)){//ZIP生成文件路径不为空
if(zipName!=null && !"".equals(zipName)){//zip生成文件名称不为空
sourceNowFileName=new String(sourceFileName.getBytes(),"UTF-8");
/**
*把文件名称转换为UTF-8格式
*/
zipNowFileName=new String(zipName.getBytes(),"UTF-8");
/**
* 拼接原文件整体路径和zip文件路径
*/
sourceFile=sourcePath+File.separator+sourceNowFileName;
zipFile=zipPath+File.separator+zipNowFileName;
logger.info("sourceFile:"+sourceFile+"\nzipFile:"+zipFile);
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(fos);
//以下两行从项目中取配置的编码中
LinkedHashMap<String, LinkedHashMap<String, String>> codeMap = SystemDataUtil
.getApplicationCodeMap();
LinkedHashMap<String, String> zipCharset=codeMap.get("SYSTEM_ZIP_CHARSET");
//定义变量zipEncoding用来存储压缩编码格式
String zipEncoding="GBK";
if(zipCharset!=null){
zipEncoding=zipCharset.get("1");
}
zos.setEncoding(zipEncoding);
/**
* 开始ZIP文件
*/
zip(new File(sourceFile), "", zos);
}
}
}
}
} catch (FileNotFoundException e) {
logger.error("startZipFile ZIP文件失败",e);
} catch (UnsupportedEncodingException ef) {
// TODO Auto-generated catch block
logger.error("startZipFile ",ef);
} finally {
try {
if (zos != null) {
zos.close();
}
} catch (IOException e) {
logger.error("startZipFile 创建ZIP文件失败",e);
}
}
logger.info("endZipFile sourcePath:"+sourcePath+";sourceFileName:"+sourceFileName+";zipPath:"+zipPath+";zipName:"+zipName);
}
/**
* zip文件
* @param file 打包文件 (源文件)
* @param parentPath(根目录)
* @param zos zipOutPutStream
*/
private static void zip(File file, String parentPath, ZipOutputStream zos) {
if(file.exists()){
if(file.isDirectory()){//处理文件夹
parentPath+=file.getName()+File.separator;
File [] files=file.listFiles();
for(File f:files){
zip(f, parentPath, zos);
}
}else{
FileInputStream fis=null;
DataInputStream dis=null;
try {
fis=new FileInputStream(file);
dis=new DataInputStream(new BufferedInputStream(fis));
logger.info("zipName:"+file.getName());
ZipEntry ze = new ZipEntry(parentPath + file.getName());
zos.putNextEntry(ze);
byte [] content=new byte[1024];
int len;
while((len=fis.read(content))!=-1){
zos.write(content,0,len);
zos.flush();
}
} catch (FileNotFoundException e) {
logger.error("zip创建ZIP文件失败",e);
} catch (IOException e) {
logger.error("zip创建ZIP文件失败",e);
}finally{
try {
if(dis!=null){
dis.close();
}
}catch(IOException e){
logger.error("zip创建ZIP文件失败",e);
}
}
}
}
}

浙公网安备 33010602011771号