Java壓縮文件夾最實用簡單的方法
Java 有一個很好的類庫來處理 zip 文件。這些類在 java.util.zip 包中可用。以下 Java 示例程序展示了如何使用 java.util.zip 類創(chuàng)建整個文件夾的 zip。我們使用Files.walkFileTree遞歸地瀏覽目錄樹,然后將每個文件添加到新創(chuàng)建的 zip 文件中。請注意,此示例僅適用于 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
實例擴展
//方法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");//默認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)閉的時候會刷新
bos.close();
}
}
zis.close();
long endTime = System.currentTimeMillis();
System.out.println("解壓完成!所需時間為:"+(endTime-startTime)+"ms");
// System.out.println("校驗和:"+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壓縮文件夾最實用簡單的方法的文章就介紹到這了,更多相關(guān)Java壓縮文件夾的方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
struts2的國際化實現(xiàn)網(wǎng)站整體中英文切換實例代碼
本篇文章主要介紹了struts2的國際化實現(xiàn)網(wǎng)站整體中英文切換實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
spring boot整合Shiro實現(xiàn)單點登錄的示例代碼
本篇文章主要介紹了spring boot整合Shiro實現(xiàn)單點登錄的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
Springboot詳解RocketMQ實現(xiàn)廣播消息流程
RocketMQ作為一款純java、分布式、隊列模型的開源消息中間件,支持事務(wù)消息、順序消息、批量消息、定時消息、消息回溯等,本篇我們了解如何實現(xiàn)廣播消息2022-06-06
Mybatis動態(tài)Sql標(biāo)簽使用小結(jié)
本文主要介紹了Mybatis動態(tài)Sql標(biāo)簽使用,常用的動態(tài)sql標(biāo)簽包括?if、choose(when、otherwise)、trim(where、set)、foreach,下面就來介紹一下2024-04-04
將Springboot項目升級成Springcloud項目的圖文教程
本文主要介紹了將Springboot項目升級成Springcloud項目,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
解決出現(xiàn) java.lang.ExceptionInInitializerError錯誤問題
這篇文章主要介紹了解決出現(xiàn) java.lang.ExceptionInInitializerError錯誤問題的相關(guān)資料,需要的朋友可以參考下2017-01-01

