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)時,需要對空文件夾進(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);
}
}
}
}
}
/**
* 刪除文件夾
*
* @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)文章
SSH框架網(wǎng)上商城項目第23戰(zhàn)之在線支付功能實現(xiàn)
這篇文章主要為大家詳細(xì)介紹了SSH框架網(wǎng)上商城項目第23戰(zhàn)之在線支付功能實現(xiàn),感興趣的小伙伴們可以參考一下2016-06-06
Java調(diào)用高德地圖API根據(jù)詳細(xì)地址獲取經(jīng)緯度詳細(xì)教程
寫了一個經(jīng)緯度相關(guān)的工具,分享給有需求的小伙伴們,下面這篇文章主要給大家介紹了關(guān)于Java調(diào)用高德地圖API根據(jù)詳細(xì)地址獲取經(jīng)緯度,文中通過圖文以及代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04
mybatis+lombok出現(xiàn)java.lang.IndexOutOfBoundsException錯誤及解決
在使用MyBatis和Lombok時,如果遇到j(luò)ava.lang.IndexOutOfBoundsException問題,是因為MyBatis在嘗試將查詢結(jié)果封裝成Java對象時,找不到構(gòu)造函數(shù)中對應(yīng)的字段,這通常是由于Lombok的@Builder注解生成了全參構(gòu)造函數(shù)2025-02-02

