package com.sjs;
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.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class UtilZip {
public static void main(String[] args) throws Exception {
String type = args[0];// 文件压缩类型
// 如果命令行压缩,目前只支持压缩单个档案
if ("zip" == type) {
String src = args[1];// 源文件
String[] srcFile = { src };
String savePath = args[2];// 目标文件目录
String destFile = args[3];// 目标文件名称
// // 调用压缩文件方法
zip(srcFile, savePath, destFile);
} else if ("unzip" == type) {
String savePath = args[1];// 解压文件之后所放置的目录
String destFile = args[2];// 源压缩文件
// 调用压缩文件方法
unZip(destFile, savePath);
} else {
throw new RuntimeException(" $1 must be zip or unzip .");
}
}
/**
*
* @param source
* 源文件
* @param savePath
* 压缩文件路径
* @param destFileName
* 压缩文件名
* @throws Exception
*
* @Description 压缩文件方法
*/
public static void zip(String[] source, String savePath, String destFileName) throws Exception {
// 检查被压缩元素是否存在,并创建目标目录
init(source, savePath, destFileName);
File destFile = new File(savePath, destFileName);
FileInputStream fis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
try {
// 打开目标文件
fos = new FileOutputStream(destFile);
} catch (FileNotFoundException e) {
throw new RuntimeException("open" + destFile.getAbsolutePath() + "operate fail cause " + e);
}
zos = new ZipOutputStream(fos);
try {
for (int i = 0; i < source.length; i++) {
File srcFile = new File(source[i]);
// 将盘符去掉:由“c:/home”变成“home”;即把绝对路径换成相对路径进行压缩
String entryPath = source[i].substring(source[i].indexOf("/") + 1, source[i].length());
if (srcFile.isDirectory()) {
String parent = srcFile.getParent();
System.out.println("parent :" + parent);
// 压缩目录档案
zip(zos, srcFile, parent);
}
// 压缩非目录档案
if (!(srcFile.isDirectory())) {
fis = new FileInputStream(new File(source[i]));
ZipEntry entry = new ZipEntry(entryPath);
zos.putNextEntry(entry);
writeFile(fis, zos);
}
}
} catch (Exception e) {
throw e;
} finally {
try {
if (zos != null) {
zos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
*
* @param zipOutputStream
* 压缩文件输出流
* @param file
* 被压缩文件
* @param src
* 被压缩文件的父目录
*/
private static void zip(ZipOutputStream zipOutputStream, File file, String src) {
if (file.isDirectory()) {
File[] fileList = file.listFiles();
String entryPath = file.getAbsolutePath().substring(src.length()) + "/";
try {
// ##### 对目录进行处理,如果没有此句,则空目录和非空目录都不会被压缩进去
zipOutputStream.putNextEntry(new ZipEntry(entryPath));
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < fileList.length; ++i)
zip(zipOutputStream, fileList[i], src);
} else {
FileInputStream in = null;
try {
String fileName = file.getAbsolutePath();
in = new FileInputStream(file);
String entryPath = fileName.substring(src.length());
// 将此语句解注,配合将“#####”语句注释掉,可以将所有目录去掉,只压缩文件,不会由目录出现(即压缩文件中不会由目录结构)
// entryPath = entryPath.substring(entryPath.lastIndexOf("\\") +
// 1);
ZipEntry entry = new ZipEntry(entryPath);
zipOutputStream.putNextEntry(entry);
writeFile(in, zipOutputStream);
} catch (Exception e) {
throw new RuntimeException("zip file " + file.getAbsolutePath() + " has failed.");
} finally {
if (in != null)
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
// 将数据写入输出流
private static void writeFile(InputStream fis, OutputStream zos) throws IOException {
int length = 0;
byte[] buffer = new byte[1024];
while ((length = fis.read(buffer)) != -1) {
zos.write(buffer, 0, length);
}
zos.flush();
}
/**
*
* @param source
* @param savePath
* @param destFileName
* @Description: 初始化压缩文件的条件
*/
private static void init(String[] source, String savePath, String destFileName) {
if (source == null) {
throw new IllegalArgumentException("the source file can't be null.");
}
if (source.length == 0) {
throw new IllegalArgumentException("the source file is empty.");
}
File sourceFile = null;
for (int i = 0; i < source.length; i++) {
sourceFile = new File(source[i]);
if (!sourceFile.exists()) {
throw new IllegalArgumentException("the file" + source[i] + "is not exist.");
}
}
File destFile = new File(savePath);
if (!destFile.exists()) {
destFile.mkdirs();
}
}
public static String[] unZip(String fileName, String savePath) {
ArrayList<String> fileList = new ArrayList<String>();
File unzipfile = null;
InputStream input = null;
OutputStream output = null;
ZipFile zipFile = null;
try {
zipFile = new ZipFile(new File(fileName));
} catch (Exception e) {
throw new RuntimeException("Zip File has Exception " + e);
}
Enumeration enumeration = zipFile.entries();
try {
do {
ZipEntry entry = (ZipEntry) enumeration.nextElement();
unzipfile = new File(savePath, entry.getName());
input = zipFile.getInputStream(entry);
if (entry.isDirectory()) {
unzipfile.mkdirs();
}
if (!(entry.isDirectory())) {
output = new FileOutputStream(unzipfile);
writeFile(input, output);
try {
if (input != null)
input.close();
if (output != null)
output.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
fileList.add(entry.getName());
} while (enumeration.hasMoreElements());
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (zipFile != null) {
zipFile.close();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
String[] str = new String[fileList.size()];
for (int j = 0; j < fileList.size(); ++j)
str[j] = ((String) fileList.get(j));
return str;
}
}