package com.my.zip;
import java.io.BufferedInputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipInputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/**
* 文件、文件夹压缩为 zip包 <br />
* zip包解压 <br />
* 依赖jar包 <br />
* <dependency>
* <groupId>org.apache.ant</groupId>
* <artifactId>apache-ant-zip</artifactId>
* <version>1.8.0</version>
* </dependency>
*/
public class Zip {
private static final int BUFFER = 8192;
/**
* 设置压缩编码,解决中文文件名乱码问题
*/
private static final String encoding = System.getProperty("sun.jnu.encoding");
static {
/**
* 解压依据的编码是sun.zip.encoding 所以需要设置 解压编码
*/
System.setProperty("sun.zip.encoding", encoding);
}
private Zip() { }
/**
* 释放资源
* @param resources
*/
private static void closeResource(Closeable... resources){
if(resources==null || resources.length < 1) return;
for (Closeable cur : resources) {
if(cur == null) continue;
if(cur instanceof ZipInputStream){
ZipInputStream zis = (ZipInputStream)cur;
try {
zis.closeEntry();
} catch (IOException e) {
}
}
if(cur instanceof ZipOutputStream){
ZipOutputStream zos = (ZipOutputStream)cur;
try {
zos.closeEntry();
} catch (IOException e) {
}
}
try {
cur.close();
} catch (IOException e) {
}
}
}
/**
* 执行压缩
* @param file
* @param out
* @param basedir
*/
private static void doCompress(File file, ZipOutputStream out, String basedir) {
if (!file.exists()){
return;
}
if (file.isDirectory()) {
//压缩目录
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
doCompress(files[i], out, basedir + file.getName() + "/");
}
} else {
//压缩文件
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
ZipEntry entry = new ZipEntry(basedir + file.getName());
out.putNextEntry(entry);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
} catch (Exception e) {
throw new RuntimeException(e);
}finally{
closeResource(bis);
}
}
}
/**
* 获取当前时间
* @return
*/
private static String getNowStr(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(new Date());
}
/**
* 配置压缩属性
* @param out
*/
private static void configCompressProperties(ZipOutputStream out){
//设置文件名编码方式
out.setEncoding(encoding);
//设置压缩说明
out.setComment("\n压缩时间:"+getNowStr()+" \n");
}
/**
* Zip压缩文件或者文件夹
* @param srcPathName 待压缩文件或者文件夹路径
* @param zipFileName 压缩后压缩文件存放路径
*/
public static void compress(String srcPathName, String zipFileName) {
File file = new File(srcPathName);
File zipFile = new File(zipFileName);
if (!file.exists()){
throw new RuntimeException(srcPathName + "不存在!");
}
boolean isSuccessful = true;
ZipOutputStream out = null;
try {
CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(zipFile), new CRC32());
out = new ZipOutputStream(cos);
//配置压缩属性
configCompressProperties(out);
//执行压缩
doCompress(file, out, "");
} catch (Exception e) {
isSuccessful = false;
throw new RuntimeException(e);
}finally{
closeResource(out);
System.out.println("压缩处理"+(isSuccessful ? "成功!": "失败!"));
}
}
/**
* Zip解压压缩包
* @param zipFilePath 压缩包路基
* @param destDir 解压后存放路径
*/
public static void uncompress(String zipFilePath, String destDir) {
File dir = new File(destDir);
if (!dir.exists()){
dir.mkdirs();
}
boolean isSuccessful = true;
ZipInputStream zis = null;
FileOutputStream fos = null;
byte[] buffer = new byte[BUFFER];
try {
zis = new ZipInputStream(new FileInputStream(zipFilePath));
java.util.zip.ZipEntry ze = zis.getNextEntry();
while (ze != null) {
String fileName = ze.getName();
File newFile = new File(destDir + File.separator + fileName);
new File(newFile.getParent()).mkdirs();
fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
zis.closeEntry();
ze = zis.getNextEntry();
}
} catch (IOException e) {
isSuccessful = false;
throw new RuntimeException(e);
}finally{
closeResource(fos, zis);
System.out.println("解压处理"+(isSuccessful ? "成功!": "失败!"));
}
}
/**
* 测试用例
*/
public static void main(String[] args) {
String srcDir = "D:\\mytestdir";
String tarFile = "D:\\mytestdir.zip";
Zip.compress(srcDir, tarFile);
Zip.uncompress(tarFile, "d:\\mytestdir_uncompress");
}
}
Zip压缩解压缩_已解决中文乱码
最新推荐文章于 2024-07-19 19:41:03 发布