Zip壓縮解壓縮_已解決中文亂碼
阿新 • • 發佈:2019-02-03
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");
}
}