(这是转载过来的,使用的是apache的ZIP,挺好用,我自己加了几个方法)
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
public class ZipUtil {
public final static int BUFFER_SIZE = 1024 * 8;
/**
* 打成zip压缩包
*
* @param dirPath
* 源文件夹路径
* @param toZipPath
* 解压后存放文件目录
*/
public static void doZip(String dirPath, String toZipPath) {
File dir = null;
ZipOutputStream zipOut = null;
String zipDirName = ""; // 存储生成的zip包的路径
String parentPath = null;
try {
dir = new File(dirPath);
zipDirName = getZipPath(dir.getName(), toZipPath);
parentPath = dir.getParent();
zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipDirName)));
doZipHandlerDir(dir, zipOut, parentPath);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != zipOut) {
try {
zipOut.flush();
zipOut.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
zipOut = null;
}
}
}
}
/**
* 获得zip存储路径
*
* @param dirName
* @param toZipPath
* @return
*/
private static String getZipPath(String dirName, String toZipPath) {
String zipDirName = "";
if (toZipPath != null && !"".equals(toZipPath.trim())) {
zipDirName = toZipPath + File.separator;
File newDir = new File(zipDirName);
if (!newDir.exists()) {
newDir.mkdirs();
}
}
zipDirName += dirName + ".zip";
return zipDirName;
}
/**
* 递归完成目录下文件读取
*
* @param dir
* @param zipOut
* @throws Exception
*/
private static void doZipHandlerDir(File dir, ZipOutputStream zipOut, String parentPath) throws Exception {
File[] files = dir.listFiles();// 获得目录下的所有文件(包括目录和文件)
byte[] buffer = new byte[BUFFER_SIZE];// 缓存大小
if (files.length == 0) {// 如果目录为空另行创建
zipOut.putNextEntry(new ZipEntry(handlerFilePath(dir.toString(), parentPath) + File.separator));
zipOut.closeEntry();
} else {// 如果目录下不为空 则分别处理目录和文件
for (File file : files) {
if (file.isDirectory()) {// 目录情况递归遍历
doZipHandlerDir(file, zipOut, parentPath);
} else {// 文件情况读文件 并写入到zip包中
doZipWriteFile(file, zipOut, parentPath, buffer);
}
}
}
zipOut.flush();
zipOut.close();
}
/**
* 向zip包中写入文件
*
* @param file
* 文件对象
* @param zipOut
* zip输出流
* @param parentPath
* 父目录路径
* @param buffer
* 缓存
* @throws Exception
* 向上抛出异常
*/
private static void doZipWriteFile(File file, ZipOutputStream zipOut, String parentPath, byte[] buffer)
throws Exception {
FileInputStream fis = new FileInputStream(file);
zipOut.putNextEntry(new ZipEntry(handlerFilePath(file.toString(), parentPath)));
int length = 0;// 读取字节长度
while ((length = fis.read(buffer)) > 0) {
zipOut.write(buffer, 0, length);
}
zipOut.closeEntry();
fis.close();
}
/**
* 处理路径 将绝对路径处理成相对路径 否则zip包中会出现绝对路径下的每一层目录
*
* @param realPath
* 绝度路径
* @param parentPath
* 需要去掉的父路径
* @return 处理后的相对路径
* @throws Exception
* 找不到父路径时抛出异常
*/
private static String handlerFilePath(String realPath, String parentPath) throws Exception {
int index = -1;
index = realPath.indexOf(parentPath);
if (index == -1) {
throw new Exception("路径错误");
}
return realPath.substring(index + parentPath.length());
}
/**
* 解压缩文件
*
* @param unZipPath
* 要解压缩的zip文件路径 (路径+文件名)
* @param toUnZipPath
* 解压后存放的路径
*/
public static void unZip(String unZipPath, String toUnZipPath) {
ZipFile zipFile = null;
FileOutputStream outStream = null;
InputStream inputStream = null;
File file = null;
try {
zipFile = new ZipFile(unZipPath, "GB2312");
for (Enumeration<ZipEntry> entities = zipFile.getEntries(); entities.hasMoreElements();) {// 遍历zip包下的zip条目
ZipEntry zipEntry = entities.nextElement();
file = new File(getUnZipPath(zipEntry.getName(), toUnZipPath));
mkdirs(zipEntry, file);
inputStream = zipFile.getInputStream(zipEntry);// 从zip条目获得输入流
outStream = new FileOutputStream(file);// 获得写入磁盘的输出流
write2Disk(outStream, inputStream);
}
zipFile.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (outStream != null) {
outStream.flush();
outStream.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 将zip包中某个文件写入到指定文件
*
* @param zipPath
* zip路径
* @param fileName
* 某个文件
* @param toFilePath
* 指定文件路径
*/
public static void writeToFile(String zipPath, String fileName, String toFilePath) {
ZipFile zipFile = null;
InputStream inputStream = null;
FileOutputStream outStream = null;
ZipEntry zipEntry = null;
File file = null;
try {
zipFile = new ZipFile(zipPath, "UTF-8");
// 遍历zip包下的zip条目
for (Enumeration<ZipEntry> entities = zipFile.getEntries(); entities.hasMoreElements();) {
zipEntry = (ZipEntry) entities.nextElement();
String zipEntryName = zipEntry.getName().substring(zipEntry.getName().lastIndexOf(File.separator));
if (fileName.equals(zipEntryName))
break;
}
if (null != zipEntry) {
inputStream = zipFile.getInputStream(zipEntry);// 从zip条目获得输入流
file = new File(toFilePath);
outStream = new FileOutputStream(file);// 获得写入磁盘的输出流
write2Disk(outStream, inputStream);
zipFile.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
inputStream = null;
}
try {
if (outStream != null) {
outStream.flush();
outStream.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
outStream = null;
}
}
}
/**
* 获取zip包中的条目列表
*
* @param zipPath
* @return
*/
public static List<ZipEntry> getZipEntrys(String zipPath) {
ZipFile zipFile = null;
List<ZipEntry> list = new ArrayList<ZipEntry>();
try {
zipFile = new ZipFile(zipPath, "UTF-8");
ZipEntry zipEntry = null;
for (Enumeration<ZipEntry> entities = zipFile.getEntries(); entities.hasMoreElements();) {// 遍历zip包下的zip条目
zipEntry = (ZipEntry) entities.nextElement();
list.add(zipEntry);
}
zipFile.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
/**
* 解压过程中创建目录
*
* @param zipEntry
* zip条目
* @param file
* 解压的文件夹或者文件
*/
private static void mkdirs(ZipEntry zipEntry, File file) {
if (zipEntry.isDirectory()) {
file.mkdirs();
} else {
File parent = file.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
}
}
/**
* 解压后写到磁盘
*
* @param outStream
* 输出流
* @param inputStream
* 读文件的输入流
* @throws IOException
* io异常
*/
private static void write2Disk(FileOutputStream outStream, InputStream inputStream) throws IOException {
int length = 0;
byte[] buffer = new byte[BUFFER_SIZE];
while ((length = inputStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
outStream.flush();
outStream.close();
inputStream.close();
}
/**
* 获得解压后存放路径
*
* @param zipName
* zip条目名
* @param toUnZipPath
* 解压路径
* @return
*/
private static String getUnZipPath(String zipName, String toUnZipPath) {
String unZipPath = "";// 解压后存储路径
if (toUnZipPath != null && !"".equals(toUnZipPath)) {
unZipPath = toUnZipPath + File.separator;
}
unZipPath += zipName;
return unZipPath;
}
/**
* 修改文件后缀并返回修改后文件名
*
* @param filePath
* @param from
* @param to
* @return
*/
public static String updateFileSuffix(String filePath, String from, String to) {
File file = new File(filePath);
String name = file.getName();
if (name.endsWith(from)) {
File newFile = new File(file.getParent() + "/" + name.substring(0, name.indexOf(from)) + to);
file.renameTo(newFile);
return newFile.getName();
}
return name;
}
/**
* 获取文件后缀
*
* @param fileName
* @return
*/
public static String getFileSuffix(String fileName) {
return fileName.substring(fileName.lastIndexOf("."));
}
/**
* 解压缩文件并返回文件条目和对应的输入流
*
* @param unZipPath
* 要解压缩的zip文件路径 (路径+文件名)
*/
public static Map<String, InputStream> unZipToStream(String unZipPath) {
Map<String, InputStream> zipMap = new HashMap<String, InputStream>();
ZipFile zipFile = null;
InputStream inputStream = null;
try {
zipFile = new ZipFile(unZipPath, "GB2312");
if(null != zipFile){
for (Enumeration<ZipEntry> entities = zipFile.getEntries(); entities.hasMoreElements();) {// 遍历zip包下的zip条目
ZipEntry zipEntry = entities.nextElement();
if(null == zipEntry){
continue;
}
String entryName = zipEntry.getName();
inputStream = zipFile.getInputStream(zipEntry);// 从zip条目获得输入流
zipMap.put(entryName, inputStream);
}
zipFile.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally{
inputStream = null;
}
}
return zipMap;
}
public static void main(String[] args) {
Map<String, InputStream> zipMap = unZipToStream("C:/Users/Administrator/Desktop/test2.zip");
Set<Entry<String, InputStream>> entrySet = zipMap.entrySet();
for (Entry<String, InputStream> entry : entrySet) {
// System.out.println(entry.getKey());
// System.out.println(entry.getValue());
}
}
}