java壓縮文件與刪除文件的示例代碼
壓縮文件 :toZip(String srcDir, OutputStream out,boolean KeepDirStructure)
刪除文件:deleteFolder(File folder)
/** * 壓縮成ZIP 方法1 * * @param srcDir * 壓縮文件夾路徑 * @param out * 壓縮文件輸出流 * @param KeepDirStructure * 是否保留原來的目錄結(jié)構(gòu),true:保留目錄結(jié)構(gòu); * false:所有文件跑到壓縮包根目錄下(注意:不保留目錄結(jié)構(gòu)可能會出現(xiàn)同名文件,會壓縮失敗) * @throws RuntimeException * 壓縮失敗會拋出運行時異常 */ protected void toZip(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("壓縮完成,耗時:" + (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); * false:所有文件跑到壓縮包根目錄下(注意:不保留目錄結(jié)構(gòu)可能會出現(xiàn)同名文件,會壓縮失敗) * @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輸出流中添加一個zip實體,構(gòu)造器中name為zip實體的文件的名字 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)時,需要對空文件夾進行處理 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); } } } } } /** * 刪除文件夾 * * @param folder */ protected void deleteFolder(File folder) { // 待刪除文件路徑 String path = this.getClass().getResource("/").getPath().replace( "WEB-INF/classes/", "postFile/"); if (folder.isDirectory()) { File[] files = folder.listFiles(); for (int i = 0; i < files.length; i++) { deleteFolder(files[i]); } // 非當(dāng)前目錄,刪除 if (!folder.getAbsolutePath().equalsIgnoreCase(path)) { // // 只刪除在7天前創(chuàng)建的文件 // if (canDeleteFile(folder)) { // if (folder.delete()) { // System.out.println("文件夾" + folder.getName() + "刪除成功!"); // } else { // System.out.println("文件夾" + folder.getName() // + "刪除失敗!此文件夾內(nèi)的文件可能正在被使用"); // } // } //只要是空的文件夾就刪除不區(qū)分幾天前創(chuàng)建 if (folder.delete()) { System.out.println("文件夾" + folder.getName() + "刪除成功!"); } else { System.out.println("文件夾" + folder.getName() + "刪除失敗!此文件夾內(nèi)的文件可能正在被使用"); } } } else { deleteFile(folder); } } /** * 判斷文件是否能夠被刪除 */ protected boolean canDeleteFile(File file) { Date fileDate = getfileDate(file); Date date = new Date(); long time = (date.getTime() - fileDate.getTime()) / 1000 / 60 / 60 / 24;// 當(dāng)前時間與文件創(chuàng)建時間的間隔天數(shù) // 大于7天可刪除,小于10天不刪除 if (time > 10) { return true; } else { return false; } // return true; } /** * 獲取文件最后的修改時間 * * @param file * @return */ protected Date getfileDate(File file) { long modifiedTime = file.lastModified(); Date d = new Date(modifiedTime); return d; } /** * 刪除文件 * * @param file */ protected void deleteFile(File file) { try { if (file.isFile()) { // 刪除符合條件的文件 if (canDeleteFile(file)) { if (file.delete()) { System.out.println("文件" + file.getName() + "刪除成功!"); } else { System.out.println("文件" + file.getName() + "刪除失敗!此文件可能正在被使用"); } } else { } } else { System.out.println("沒有可以刪除的文件了"); } } catch (Exception e) { System.out.println("刪除文件失敗========"); e.printStackTrace(); } }
總結(jié)
到此這篇關(guān)于java壓縮文件與刪除文件的示例代碼的文章就介紹到這了,更多相關(guān)java壓縮文件與刪除文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Data?JPA框架快速入門之自定義Repository接口
Spring?Data?JPA是Spring基于JPA規(guī)范的基礎(chǔ)上封裝的?套?JPA?應(yīng)?框架,可使開發(fā)者?極簡的代碼即可實現(xiàn)對數(shù)據(jù)庫的訪問和操作,本篇我們來了解Spring?Data?JPA框架的自定義Repository接口2022-04-04Java查詢Elasticsearch數(shù)據(jù)根據(jù)指定id檢索(in查詢)、sql權(quán)限過濾、多字段匹配檢索及數(shù)據(jù)排序
在Java開發(fā)中Elasticsearch(簡稱ES)是一個非常流行的搜索引擎,它提供了強大的全文搜索和分析功能,這篇文章主要給大家介紹了關(guān)于Java查詢Elasticsearch數(shù)據(jù)根據(jù)指定id檢索(in查詢)、sql權(quán)限過濾、多字段匹配檢索及數(shù)據(jù)排序的相關(guān)資料,需要的朋友可以參考下2024-05-05MyBatis中的SQL映射文件如何配置參數(shù)映射和使用方法
MyBatis 是一種開源的 Java 持久化框架,它可以自動將數(shù)據(jù)庫中的數(shù)據(jù)映射到 Java 對象中,并且使得 Java 對象可以非常方便地存儲到數(shù)據(jù)庫中,本文將介紹 MyBatis 中 SQL 映射文件的參數(shù)映射配置和使用方法,需要的朋友可以參考下2023-07-07Spring Cloud Gateway 使用JWT工具類做用戶登錄校驗功能
這篇文章主要介紹了Spring Cloud Gateway 使用JWT工具類做用戶登錄校驗的示例代碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01Java數(shù)據(jù)結(jié)構(gòu)之線性表
線性表是其組成元素間具有線性關(guān)系的一種數(shù)據(jù)結(jié)構(gòu),對線性表的基本操作主要有,獲取元素,設(shè)置元素值,遍歷,插入,刪除,查找,替換,排序等。而線性表可以采用順序儲存結(jié)構(gòu)和鏈式儲存結(jié)構(gòu),本節(jié)主要講解順序表、單鏈表以及雙鏈表的各種基本操作。2017-03-03本地MinIO存儲服務(wù)Java遠程調(diào)用上傳文件的操作過程
MinIO是一款高性能、分布式的對象存儲系統(tǒng),它可以100%的運行在標準硬件上,即X86等低成本機器也能夠很好的運行MinIO,這篇文章主要介紹了本地MinIO存儲服務(wù)Java遠程調(diào)用上傳文件的操作過程,需要的朋友可以參考下2023-11-11