您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Java如何實(shí)現(xiàn)文件壓縮為zip和解壓zip壓縮包”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
代碼如下:
/** * 壓縮成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í),需要對(duì)空文件夾進(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); } } } } }
測(cè)試驗(yàn)證代碼:
/** * 測(cè)試打包本地的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文件到指定目錄 * @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(); } }
測(cè)試驗(yàn)證代碼:
/** * 測(cè)試解壓本地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壓縮包”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。