/**
* zip解压
*
* @param inputFile 待解压文件名
* @param destDirPath 解压路径
*/
public static void ZipUncompress(String inputFile, String destDirPath) throws Exception {
File srcFile = new File(inputFile);//获取当前压缩文件
// 判断源文件是否存在
if (!srcFile.exists()) {
throw new Exception(srcFile.getPath() + "所指文件不存在");
}
//开始解压
//构建解压输入流
ZipInputStream zIn = new ZipInputStream(new FileInputStream(srcFile));
ZipEntry entry = null;
File file = null;
while ((entry = zIn.getNextEntry()) != null) {
if (!entry.isDirectory()) {
OutputStream out = null;
BufferedOutputStream bos = null;
try {
file = new File(destDirPath, entry.getName());
if (!file.exists()) {
new File(file.getParent()).mkdirs();//创建此文件的上级目录
}
out = new FileOutputStream(file);
bos = new BufferedOutputStream(out);
int len = -1;
byte[] buf = new byte[1024];
while ((len = zIn.read(buf)) != -1) {
bos.write(buf, 0, len);
}
// 关流顺序,先打开的后关闭
bos.close();
out.close();
} catch (IOException e) {
} finally {
if (bos != null) {
bos.close();
}
if (out != null) {
out.close();
}
}
}
}
}
public static boolean zip(String srcPath, String zipFileName) {
// System.out.println("zip compressing...");
File srcFile = new File(srcPath);
List<File> fileList = FileUtils.getAllFiles(srcFile);// 所有要压缩的文件
byte[] buffer = new byte[BUFFER];// 缓冲器
ZipEntry zipEntry = null;
int readLength = 0;// 每次读出来的长度
ZipOutputStream zipOutputStream = null;
try {
zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFileName));
for (File file : fileList) {
if (file.isFile()) {// 若是文件,则压缩这个文件
zipEntry = new ZipEntry(getRelativePath(srcPath, file));
zipEntry.setSize(file.length());
zipEntry.setTime(file.lastModified());
zipOutputStream.putNextEntry(zipEntry);
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
while ((readLength = inputStream.read(buffer, 0, BUFFER)) != -1) {
zipOutputStream.write(buffer, 0, readLength);
}
inputStream.close();
// System.out.println("file compressed: " + file.getCanonicalPath());
} else {// 若是目录(即空目录)则将这个目录写入zip条目
zipEntry = new ZipEntry(getRelativePath(srcPath, file) + "/");
zipOutputStream.putNextEntry(zipEntry);
// System.out.println("dir compressed: " + file.getCanonicalPath() + "/");
}
} // end for
zipOutputStream.close();
} catch (FileNotFoundException e) {
// System.out.println(e.getMessage());
SysLog.error(e);
// System.out.println("zip fail!");
return false;
} catch (IOException e) {
// System.out.println(e.getMessage());
SysLog.error(e);
// System.out.println("zip fail!");
return false;
} finally {
try {
if (zipOutputStream != null) {
zipOutputStream.close();
}
} catch (IOException e) {
SysLog.error(e);
}
}
// System.out.println("zip success!");
return true;
}
/**
* 取的给定源目录下的所有文件及空的子目录 递归实现
*
* @param srcFile
* @return
*/
public static List<File> getAllFiles(File srcFile) {
List<File> fileList = new ArrayList<File>();
File[] tmp = srcFile.listFiles();
if (tmp == null) {
return fileList;
}
for (int i = 0; i < tmp.length; i++) {
if (tmp[i].isFile()) {
fileList.add(tmp[i]);
// System.out.println("add file: " + tmp[i].getName());
}
if (tmp[i].isDirectory()) {
if (tmp[i].listFiles().length != 0) {// 若不是空目录,则递归添加其下的目录和文件
fileList.addAll(getAllFiles(tmp[i]));
} else {// 若是空目录,则添加这个目录到fileList
fileList.add(tmp[i]);
// System.out.println("add empty dir: " + tmp[i].getName());
}
}
} // end for
return fileList;
}
/**
* 取相对路径 依据文件名和压缩源路径得到文件在压缩源路径下的相对路径
*
* @param dirPath 压缩源路径
* @param file
* @return 相对路径
*/
private static String getRelativePath(String dirPath, File file) {
File dir = new File(dirPath);
String relativePath = file.getName();
while (true) {
file = file.getParentFile();
if (file == null) {
break;
}
if (file.equals(dir)) {
break;
} else {
relativePath = file.getName() + "/" + relativePath;
}
} // end while
return relativePath;
}