壓縮文件 :toZip(String srcDir, OutputStream out,boolean KeepDirStructure)
刪除文件:deleteFolder(File folder)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
/** * 壓縮成ZIP 方法1 * * @param srcDir * 壓縮文件夾路徑 * @param out * 壓縮文件輸出流 * @param KeepDirStructure * 是否保留原來的目錄結(jié)構(gòu),true:保留目錄結(jié)構(gòu); * false:所有文件跑到壓縮包根目錄下(注意:不保留目錄結(jié)構(gòu)可能會出現(xiàn)同名文件,會壓縮失敗) * @throws RuntimeException * 壓縮失敗會拋出運(yùn)行時異常 */ 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實(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)時,需要對空文件夾進(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)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/HXiao0805/article/details/108082757