import com.google.common.collect.Lists;
import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import java.io.*;
import java.util.Enumeration;
import java.util.List;
/**
* <p>User: Zhang Kaitao
* <p>Date: 13-6-14 下午3:37
* <p>Version: 1.0
*/
public class CompressUtils {
public static final void zip(String compressPath, String[] needCompressPaths) {
File compressFile = new File(compressPath);
List<File> files = Lists.newArrayList();
for (String needCompressPath : needCompressPaths) {
File needCompressFile = new File(needCompressPath);
if (!needCompressFile.exists()) {
continue;
}
files.add(needCompressFile);
}
try {
ZipArchiveOutputStream zaos = null;
try {
zaos = new ZipArchiveOutputStream(compressFile);
zaos.setUseZip64(Zip64Mode.AsNeeded);
zaos.setEncoding("GBK");
for (File file : files) {
addFilesToCompression(zaos, file, "");
}
} catch (IOException e) {
throw e;
} finally {
IOUtils.closeQuietly(zaos);
}
} catch (Exception e) {
FileUtils.deleteQuietly(compressFile);
throw new RuntimeException("压缩失败", e);
}
}
private static void addFilesToCompression(ZipArchiveOutputStream zaos, File file, String dir) throws IOException {
ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, dir + file.getName());
zaos.putArchiveEntry(zipArchiveEntry);
if (file.isFile()) {
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
IOUtils.copy(bis, zaos);
zaos.closeArchiveEntry();
} catch (IOException e) {
throw e;
} finally {
IOUtils.closeQuietly(bis);
}
} else if (file.isDirectory()) {
zaos.closeArchiveEntry();
for (File childFile : file.listFiles()) {
addFilesToCompression(zaos, childFile, dir + file.getName() + File.separator);
}
}
}
public static void unzip(String path, String descPath, boolean override) {
File uncompressFile = new File(path);
File descPathFile = null;
try {
descPathFile = new File(descPath);
unzipFolder(uncompressFile, descPathFile, override);
} catch (RuntimeException e) {
throw e;
}
}
@SuppressWarnings("unchecked")
private static void unzipFolder(File uncompressFile, File descPathFile, boolean override) {
ZipFile zipFile = null;
try {
zipFile = new ZipFile(uncompressFile, "GBK");
Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
while (entries.hasMoreElements()) {
ZipArchiveEntry zipEntry = entries.nextElement();
String name = zipEntry.getName();
name = name.replace("\\", "/");
File currentFile = new File(descPathFile, name);
//非覆盖 跳过
if(currentFile.isFile() && currentFile.exists() && !override) {
continue;
}
if(name.endsWith("/")) {
currentFile.mkdirs();
continue;
} else {
currentFile.getParentFile().mkdirs();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(currentFile);
InputStream is = zipFile.getInputStream(zipEntry);
IOUtils.copy(is, fos);
} finally {
IOUtils.closeQuietly(fos);
}
}
} catch (IOException e) {
throw new RuntimeException("解压缩失败", e);
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException e) {
}
}
}
}
public static void main(String[] args) {
String[] needCompress = new String[]{
"E:/test/abc",
"E:/test/after.pdf",
"E:/test/test.txt"
};
zip("E:/testZip.zip", needCompress);
unzip("E:/testZip.zip", "E:\\新建文件夹",true);
}
}
压缩解压CompressUtils
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 声明:算法和数据结构的文章均是作者从github上翻译过来,为方便大家阅读。如果英语阅读能力强的朋友,可以直接到s...
- Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...