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
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門屿笼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人翁巍,你說我怎么就攤上這事驴一。” “怎么了灶壶?”我有些...
- 文/不壞的土叔 我叫張陵肝断,是天一觀的道長。 經(jīng)常有香客問我驰凛,道長胸懈,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任恰响,我火速辦了婚禮箫荡,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘渔隶。我一直安慰自己羔挡,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布间唉。 她就那樣靜靜地躺著绞灼,像睡著了一般。 火紅的嫁衣襯著肌膚如雪呈野。 梳的紋絲不亂的頭發(fā)上低矮,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼终议!你這毒婦竟也來了汇竭?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬榮一對情侶失蹤穴张,失蹤者是張志新(化名)和其女友劉穎细燎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體皂甘,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡玻驻,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了偿枕。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片璧瞬。...
- 正文 年R本政府宣布,位于F島的核電站膘茎,受9級特大地震影響桃纯,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜披坏,卻給世界環(huán)境...
- 文/蒙蒙 一态坦、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧棒拂,春花似錦伞梯、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至攻旦,卻和暖如春喻旷,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背牢屋。 一陣腳步聲響...
- 正文 我出身青樓锋谐,卻偏偏與公主長得像遍尺,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子怀估,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 聲明:算法和數(shù)據(jù)結(jié)構(gòu)的文章均是作者從github上翻譯過來狮鸭,為方便大家閱讀。如果英語閱讀能力強(qiáng)的朋友多搀,可以直接到s...
- error code(錯(cuò)誤代碼)=0是操作成功完成歧蕉。error code(錯(cuò)誤代碼)=1是功能錯(cuò)誤。error c...
- Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...