Java壓縮文件夾最實(shí)用簡單的方法
Java 有一個(gè)很好的類庫來處理 zip 文件。這些類在 java.util.zip 包中可用。以下 Java 示例程序展示了如何使用 java.util.zip 類創(chuàng)建整個(gè)文件夾的 zip。我們使用Files.walkFileTree遞歸地瀏覽目錄樹,然后將每個(gè)文件添加到新創(chuàng)建的 zip 文件中。請(qǐng)注意,此示例僅適用于 Java 1.7 及更高版本。
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
// Source code to create a zip file from a given folder
// This example program recursively adds all files in the folder
// Works only with Java 7 and above
public class ZipFolder {
public static void main(String[] args) throws Exception {
ZipFolder zf = new ZipFolder();
// Use the following paths for windows
//String folderToZip = "c:\\demo\\test";
//String zipName = "c:\\demo\\test.zip";
// Linux/mac paths
String folderToZip = "/Users/jj/test";
String zipName = "/Users/jj/test.zip";
zf.zipFolder(Paths.get(folderToZip), Paths.get(zipName));
}
// Uses java.util.zip to create zip file
private void zipFolder(Path sourceFolderPath, Path zipPath) throws Exception {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipPath.toFile()));
Files.walkFileTree(sourceFolderPath, new SimpleFileVisitor() {
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
zos.putNextEntry(new ZipEntry(sourceFolderPath.relativize(file).toString()));
Files.copy(file, zos);
zos.closeEntry();
return FileVisitResult.CONTINUE;
}
});
zos.close();
}
}在 linux/mac 中,您可以使用以下命令測試新創(chuàng)建的 zip 文件
解壓-t test.zip
實(shí)例擴(kuò)展
//方法1:
public void unZip(String zipfile) throws IOException {
//檢查是否是zip文件,并判斷文件是否存在
checkFileName(zipfile);
long startTime = System.currentTimeMillis();
File zfile=new File(zipfile);
//獲取待解壓文件的父路徑
String Parent=zfile.getParent()+"/";
FileInputStream fis=new FileInputStream(zfile);
Charset charset = Charset.forName("GBK");//默認(rèn)UTF-8
// CheckedInputStream cis = new CheckedInputStream(fis,new CRC32());
ZipInputStream zis = new ZipInputStream(fis,charset);// 輸入源zip路徑
ZipEntry entry=null;
BufferedOutputStream bos=null;
while ((entry=zis.getNextEntry())!=null) {
if (entry.isDirectory()) {
File filePath=new File(Parent+entry.getName());
//如果目錄不存在,則創(chuàng)建
if (!filePath.exists()) {
filePath.mkdirs();
}
}else{
FileOutputStream fos=new FileOutputStream(Parent+entry.getName());
bos=new BufferedOutputStream(fos);
byte buf[] = new byte[1024];
int len;
while ((len = zis.read(buf)) != -1) {
bos.write(buf, 0, len);
}
zis.closeEntry();
//關(guān)閉的時(shí)候會(huì)刷新
bos.close();
}
}
zis.close();
long endTime = System.currentTimeMillis();
System.out.println("解壓完成!所需時(shí)間為:"+(endTime-startTime)+"ms");
// System.out.println("校驗(yàn)和:"+cis.getChecksum().getValue());
}
private void checkFileName(String name) {
//文件是否存在
if (!new File(name).exists()) {
System.err.println("要解壓的文件不存在!");
System.exit(1);
}
// 判斷是否是zip文件
int index = name.lastIndexOf(".");
String str=name.substring(index+1);
if (!"zip".equalsIgnoreCase(str)) {
System.err.println("不是zip文件,無法解壓!");
System.exit(1);
}
}
到此這篇關(guān)于Java壓縮文件夾最實(shí)用簡單的方法的文章就介紹到這了,更多相關(guān)Java壓縮文件夾的方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring中@Controller和@RestController的區(qū)別詳解
這篇文章主要介紹了Spring中@Controller和@RestController的區(qū)別詳解,@RestController?是?@Controller?和?@ResponseBody?的結(jié)合體,單獨(dú)使用?@RestController?的效果與?@Controller?和?@ResponseBody?二者同時(shí)使用的效果相同,需要的朋友可以參考下2023-10-10
MyBatis 動(dòng)態(tài)拼接Sql字符串的問題
MyBatis的動(dòng)態(tài)SQL,解決了SQL字符串拼接的痛苦。下文分步驟給大家詳細(xì)介紹了MyBatis 動(dòng)態(tài)拼接Sql字符串的問題,非常不錯(cuò),感興趣的朋友一起看下吧2016-08-08
為什么SpringMVC中請(qǐng)求的body不支持多次讀取
這篇文章主要介紹了為什么SpringMVC中請(qǐng)求的body不支持多次讀取,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Java注解處理器學(xué)習(xí)之編譯時(shí)處理的注解詳析
編譯時(shí)注解相信對(duì)每一個(gè)java開發(fā)者來說都不陌生,下面這篇文章主要給大家介紹了關(guān)于Java注解處理器學(xué)習(xí)之編譯時(shí)處理的注解的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧2018-05-05
詳解Java圖形化編程中的鼠標(biāo)事件設(shè)計(jì)
這篇文章主要介紹了Java圖形化編程中的鼠標(biāo)事件設(shè)計(jì),是Java的GUI開發(fā)中的基礎(chǔ)部分,需要的朋友可以參考下2015-10-10
json如何解析混合數(shù)組對(duì)象到實(shí)體類的list集合里去
這篇文章主要介紹了json解析混合數(shù)組對(duì)象到實(shí)體類的list集合里去的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
java web實(shí)現(xiàn)自動(dòng)登錄功能
這篇文章主要為大家詳細(xì)介紹了java web實(shí)現(xiàn)自動(dòng)登錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10

