Java實(shí)現(xiàn)的zip壓縮及解壓縮工具類示例
本文實(shí)例講述了Java實(shí)現(xiàn)的zip壓縮及解壓縮工具類。分享給大家供大家參考,具體如下:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
public class ZipUtil {
private static final int BUFFEREDSIZE = 1024;
/**
* 壓縮文件
*
* @param zipFileName
* 保存的壓縮包文件路徑
* @param filePath
* 需要壓縮的文件夾或者文件路徑
* @param isDelete
* 是否刪除源文件
* @throws Exception
*/
public void zip(String zipFileName, String filePath, boolean isDelete) throws Exception {
zip(zipFileName, new File(filePath), isDelete);
}
/**
* 壓縮文件
*
* @param zipFileName
* 保存的壓縮包文件路徑
* @param inputFile
* 需要壓縮的文件夾或者文件
* @param isDelete
* 是否刪除源文件
* @throws Exception
*/
public void zip(String zipFileName, File inputFile, boolean isDelete) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
if (!inputFile.exists()) {
throw new FileNotFoundException("在指定路徑未找到需要壓縮的文件!");
}
zip(out, inputFile, "", isDelete);
out.close();
}
/**
* 遞歸壓縮方法
*
* @param out
* 壓縮包輸出流
* @param f
* 需要壓縮的文件
* @param base
* 壓縮的路徑
* @param isDelete
* 是否刪除源文件
* @throws Exception
*/
private void zip(ZipOutputStream out, File inputFile, String base, boolean isDelete) throws Exception {
if (inputFile.isDirectory()) { // 如果是目錄
File[] inputFiles = inputFile.listFiles();
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < inputFiles.length; i++) {
zip(out, inputFiles[i], base + inputFiles[i].getName(), isDelete);
}
} else { // 如果是文件
if (base.length() > 0) {
out.putNextEntry(new ZipEntry(base));
} else {
out.putNextEntry(new ZipEntry(inputFile.getName()));
}
FileInputStream in = new FileInputStream(inputFile);
try {
int len;
byte[] buff = new byte[BUFFEREDSIZE];
while ((len = in.read(buff)) != -1) {
out.write(buff, 0, len);
}
} catch (IOException e) {
throw e;
} finally {
in.close();
}
}
if (isDelete) {
inputFile.delete();
}
}
/**
* 解壓縮
*
* @param zipFilePath
* 壓縮包路徑
* @param fileSavePath
* 解壓路徑
* @param isDelete
* 是否刪除源文件
* @throws Exception
*/
public void unZip(String zipFilePath, String fileSavePath, boolean isDelete) throws Exception {
try {
(new File(fileSavePath)).mkdirs();
File f = new File(zipFilePath);
if ((!f.exists()) && (f.length() <= 0)) {
throw new Exception("要解壓的文件不存在!");
}
ZipFile zipFile = new ZipFile(f);
String strPath, gbkPath, strtemp;
File tempFile = new File(fileSavePath);// 從當(dāng)前目錄開(kāi)始
strPath = tempFile.getAbsolutePath();// 輸出的絕對(duì)位置
Enumeration<ZipEntry> e = zipFile.getEntries();
while (e.hasMoreElements()) {
org.apache.tools.zip.ZipEntry zipEnt = e.nextElement();
gbkPath = zipEnt.getName();
if (zipEnt.isDirectory()) {
strtemp = strPath + File.separator + gbkPath;
File dir = new File(strtemp);
dir.mkdirs();
continue;
} else {
// 讀寫(xiě)文件
InputStream is = zipFile.getInputStream(zipEnt);
BufferedInputStream bis = new BufferedInputStream(is);
gbkPath = zipEnt.getName();
strtemp = strPath + File.separator + gbkPath;
// 建目錄
String strsubdir = gbkPath;
for (int i = 0; i < strsubdir.length(); i++) {
if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {
String temp = strPath + File.separator + strsubdir.substring(0, i);
File subdir = new File(temp);
if (!subdir.exists())
subdir.mkdir();
}
}
FileOutputStream fos = new FileOutputStream(strtemp);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int len;
byte[] buff = new byte[BUFFEREDSIZE];
while ((len = bis.read(buff)) != -1) {
bos.write(buff, 0, len);
}
bos.close();
fos.close();
}
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
if (isDelete) {
new File(zipFilePath).delete();
}
}
// public static void main(String[] args) {
// ZipUtil cpr = new ZipUtil();
// try {
// cpr.zip("C:/Users/Lenovo User/Desktop/test中文.zip", "C:/Users/Lenovo User/Desktop/新建文件夾", false);
// cpr.unZip("C:/Users/Lenovo User/Desktop/test中文.zip", "C:/Users/Lenovo User/Desktop/新建文件夾2", false);
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// }
}
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
java單點(diǎn)登錄(SSO)的實(shí)現(xiàn)
SSO是指在多個(gè)應(yīng)用系統(tǒng)中個(gè),用戶只需要登陸一次就可以訪問(wèn)所有相互信任的應(yīng)用系統(tǒng),本文主要介紹了java單點(diǎn)登錄的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2021-07-07
關(guān)于springboot中的自定義配置項(xiàng)
這篇文章主要介紹了關(guān)于springboot中的自定義配置項(xiàng),在項(xiàng)目開(kāi)發(fā)的過(guò)程中,經(jīng)常需要自定義系統(tǒng)業(yè)務(wù)方面的配置文件及配置項(xiàng),Spring Boot提供了@value注解、@ConfigurationProperties注解和Environment接口等3種方式自定義配置項(xiàng),需要的朋友可以參考下2023-07-07
Java實(shí)現(xiàn)redis分布式鎖的三種方式
本文主要介紹了Java實(shí)現(xiàn)redis分布式鎖的三種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
idea?maven依賴引入失效無(wú)法正常導(dǎo)入依賴問(wèn)題的解決方法
有時(shí)候idea導(dǎo)入一個(gè)新項(xiàng)目,或者pom文件修改(新增)了依賴,pom文件和代碼會(huì)報(bào)紅,提示依賴包不存在,下面這篇文章主要給大家介紹了關(guān)于idea?maven依賴引入失效無(wú)法正常導(dǎo)入依賴問(wèn)題的解決方法,需要的朋友可以參考下2023-04-04
mybatisplus中EntityWrapper的常用方法
這篇文章主要介紹了mybatisplus中EntityWrapper的常用方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Spring Security實(shí)現(xiàn)登錄認(rèn)證實(shí)戰(zhàn)教程
這篇文章主要介紹了Spring Security實(shí)現(xiàn)登錄認(rèn)證實(shí)戰(zhàn)教程,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-06-06

