Java實(shí)現(xiàn)文件壓縮為zip和解壓zip壓縮包
壓縮成.zip
代碼如下:
/** * 壓縮成ZIP * * @param srcDir 壓縮文件夾路徑 * @param out 壓縮文件輸出流 * @throws RuntimeException 壓縮失敗會(huì)拋出運(yùn)行時(shí)異常 */ public static void toZip(String srcDir, OutputStream out) throws RuntimeException { long start = System.currentTimeMillis(); ZipOutputStream zos = null; try { zos = new ZipOutputStream(out); File sourceFile = new File(srcDir); compress(sourceFile, zos, sourceFile.getName(), false); 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 是否保留原來的目錄結(jié)構(gòu),true:保留目錄結(jié)構(gòu); * <p> * false:所有文件跑到壓縮包根目錄下(注意:不保留目錄結(jié)構(gòu)可能會(huì)出現(xiàn)同名文件,會(huì)壓縮失敗) * @throws Exception */ private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure) throws Exception { 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) { // 需要保留原來的文件結(jié)構(gòu)時(shí),需要對空文件夾進(jìn)行處理 if (KeepDirStructure) { // 空文件夾的處理 zos.putNextEntry(new ZipEntry(name + "/")); // 沒有文件,不需要文件的copy zos.closeEntry(); } } else { for (File file : listFiles) { // 判斷是否需要保留原來的文件結(jié)構(gòu) if (KeepDirStructure) { // 注意:file.getName()前面需要帶上父文件夾的名字加一斜杠, // 不然最后壓縮包中就不能保留原來的文件結(jié)構(gòu),即:所有文件都跑到壓縮包根目錄下了 compress(file, zos, name + "/" + file.getName(), KeepDirStructure); } else { compress(file, zos, file.getName(), KeepDirStructure); } } } } }
測試驗(yàn)證代碼:
/** * 測試打包本地的Navicat,輸出為zip文件 * @throws Exception */ @Test public void test() throws Exception { //Navicat路徑 String inDir = "E:\\developer\\Navicat"; //打包后輸出路徑 String outDir = "E:\\developer\\NavicatZip\\Navicat.zip"; OutputStream fileOutputStream = new FileOutputStream(new File(outDir)); ZipUtils.toZip(inDir, fileOutputStream); }
打包前后的文件如下:
解壓.zip
代碼如下:
/** * 解壓zip文件到指定目錄 * @param fileZip * @param path_to_dest * @throws IOException */ public static void readZip(String fileZip,String path_to_dest) throws IOException { try (FileInputStream fis = new FileInputStream(fileZip); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) { ZipEntry entry; // 從ZipInputStream讀取每個(gè)條目,直到?jīng)]有 // 發(fā)現(xiàn)更多條目,返回值為空 // getNextEntry()方法。 while ((entry = zis.getNextEntry()) != null) { System.out.println("Unzipping: " + entry.getName()); int size; byte[] buffer = new byte[2048]; File fileOut = new File(path_to_dest+"\\"+entry.getName()); try (FileOutputStream fos = new FileOutputStream(fileOut); BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length)) { while ((size = zis.read(buffer, 0, buffer.length)) != -1) { bos.write(buffer, 0, size); } bos.flush(); } } } catch (IOException e) { e.printStackTrace(); } }
測試驗(yàn)證代碼:
/** * 測試解壓本地zip文件 * @throws Exception */ @Test public void readZip() throws Exception { //解壓后路徑 String path_to_dest = "E:\\developer\\NavicatUnzip"; //zip文件路徑 String fileZip = "E:\\developer\\NavicatZip\\Navicat.zip"; ZipUtils.readZip(fileZip, path_to_dest); }
解壓前后的文件如下:
以上就是Java實(shí)現(xiàn)文件壓縮為zip和解壓zip壓縮包的詳細(xì)內(nèi)容,更多關(guān)于Java文件壓縮為zip的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring AOP與AspectJ的對比及應(yīng)用詳解
這篇文章主要為大家介紹了Spring AOP與AspectJ的對比及應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Springboot實(shí)現(xiàn)前后端分離excel下載
這篇文章主要介紹了Springboot實(shí)現(xiàn)前后端分離excel下載,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Java Cmd運(yùn)行Jar出現(xiàn)亂碼的解決方案
這篇文章主要介紹了Java Cmd運(yùn)行Jar出現(xiàn)亂碼的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09java利用數(shù)組隨機(jī)抽取幸運(yùn)觀眾
這篇文章主要為大家詳細(xì)介紹了java利用數(shù)組隨機(jī)抽取幸運(yùn)觀眾,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05如何使用CountDownLatch同步j(luò)ava多線程
這篇文章主要介紹了如何使用CountDownLatch同步j(luò)ava多線程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08