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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(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...