Java實(shí)現(xiàn)的zip工具類完整實(shí)例
本文實(shí)例講述了Java實(shí)現(xiàn)的zip工具類。分享給大家供大家參考,具體如下:
實(shí)現(xiàn)把zip解壓到指定路徑,把文件夾壓縮到zip,把文件列表壓縮為zip的三個(gè)方法
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; public class ZipUtils { /** * 解壓zip到指定路徑 * @param zipFile 待解壓文件 * @param descDir 指定解壓路徑 * @return fileNames 解壓的全部文件名 * @throws IOException */ public static List<String> unZipFiles(File zipFile, String descDir) throws IOException { List<String> fileNames = new ArrayList<String>(); ZipFile zip = new ZipFile(zipFile,Charset.forName("GBK"));//解決中文文件夾亂碼 String name = zip.getName().substring(zip.getName().lastIndexOf('\\')+1, zip.getName().lastIndexOf('.')); File pathFile = new File(descDir+name); if (!pathFile.exists()) { pathFile.mkdirs(); } String outPath = ""; for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) { ZipEntry entry = (ZipEntry) entries.nextElement(); String zipEntryName = entry.getName(); fileNames.add(zipEntryName); InputStream in = zip.getInputStream(entry); outPath = (descDir + name +"/"+ zipEntryName).replaceAll("\\*", "/"); // 判斷路徑是否存在,不存在則創(chuàng)建文件路徑 File file = new File(outPath.substring(0, outPath.lastIndexOf('/'))); if (!file.exists()) { file.mkdirs(); } // 判斷文件全路徑是否為文件夾,如果是上面已經(jīng)上傳,不需要解壓 if (new File(outPath).isDirectory()) { continue; } // 輸出文件路徑信息 FileOutputStream out = new FileOutputStream(outPath); byte[] buf1 = new byte[1024]; int len; while ((len = in.read(buf1)) > 0) { out.write(buf1, 0, len); } in.close(); out.close(); } pathFile.delete(); return fileNames; } /** * 壓縮文件夾成zip * @param srcDir 待打包的文件夾路徑 * @param out 打包文件名及存儲(chǔ)路徑 * @param KeepDirStructure 是否保留文件夾結(jié)構(gòu) 不保留則把文件夾下全部文件都打壓在一起 * @throws RuntimeException */ public static void docToZip(String srcDir, OutputStream out, boolean KeepDirStructure)throws RuntimeException { long start = System.currentTimeMillis(); ZipOutputStream zos = null ; try { zos = new ZipOutputStream(out); File sourceFile = new File(srcDir); compress(sourceFile,zos,sourceFile.getName(),KeepDirStructure); long end = System.currentTimeMillis(); System.out.println("壓縮完成,耗時(shí):" + (end - start) +" ms"); } catch (Exception e) { throw new RuntimeException("zip error from ZipUtils",e); }finally { if(zos != null) { try { zos.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 壓縮成ZIP 將多個(gè)文件大包 * @param srcFiles 需要壓縮的文件列表 * @param out 壓縮文件輸出流 * @throws RuntimeException 壓縮失敗會(huì)拋出運(yùn)行時(shí)異常 */ public static void filesToZip(List<File> srcFiles , OutputStream out)throws RuntimeException { long start = System.currentTimeMillis(); ZipOutputStream zos = null ; int BUFFER_SIZE = 2 * 1024; try { zos = new ZipOutputStream(out); for (File srcFile : srcFiles) { byte[] buf = new byte[BUFFER_SIZE]; zos.putNextEntry(new ZipEntry(srcFile.getName())); int len; FileInputStream in = new FileInputStream(srcFile); while ((len = in.read(buf)) != -1) { zos.write(buf, 0, len); } zos.closeEntry(); in.close(); } long end = System.currentTimeMillis(); System.out.println("壓縮完成,耗時(shí):" + (end - start) +" ms"); } catch (Exception e) { throw new RuntimeException("zip error from ZipUtils",e); }finally { if(zos != null) { try { zos.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 遞歸壓縮方法 * @param sourceFile 源文件 * @param zos zip輸出流 * @param name 壓縮后的名稱 * @param KeepDirStructure 是否保留原來(lái)的目錄結(jié)構(gòu),true:保留目錄結(jié)構(gòu); * false:所有文件跑到壓縮包根目錄下(注意:不保留目錄結(jié)構(gòu)可能會(huì)出現(xiàn)同名文件,會(huì)壓縮失敗) * @throws Exception */ private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure) throws Exception { int BUFFER_SIZE = 2 * 1024; byte[] buf = new byte[BUFFER_SIZE]; if(sourceFile.isFile()) { // 向zip輸出流中添加一個(gè)zip實(shí)體,構(gòu)造器中name為zip實(shí)體的文件的名字 zos.putNextEntry(new ZipEntry(name)); // copy文件到zip輸出流中 int len; FileInputStream in = new FileInputStream(sourceFile); while ((len = in.read(buf)) != -1) { zos.write(buf, 0, len); } // Complete the entry zos.closeEntry(); in.close(); } else { File[] listFiles = sourceFile.listFiles(); if(listFiles == null || listFiles.length == 0) { // 需要保留原來(lái)的文件結(jié)構(gòu)時(shí),需要對(duì)空文件夾進(jìn)行處理 if(KeepDirStructure) { // 空文件夾的處理 zos.putNextEntry(new ZipEntry(name + "/")); // 沒(méi)有文件,不需要文件的copy zos.closeEntry(); } }else { for (File file : listFiles) { // 判斷是否需要保留原來(lái)的文件結(jié)構(gòu) if (KeepDirStructure) { // 注意:file.getName()前面需要帶上父文件夾的名字加一斜杠, // 不然最后壓縮包中就不能保留原來(lái)的文件結(jié)構(gòu),即:所有文件都跑到壓縮包根目錄下了 compress(file, zos, name + "/" + file.getName(),KeepDirStructure); } else { compress(file, zos, file.getName(),KeepDirStructure); } } } } } }
更多關(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ì)有所幫助。
- java ant包中的org.apache.tools.zip實(shí)現(xiàn)壓縮和解壓縮實(shí)例詳解
- java中GZIP壓縮解壓類使用實(shí)例
- Java實(shí)現(xiàn)文件壓縮與解壓的示例[zip格式,gzip格式]
- java實(shí)現(xiàn)zip,gzip,7z,zlib格式的壓縮打包
- java 壓縮和解壓縮Zip、Jar、Gzip文件實(shí)例代碼
- java使用gzip實(shí)現(xiàn)文件解壓縮示例
- java實(shí)現(xiàn)批量下載 多文件打包成zip格式下載
- Java解壓和壓縮帶密碼的zip文件過(guò)程詳解
- tomcat啟動(dòng)報(bào)錯(cuò):java.util.zip.ZipException的解決方法
- Java GZIP壓縮與解壓縮代碼實(shí)例
相關(guān)文章
Java?Date(日期)對(duì)象進(jìn)行格式化的思路詳解
Date類是經(jīng)常會(huì)使用到的一個(gè)用來(lái)處理日期、時(shí)間的一個(gè)類。Date類是在java.util包下的Date類,這篇文章主要介紹了Java?Date(日期)對(duì)象如何進(jìn)行格式化呢,需要的朋友可以參考下2022-09-09Java實(shí)現(xiàn)的分頁(yè)工具類與用法示例
這篇文章主要介紹了Java實(shí)現(xiàn)的分頁(yè)工具類與用法,結(jié)合完整實(shí)例形式分析了java分頁(yè)工具類的定義、使用方法及相關(guān)操作技巧,需要的朋友可以參考下2019-10-10Java編程實(shí)現(xiàn)判斷網(wǎng)上鄰居文件是否存在的方法
這篇文章主要介紹了Java編程實(shí)現(xiàn)判斷網(wǎng)上鄰居文件是否存在的方法,涉及Java針對(duì)路徑轉(zhuǎn)換及字符串操作的相關(guān)技巧,需要的朋友可以參考下2015-10-10JavaWeb項(xiàng)目實(shí)戰(zhàn)之表白墻和在線相冊(cè)
這篇文章主要給大家介紹了關(guān)于JavaWeb項(xiàng)目實(shí)戰(zhàn)之表白墻和在線相冊(cè)的相關(guān)資料,JavaWeb表白墻是一款基于JavaWeb技術(shù)開(kāi)發(fā)的表白墻應(yīng)用,用戶可以在上面發(fā)布表白信息,也可以查看其他用戶的表白信息,需要的朋友可以參考下2023-03-03使用Java創(chuàng)建數(shù)據(jù)透視表并導(dǎo)出為PDF的方法
數(shù)據(jù)透視分析是一種強(qiáng)大的工具,可以幫助我們從大量數(shù)據(jù)中提取有用信息并進(jìn)行深入分析,本文將介紹如何使用Java來(lái)構(gòu)建PivotTable以及實(shí)現(xiàn)數(shù)據(jù)透視分析,并將其導(dǎo)出為PDF2023-10-10maven自動(dòng)將源碼打包并發(fā)布的實(shí)現(xiàn)步驟
maven-source-plugin 提供項(xiàng)目自動(dòng)將源碼打包并發(fā)布的功能,在需要發(fā)布源碼項(xiàng)目的 pom.xml 文件中添加即可,本文就來(lái)介紹一下如何設(shè)置,感興趣的可以了解一下2023-11-11多數(shù)據(jù)源@DS和@Transactional實(shí)戰(zhàn)
這篇文章主要介紹了多數(shù)據(jù)源@DS和@Transactional實(shí)戰(zhàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09