java解壓縮zip - 多個文件(包括文件夾),對多個文件和文件夾進行壓縮,對復雜的文件目錄進行解壓。壓縮方法使用的是可變參數,可以壓縮1到多個文件..可以寫數組的方式或者一個個寫到參數列表里面...
1
|
zipfiles(zip, "abc" , new file( "d:/english" ), new file( "d:/發放數據.xls" )); |
測試文件目錄結構:
測試的壓縮內容:english文件夾和同級的兩個excel文件
1
|
file[] files = new file[]{ new file( "d:/english" ), new file( "d:/發放數據.xls" ), new file( "d:/中文名稱.xls" )}; |
下面是壓縮的代碼:
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
|
/** * 壓縮文件-由于out要在遞歸調用外,所以封裝一個方法用來 * 調用zipfiles(zipoutputstream out,string path,file... srcfiles) * @param zip * @param path * @param srcfiles * @throws ioexception * */ public static void zipfiles(file zip,string path,file... srcfiles) throws ioexception{ zipoutputstream out = new zipoutputstream( new fileoutputstream(zip)); ziptest.zipfiles(out,path,srcfiles); out.close(); system.out.println( "*****************壓縮完畢*******************" ); } /** * 壓縮文件-file * @param zipfile zip文件 * @param srcfiles 被壓縮源文件 * */ public static void zipfiles(zipoutputstream out,string path,file... srcfiles){ path = path.replaceall( "\\*" , "/" ); if (!path.endswith( "/" )){ path+= "/" ; } byte [] buf = new byte [ 1024 ]; try { for ( int i= 0 ;i<srcfiles.length;i++){ if (srcfiles[i].isdirectory()){ file[] files = srcfiles[i].listfiles(); string srcpath = srcfiles[i].getname(); srcpath = srcpath.replaceall( "\\*" , "/" ); if (!srcpath.endswith( "/" )){ srcpath+= "/" ; } out.putnextentry( new zipentry(path+srcpath)); zipfiles(out,path+srcpath,files); } else { fileinputstream in = new fileinputstream(srcfiles[i]); system.out.println(path + srcfiles[i].getname()); out.putnextentry( new zipentry(path + srcfiles[i].getname())); int len; while ((len=in.read(buf))> 0 ){ out.write(buf, 0 ,len); } out.closeentry(); in.close(); } } } catch (exception e) { e.printstacktrace(); } } |
在壓縮的時候,針對文件夾進行判斷,然后遞歸壓縮文件。
然后是解壓:
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
|
/** * 解壓到指定目錄 * @param zippath * @param descdir * */ public static void unzipfiles(string zippath,string descdir) throws ioexception{ unzipfiles( new file(zippath), descdir); } /** * 解壓文件到指定目錄 * @param zipfile * @param descdir * */ @suppresswarnings ( "rawtypes" ) public static void unzipfiles(file zipfile,string descdir) throws ioexception{ file pathfile = new file(descdir); if (!pathfile.exists()){ pathfile.mkdirs(); } zipfile zip = new zipfile(zipfile); for (enumeration entries = zip.getentries();entries.hasmoreelements();){ zipentry entry = (zipentry)entries.nextelement(); string zipentryname = entry.getname(); inputstream in = zip.getinputstream(entry); string outpath = (descdir+zipentryname).replaceall( "\\*" , "/" );; //判斷路徑是否存在,不存在則創建文件路徑 file file = new file(outpath.substring( 0 , outpath.lastindexof( '/' ))); if (!file.exists()){ file.mkdirs(); } //判斷文件全路徑是否為文件夾,如果是上面已經上傳,不需要解壓 if ( new file(outpath).isdirectory()){ continue ; } //輸出文件路徑信息 system.out.println(outpath); outputstream out = new fileoutputstream(outpath); byte [] buf1 = new byte [ 1024 ]; int len; while ((len=in.read(buf1))> 0 ){ out.write(buf1, 0 ,len); } in.close(); out.close(); } system.out.println( "******************解壓完畢********************" ); } |
解壓的時候,針對文件夾判斷創建不存在的文件夾,對文件夾只創建,不進行解壓..因為解壓是針對文件的,不是文件夾,文件夾需要自己創建。
測試方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public static void main(string[] args) throws ioexception { /** * 壓縮文件 */ file[] files = new file[]{ new file( "d:/english" ), new file( "d:/發放數據.xls" ), new file( "d:/中文名稱.xls" )}; file zip = new file( "d:/壓縮.zip" ); zipfiles(zip, "abc" ,files); /** * 解壓文件 */ file zipfile = new file( "d:/壓縮.zip" ); string path = "d:/zipfile/" ; unzipfiles(zipfile, path); } |
測試方法并沒有對異常做任何處理,這是不對的,請不要模仿。
輸出結果:
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
|
abc/english/templete.xls abc/english/中文/bjpowernode/isea/ 533 /abc/templete.xls abc/english/中文/bjpowernode/isea/ 533 /abc/zipfile2/templete.xls abc/english/中文/bjpowernode/isea/ 533 /abc/zipfile2/zipfile/abc/templete.xls abc/english/中文/bjpowernode/isea/ 533 /abc/zipfile2/zipfile/abc/zipfile2/templete.xls abc/english/中文/bjpowernode/isea/ 533 /abc/zipfile2/zipfile/abc/zipfile2/領卡清單.xls abc/english/中文/bjpowernode/isea/ 533 /abc/zipfile2/領卡清單.xls abc/english/中文/bjpowernode/isea/templete.xls abc/english/中文/bjpowernode/isea/領卡清單.xls abc/english/中文/bjpowernode/templete.xls abc/english/領卡清單.xls abc/發放數據.xls abc/中文名稱.xls *****************壓縮完畢******************* d:/zipfile/abc/中文名稱.xls d:/zipfile/abc/發放數據.xls d:/zipfile/abc/english/領卡清單.xls d:/zipfile/abc/english/中文/bjpowernode/templete.xls d:/zipfile/abc/english/中文/bjpowernode/isea/領卡清單.xls d:/zipfile/abc/english/中文/bjpowernode/isea/templete.xls d:/zipfile/abc/english/中文/bjpowernode/isea/ 533 /abc/templete.xls d:/zipfile/abc/english/templete.xls d:/zipfile/abc/english/中文/bjpowernode/isea/ 533 /abc/zipfile2/templete.xls d:/zipfile/abc/english/中文/bjpowernode/isea/ 533 /abc/zipfile2/zipfile/abc/templete.xls d:/zipfile/abc/english/中文/bjpowernode/isea/ 533 /abc/zipfile2/zipfile/abc/zipfile2/templete.xls d:/zipfile/abc/english/中文/bjpowernode/isea/ 533 /abc/zipfile2/zipfile/abc/zipfile2/領卡清單.xls d:/zipfile/abc/english/中文/bjpowernode/isea/ 533 /abc/zipfile2/領卡清單.xls ******************解壓完畢******************** |
以上所述是小編給大家介紹的java壓縮解壓zip技術,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!