提交 | 用户 | 时间
|
58d006
|
1 |
package com.mandi.servlet.file.impl; |
A |
2 |
|
|
3 |
import java.io.BufferedInputStream; |
|
4 |
import java.io.File; |
|
5 |
import java.io.FileInputStream; |
|
6 |
import java.io.FileNotFoundException; |
|
7 |
import java.io.FileOutputStream; |
|
8 |
import java.io.IOException; |
|
9 |
import java.nio.charset.Charset; |
|
10 |
import java.nio.file.Files; |
|
11 |
import java.nio.file.Paths; |
|
12 |
import java.util.Enumeration; |
|
13 |
import java.util.zip.CRC32; |
|
14 |
import java.util.zip.CheckedOutputStream; |
|
15 |
import java.util.zip.ZipEntry; |
|
16 |
import java.util.zip.ZipFile; |
|
17 |
import java.util.zip.ZipOutputStream; |
|
18 |
|
|
19 |
|
|
20 |
import com.mandi.servlet.file.Zipfile; |
|
21 |
|
|
22 |
/** |
|
23 |
* @author mengly |
|
24 |
* @version 创建时间:2015年11月6日 下午7:00:20 |
|
25 |
* 类说明 |
|
26 |
*/ |
|
27 |
|
|
28 |
public class Zipfileimpl implements Zipfile { |
|
29 |
private int buffersize=1024; |
|
30 |
@Override |
|
31 |
public File zip(String path,String dest) { |
|
32 |
File sf=new File(path); |
|
33 |
if(!sf.exists()) |
|
34 |
return null; |
|
35 |
this.zip(sf, dest); |
|
36 |
return null; |
|
37 |
} |
|
38 |
private void compress(File f,ZipOutputStream zos,String pathbase) |
|
39 |
{ |
|
40 |
if(f.isDirectory()) |
|
41 |
{ |
|
42 |
File[] files = f.listFiles(); |
|
43 |
if(files.length<1) |
|
44 |
{ |
|
45 |
ZipEntry ze=new ZipEntry(pathbase + f.getName()+ File.separator); |
|
46 |
try { |
|
47 |
zos.putNextEntry(ze); |
|
48 |
zos.closeEntry(); |
|
49 |
} catch (IOException e) { |
|
50 |
e.printStackTrace(); |
|
51 |
} |
|
52 |
}else{ |
|
53 |
for (File file : files) { |
|
54 |
compress(file, zos,f.getName()+ File.separator); |
|
55 |
} |
|
56 |
} |
|
57 |
}else if(f.isFile()){ |
|
58 |
ZipEntry ze=new ZipEntry(pathbase + f.getName()); |
|
59 |
BufferedInputStream bis=null; |
|
60 |
try { |
|
61 |
zos.putNextEntry(ze); |
|
62 |
int count; |
|
63 |
bis=new BufferedInputStream(new FileInputStream(f)); |
|
64 |
byte data[]=new byte[buffersize]; |
|
65 |
while((count=bis.read(data, 0, buffersize))!=-1) |
|
66 |
{ |
|
67 |
zos.write(data,0,count); |
|
68 |
} |
|
69 |
} catch (IOException e) { |
|
70 |
e.printStackTrace(); |
|
71 |
}finally{ |
|
72 |
try { |
|
73 |
bis.close(); |
|
74 |
zos.closeEntry(); |
|
75 |
} catch (IOException e) { |
|
76 |
e.printStackTrace(); |
|
77 |
} |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 |
} |
|
82 |
|
|
83 |
@Override |
|
84 |
public File zip(File file,String dest) { |
|
85 |
if(!file.exists()) |
|
86 |
return null; |
|
87 |
ZipOutputStream zos=null; |
|
88 |
try { |
|
89 |
File df=new File(dest); |
|
90 |
CheckedOutputStream cos=new CheckedOutputStream(new FileOutputStream(df), new CRC32()); |
|
91 |
zos=new ZipOutputStream(cos); |
|
92 |
if(file.isFile()) |
|
93 |
compress(file,zos,""); |
|
94 |
else{ |
|
95 |
File[] fs=file.listFiles(); |
|
96 |
for (File file2 : fs) { |
|
97 |
compress(file2,zos,""); |
|
98 |
} |
|
99 |
} |
|
100 |
return df; |
|
101 |
} catch (FileNotFoundException e) { |
|
102 |
e.printStackTrace(); |
|
103 |
}finally{ |
|
104 |
try { |
|
105 |
zos.close(); |
|
106 |
} catch (IOException e) { |
|
107 |
e.printStackTrace(); |
|
108 |
} |
|
109 |
} |
|
110 |
return null; |
|
111 |
} |
|
112 |
|
|
113 |
@Override |
|
114 |
public File unzip(String path,String dest) { |
|
115 |
File f=new File(path); |
|
116 |
if(!f.exists()) |
|
117 |
return null; |
|
118 |
this.unzip(f, dest); |
|
119 |
return null; |
|
120 |
} |
|
121 |
|
|
122 |
@Override |
|
123 |
public File unzip(File file,String dest) { |
|
124 |
File destfile=new File(dest); |
|
125 |
if(!destfile.exists()||!destfile.isDirectory()) |
|
126 |
destfile.mkdirs(); |
|
127 |
if(!dest.endsWith(File.separator)) |
|
128 |
{ |
|
129 |
dest=dest+File.separator; |
|
130 |
} |
|
131 |
ZipFile zf=null; |
|
132 |
try{ |
|
133 |
zf=new ZipFile(file,Charset.forName(System.getProperty("sun.jnu.encoding"))); |
|
134 |
Enumeration<ZipEntry> enums=(Enumeration<ZipEntry>) zf.entries(); |
|
135 |
while(enums.hasMoreElements()) |
|
136 |
{ |
|
137 |
ZipEntry zee=enums.nextElement(); |
|
138 |
if(zee.isDirectory()) |
|
139 |
{ |
|
140 |
String entryname=zee.getName(); |
|
141 |
String tempname=dest+entryname.substring(0,entryname.length()-1); |
|
142 |
File f = new File(tempname); |
|
143 |
if (!f.exists()||!f.isDirectory()) |
|
144 |
f.mkdirs(); |
|
145 |
}else{ |
|
146 |
Files.copy(zf.getInputStream(zee), Paths.get(dest+zee.getName())); |
|
147 |
} |
|
148 |
} |
|
149 |
|
|
150 |
|
|
151 |
}catch(Exception e){ |
|
152 |
e.printStackTrace(); |
|
153 |
}finally{ |
|
154 |
try { |
|
155 |
zf.close(); |
|
156 |
} catch (IOException e) { |
|
157 |
e.printStackTrace(); |
|
158 |
} |
|
159 |
} |
|
160 |
|
|
161 |
return null; |
|
162 |
} |
|
163 |
|
|
164 |
} |