在某些项目中,需要对文件进行打包,解包。通过网上学习,写了一个压缩、解压的工具类,方便后续使用,可供参考。代码如下:
package com.icss.dem.sdk;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import com.icss.dem.conf.SystemConfig;
import com.icss.pangu.logging.Log;
import com.icss.pangu.logging.LogFactory;
import com.icss.pangu.util.string.StringUtil;
/**
* <p>Title: 文件打包解包工具类</p>
*/
public class FileZipImpl {
private static Log log = LogFactory.getLog(FileZipImpl.class);
/**
* 数据打包压缩
* @param fileList 文件列表
* @param batchNumber 批次号
* @throws Exception
* @return 返回压缩的文件
*/
public static File zipFile(List fileList, String batchNumber) throws Exception
{
byte b[] = new byte[512];
String fileName = batchNumber+ SystemConfig.ZIP_FILE_TYPE;
log.info("===fileName===" + fileName);
File zipFile = new File("f:/" + fileName);
FileOutputStream fileOutputStream;
ZipOutputStream out = null;
CheckedOutputStream cs = null;
try {
fileOutputStream = new FileOutputStream(zipFile);
// 定义文件输出流检查
cs = new CheckedOutputStream(fileOutputStream,new CRC32());
// 定义输出的zip流对象
out = new ZipOutputStream(new BufferedOutputStream(cs));
for (int i = 0; i < fileList.size(); i++) {
/*
* 此部分代码为传递文件路径
*/
// InputStream in = new FileInputStream((String) fileList.get(i));
// log.info("filePath=="+((String)fileList.get(i)));
// String tempFileName = ((String)fileList.get(i)).replace(File.separatorChar, '/');
//下面部分为传递文件对象
InputStream in = new FileInputStream((File) fileList.get(i));
log.info("filePath=="+((File)fileList.get(i)).getAbsolutePath());
String tempFileName = ((File)fileList.get(i)).getAbsolutePath().replace(File.separatorChar, '/');
tempFileName = tempFileName.substring(tempFileName.indexOf("/") + 1);
ZipEntry e = new ZipEntry(tempFileName);
out.putNextEntry(e);
int len = 0;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
out.closeEntry();
}
out.close();
log.info("文件压缩成功");
} catch (FileNotFoundException e1) {
log.error("文件未找到",e1);
throw new Exception("文件未找到");
} catch (IOException e2) {
log.error("文件IO出现异常",e2);
throw new Exception("文件IO出现异常");
} finally{
if(null != out)
out.close();
if(null != cs)
cs.close();
}
return zipFile;
}
/**
* 解压文件
* @param zipFile 传入的.dem文件
* @return 返回文件列表
*/
public static List unZipFile(ZipFile zipFile)throws Exception
{
List list = new ArrayList();
//临时解压文件目录
String unZipFilePath = SystemConfig.TEMP_UNZIP_PATH;
File tempPath = new File(unZipFilePath);
if(tempPath.exists() != true)
tempPath.mkdir();
String zipFileName = zipFile.getName();
log.info("===zipfileName====" + zipFileName);
if(zipFileName.indexOf(SystemConfig.ZIP_FILE_TYPE) == -1)
{
log.error("所传入文件格式不正确");
throw new Exception("所传入文件格式不正确");
}
try{
Enumeration e = zipFile.entries();
ZipEntry zipEntry = null;
createDirectory(unZipFilePath, "");
while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();
log.info("解压文件名称: " + zipEntry.getName());
if (zipEntry.isDirectory()) {
String name = zipEntry.getName();
name = name.substring(0, name.length() - 1);
File f = new File(unZipFilePath + File.separator + name);
f.mkdir();
} else {
String fileName = zipEntry.getName();
if (fileName.indexOf("/") != -1) {
createDirectory(unZipFilePath, fileName.substring(0,
fileName.lastIndexOf("/")));
}
File unZipFile = new File(unZipFilePath + zipEntry.getName());
unZipFile.createNewFile();
InputStream in = zipFile.getInputStream(zipEntry);
FileOutputStream out = new FileOutputStream(unZipFile);
byte[] by = new byte[1024];
int c;
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
out.close();
in.close();
list.add(unZipFile);
}
}
} catch (FileNotFoundException e1) {
log.error("解压文件未找到",e1);
throw new Exception("解压文件未找到");
} catch (IOException e2) {
log.error("文件IO出现异常",e2);
throw new Exception("文件IO出现异常");
} finally{
}
return list;
}
/**
*
*/
private static void createDirectory(String directory, String subDirectory) {
String dir[];
File fl = new File(directory);
try {
if (subDirectory == "" && fl.exists() != true)
fl.mkdir();
else if (subDirectory != "") {
dir = StringUtil.convertToArray(subDirectory, "/");
for (int i = 0; i < dir.length; i++) {
File subFile = new File(directory + File.separator + dir[i]);
if (subFile.exists() == false)
subFile.mkdir();
directory += File.separator + dir[i];
}
}
} catch (Exception ex) {
}
}
public static void main(String[] args) throws Exception {
List list = new ArrayList();
File file1 = new File("F:/pic/1.jpg");
File file2 = new File("F:/pic/2.jpg");
//String file1="F:/pic/1.jpg";
//String file2="F:/pic/2.jpg";
list.add(file1);
list.add(file2);
try {
//压缩文件
//FileZipImpl.zipFile(list, "123");
//解压文件
FileZipImpl.unZipFile(new ZipFile("F:/123.dem"));
} catch (IOException e) {
// e.printStackTrace();
}
}
}