因为项目要用到压缩、解压缩zip格式压缩包,只好自己封装一个,对于网上流行的中文乱码的问题,本文的解决方法是用apache的包代替jdk里的。基本上还是比较好用的。
废话少说,直接上代码。
1packagecom.resoft.util;
2
3importjava.io.BufferedOutputStream;
4importjava.io.File;
5importjava.io.FileInputStream;
6importjava.io.FileNotFoundException;
7importjava.io.FileOutputStream;
8importjava.io.IOException;
9importjava.io.InputStream;
10importjava.io.OutputStream;
11importjava.util.Enumeration;
12
13importorg.apache.tools.zip.ZipEntry;
14importorg.apache.tools.zip.ZipFile;
15importorg.apache.tools.zip.ZipOutputStream;
16
17/**
18*压缩/解压缩zip包处理类
19*
20*@authoryayagepei
21*@date-8-25
22*/
23publicclassZipUtil{
24
25/**
26*压缩
27*
28*@paramzipFileName
29*压缩产生的zip包文件名--带路径,如果为null或空则默认按文件名生产压缩文件名
30*@paramrelativePath
31*相对路径,默认为空
32*@paramdirectory
33*文件或目录的绝对路径
34*@throwsFileNotFoundException
35*@throwsIOException
36*@authoryayagepei
37*@date-8-26
38*/
39publicstaticvoidzip(StringzipFileName,StringrelativePath,
40Stringdirectory)throwsFileNotFoundException,IOException{
41StringfileName=zipFileName;
42if(fileName==null||fileName.trim().equals("")){
43Filetemp=newFile(directory);
44if(temp.isDirectory()){
45fileName=directory+".zip";
46}else{
47if(directory.indexOf(".")>0){
48fileName=directory.substring(0,directory
49.lastIndexOf("."))
50+"zip";
51}else{
52fileName=directory+".zip";
53}
54}
55}
56ZipOutputStreamzos=newZipOutputStream(
57newFileOutputStream(fileName));
58try{
59zip(zos,relativePath,directory);
60}catch(IOExceptionex){
61throwex;
62}finally{
63if(null!=zos){
64zos.close();
65}
66}
67}
68
69/**
70*压缩
71*
72*@paramzos
73*压缩输出流
74*@paramrelativePath
75*相对路径
76*@paramabsolutPath
77*文件或文件夹绝对路径
78*@throwsIOException
79*@authoryayagepei
80*@date-8-26
81*/
82privatestaticvoidzip(ZipOutputStreamzos,StringrelativePath,
83StringabsolutPath)throwsIOException{
84Filefile=newFile(absolutPath);
85if(file.isDirectory()){
86File[]files=file.listFiles();
87for(inti=0;i<files.length;i++){
88FiletempFile=files[i];
89if(tempFile.isDirectory()){
90StringnewRelativePath=relativePath+tempFile.getName()
91+File.separator;
92createZipNode(zos,newRelativePath);
93zip(zos,newRelativePath,tempFile.getPath());
94}else{
95zipFile(zos,tempFile,relativePath);
96}
97}
98}else{
99zipFile(zos,file,relativePath);
100}
101}
102
103/**
104*压缩文件
105*
106*@paramzos
107*压缩输出流
108*@paramfile
109*文件对象
110*@paramrelativePath
111*相对路径
112*@throwsIOException
113*@authoryayagepei
114*@date-8-26
115*/
116privatestaticvoidzipFile(ZipOutputStreamzos,Filefile,
117StringrelativePath)throwsIOException{
118ZipEntryentry=newZipEntry(relativePath+file.getName());
119zos.putNextEntry(entry);
120InputStreamis=null;
121try{
122is=newFileInputStream(file);
123intBUFFERSIZE=2<<10;
124intlength=0;
125byte[]buffer=newbyte[BUFFERSIZE];
126while((length=is.read(buffer,0,BUFFERSIZE))>=0){
127zos.write(buffer,0,length);
128}
129zos.flush();
130zos.closeEntry();
131}catch(IOExceptionex){
132throwex;
133}finally{
134if(null!=is){
135is.close();
136}
137}
138}
139
140/**
141*创建目录
142*
143*@paramzos
144*zip输出流
145*@paramrelativePath
146*相对路径
147*@throwsIOException
148*@authoryayagepei
149*@date-8-26
150*/
151privatestaticvoidcreateZipNode(ZipOutputStreamzos,StringrelativePath)
152throwsIOException{
153ZipEntryzipEntry=newZipEntry(relativePath);
154zos.putNextEntry(zipEntry);
155zos.closeEntry();
156}
157
158/**
159*解压缩zip包
160*
161*@paramzipFilePath
162*zip文件路径
163*@paramtargetPath
164*解压缩到的位置,如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下
165*@throwsIOException
166*@authoryayagepei
167*@date-9-28
168*/
169publicstaticvoidunzip(StringzipFilePath,StringtargetPath)
170throwsIOException{
171OutputStreamos=null;
172InputStreamis=null;
173ZipFilezipFile=null;
174try{
175zipFile=newZipFile(zipFilePath);
176StringdirectoryPath="";
177if(null==targetPath||"".equals(targetPath)){
178directoryPath=zipFilePath.substring(0,zipFilePath
179.lastIndexOf("."));
180}else{
181directoryPath=targetPath;
182}
183EnumerationentryEnum=zipFile.getEntries();
184if(null!=entryEnum){
185ZipEntryzipEntry=null;
186while(entryEnum.hasMoreElements()){
187zipEntry=(ZipEntry)entryEnum.nextElement();
188if(zipEntry.isDirectory()){
189directoryPath=directoryPath+File.separator
190+zipEntry.getName();
191System.out.println(directoryPath);
192continue;
193}
194if(zipEntry.getSize()>0){
195//文件
196FiletargetFile=FileUtil.buildFile(directoryPath
197+File.separator+zipEntry.getName(),false);
198os=newBufferedOutputStream(newFileOutputStream(
199targetFile));
200is=zipFile.getInputStream(zipEntry);
201byte[]buffer=newbyte[4096];
202intreadLen=0;
203while((readLen=is.read(buffer,0,4096))>=0){
204os.write(buffer,0,readLen);
205}
206
207os.flush();
208os.close();
209}else{
210//空目录
211FileUtil.buildFile(directoryPath+File.separator
212+zipEntry.getName(),true);
213}
214}
215}
216}catch(IOExceptionex){
217throwex;
218}finally{
219if(null!=zipFile){
220zipFile=null;
221}
222if(null!=is){
223is.close();
224}
225if(null!=os){
226os.close();
227}
228}
229}
230}
231
补充一下里面用到的一个自己写的FileUtil的一个方法
/**
* 生产文件 如果文件所在路径不存在则生成路径
*
* @param fileName
* 文件名 带路径
* @param isDirectory 是否为路径
* @return
* @author yayagepei
* @date -8-27
*/
public static File buildFile(String fileName, boolean isDirectory) {
File target = new File(fileName);
if (isDirectory) {
target.mkdirs();
} else {
if (!target.getParentFile().exists()) {
target.getParentFile().mkdirs();
target = new File(target.getAbsolutePath());
}
}
return target;
}