使用apache旗下的commons-compress 压缩和解压zip文件
可以参考我的前一篇博客:http://hw1287789687.iteye.com/blog/1976309
以下是我封装的一个工具类,专门负责zip的压缩和解压
CompressZipUtil:
package com.common.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import com.io.hw.file.util.FileUtils;
import com.string.widget.util.ValueWidget;
import com.swing.messagebox.GUIUtil23;
/***
* use apache
* commons-compress,http://www.cnblogs.com/un4sure/archive/2011/09/27/
* 2193298.html,http://hw1287789687.iteye.com/blog/1976309
*
* @author huangwei
* @since 2013-11-18
*/
public final class CompressZipUtil {
private CompressZipUtil() {
throw new Error("Don't let anyone instantiate this class.");
}
/***
*
* @param zipOut
* @param filepath
* :要压缩的单个文件
* @throws IOException
*/
public static void addEntry(ZipArchiveOutputStream zipOut, String filepath)
throws IOException {
File singleFile = new File(filepath);
if (singleFile.isDirectory()) {
return;
}
ZipArchiveEntry zipEntry2 = new ZipArchiveEntry(singleFile,
SystemUtil.getFileSimpleName(filepath));
zipOut.putArchiveEntry(zipEntry2);
FileInputStream fin = new FileInputStream(filepath);
// 不要关闭zipOut,关闭之前要执行closeArchiveEntry()
FileUtils.writeIn2Output(fin, zipOut, false, true);
}
/***
*
* @param zipOut
* @param filepath
* : 要压缩的单个文件
* @throws IOException
*/
public static void addEntry(ZipArchiveOutputStream zipOut, File filepath)
throws IOException {
if (filepath.isDirectory()) {
return;
}
ZipArchiveEntry zipEntry2 = new ZipArchiveEntry(filepath,
filepath.getName());
zipOut.putArchiveEntry(zipEntry2);
FileInputStream fin = new FileInputStream(filepath);
// 不要关闭zipOut,关闭之前要执行closeArchiveEntry()
FileUtils.writeIn2Output(fin, zipOut, false, true);
}
/***
*
* @param zipOut
* @param filePaths
* :要压缩的文件
* @throws IOException
*/
public static void compressZip(ZipArchiveOutputStream zipOut,
@SuppressWarnings("rawtypes") List filePaths) throws IOException {
for (Object fileObj : filePaths) {
if (fileObj instanceof File) {
File file = (File) fileObj;
addEntry(zipOut, file);
} else {
addEntry(zipOut, (String) fileObj);
}
}
zipOut.closeArchiveEntry();
zipOut.flush();
zipOut.finish();
zipOut.close();
}
/***
* 压缩.
*
* @param zipFile
* :压缩的结果--zip文件
* @param filePaths
* @throws IOException
* @throws ArchiveException
*/
public static void compressZip(File zipFile,
@SuppressWarnings("rawtypes") List filePaths) throws IOException,
ArchiveException {
FileOutputStream fou = new FileOutputStream(zipFile);
ArchiveOutputStream archOuts = new ArchiveStreamFactory()
.createArchiveOutputStream(ArchiveStreamFactory.ZIP, fou);
if (archOuts instanceof ZipArchiveOutputStream) {
ZipArchiveOutputStream zipOut = (ZipArchiveOutputStream) archOuts;
compressZip(zipOut, filePaths);
}
}
/***
*
* @param zipFile
* :压缩的结果--zip文件
* @param filePaths
* @throws IOException
* @throws ArchiveException
*/
public static void compressZip(String zipFile,
@SuppressWarnings("rawtypes") List filePaths) throws IOException,
ArchiveException {
compressZip(new File(zipFile), filePaths);
}
/***
* compress single file.
*
* @param zipFile
* @param filepath
* :single file
* @throws IOException
* @throws ArchiveException
*/
public static void compressSingeFile(String zipFile, String filepath)
throws IOException, ArchiveException {
List filePaths = new ArrayList();
filePaths.add(filepath);
compressZip(zipFile, filePaths);
}
/***
* compress single file.
*
* @param zipFile
* @param filepath
* :single file
* @throws IOException
* @throws ArchiveException
*/
public static void compressSingeFile(String zipFile, File filepath)
throws IOException, ArchiveException {
List filePaths = new ArrayList();
filePaths.add(filepath);
compressZip(zipFile, filePaths);
}
/***
* 压缩指定文件夹中的所有文件.
*
* @param zipFile
* @param folderPaths
* @throws IOException
* @throws ArchiveException
*/
public static void compressZip(String zipFile, String folderPaths)
throws IOException, ArchiveException {
List list = FileUtils.getListFiles(new File(folderPaths));
compressZip(zipFile, list);
}
/***
* 压缩指定文件夹中的所有文件.
*
* @param zipFile
* @param folderPaths
* @throws IOException
* @throws ArchiveException
*/
public static void compressZip(String zipFile, File folderPaths)
throws IOException, ArchiveException {
List list = FileUtils.getListFiles(folderPaths);
compressZip(zipFile, list);
}
/***
* 解压zip
*
* @param zipFile
* @param decompressLoc
* :解压之后的文件所在目录
* @throws ArchiveException
* @throws IOException
*/
public static boolean decompress(String zipFile, File decompressLoc)
throws ArchiveException, IOException {
FileInputStream fin = new FileInputStream(zipFile);
ArchiveInputStream archOuts = new ArchiveStreamFactory()
.createArchiveInputStream(ArchiveStreamFactory.ZIP, fin);
ZipArchiveInputStream zipIn = (ZipArchiveInputStream) archOuts;
ZipArchiveEntry zipEntry;
while (!ValueWidget.isNullOrEmpty(zipEntry = zipIn.getNextZipEntry())) {
String fileName = zipEntry.getName();
File singFile = new File(decompressLoc, fileName);
if (singFile.exists()) {//若解压后的文件已经存在,则直接退出
GUIUtil23.warningDialog("File \"" + singFile.getAbsolutePath()
+ "\" does exist.");
zipIn.close();
// break;
return false;
}
System.out.println(fileName);
FileUtils.writeIn2Output(zipIn, new FileOutputStream(singFile),
true, false);
}
zipIn.close();
return true;
}
/***
* 解压zip
*
* @param zipFile
* @param decompressLoc
* :解压之后的文件所在目录
* @throws ArchiveException
* @throws IOException
*/
public static boolean decompress(String zipFile, String decompressLocStr)
throws ArchiveException, IOException {
File decompressLoc = new File(decompressLocStr);
return decompress(zipFile, decompressLoc);
}
}
附件:
(1)依赖的jar包 io0007-find_progess(包含源码)
(2)例子:目前只能压缩和解压一级目录的zip文件,zip_mgmt-0.0.1-SNAPSHOT.jar
执行方式 java -jar zip_mgmt-0.0.1-SNAPSHOT.jar: