java压缩/解压缩zip格式文件
生活随笔
收集整理的這篇文章主要介紹了
java压缩/解压缩zip格式文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
因為項目要用到壓縮、解壓縮zip格式壓縮包,只好自己封裝一個,對于網上流行的中文亂碼的問題,本文的解決方法是用apache的包代替jdk里的。基本上還是比較好用的。
??2
??3 import ?java.io.BufferedOutputStream;
??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.io.InputStream;
?10 import ?java.io.OutputStream;
?11 import ?java.util.Enumeration;
?12
?13 import ?org.apache.tools.zip.ZipEntry;
?14 import ?org.apache.tools.zip.ZipFile;
?15 import ?org.apache.tools.zip.ZipOutputStream;
?16
?17 /**
?18?*?壓縮/解壓縮zip包處理類
?19?*?
?20?*?@author?yayagepei
?21?*?@date?2008-8-25
?22?*/
?23 public ? class ?ZipUtil? {
?24
?25????/**
?26?????*?壓縮
?27?????*?
?28?????*?@param?zipFileName
?29?????*????????????壓縮產生的zip包文件名--帶路徑,如果為null或空則默認按文件名生產壓縮文件名
?30?????*?@param?relativePath
?31?????*????????????相對路徑,默認為空
?32?????*?@param?directory
?33?????*????????????文件或目錄的絕對路徑
?34?????*?@throws?FileNotFoundException
?35?????*?@throws?IOException
?36?????*?@author?yayagepei
?37?????*?@date?2008-8-26
?38?????*/
?39????public?static?void?zip(String?zipFileName,?String?relativePath,
?40????????????String?directory)?throws?FileNotFoundException,?IOException?{
?41????????String?fileName?=?zipFileName;
?42????????if?(fileName?==?null?||?fileName.trim().equals(""))?{
?43????????????File?temp?=?new?File(directory);
?44????????????if?(temp.isDirectory())?{
?45????????????????fileName?=?directory?+?".zip";
?46????????????}?else?{
?47????????????????if?(directory.indexOf(".")?>?0)?{
?48????????????????????fileName?=?directory.substring(0,?directory
?49????????????????????????????.lastIndexOf("."))
?50????????????????????????????+?"zip";
?51????????????????}?else?{
?52????????????????????fileName?=?directory?+?".zip";
?53????????????????}
?54????????????}
?55????????}
?56????????ZipOutputStream?zos?=?new?ZipOutputStream(
?57????????????????new?FileOutputStream(fileName));
?58????????try?{
?59????????????zip(zos,?relativePath,?directory);
?60????????}?catch?(IOException?ex)?{
?61????????????throw?ex;
?62????????}?finally?{
?63????????????if?(null?!=?zos)?{
?64????????????????zos.close();
?65????????????}
?66????????}
?67????}
?68
?69????/**
?70?????*?壓縮
?71?????*?
?72?????*?@param?zos
?73?????*????????????壓縮輸出流
?74?????*?@param?relativePath
?75?????*????????????相對路徑
?76?????*?@param?absolutPath
?77?????*????????????文件或文件夾絕對路徑
?78?????*?@throws?IOException
?79?????*?@author?yayagepei
?80?????*?@date?2008-8-26
?81?????*/
?82????private?static?void?zip(ZipOutputStream?zos,?String?relativePath,
?83????????????String?absolutPath)?throws?IOException?{
?84????????File?file?=?new?File(absolutPath);
?85????????if?(file.isDirectory())?{
?86????????????File[]?files?=?file.listFiles();
?87????????????for?(int?i?=?0;?i?<?files.length;?i++)?{
?88????????????????File?tempFile?=?files[i];
?89????????????????if?(tempFile.isDirectory())?{
?90????????????????????String?newRelativePath?=?relativePath?+?tempFile.getName()
?91????????????????????????????+?File.separator;
?92????????????????????createZipNode(zos,?newRelativePath);
?93????????????????????zip(zos,?newRelativePath,?tempFile.getPath());
?94????????????????}?else?{
?95????????????????????zipFile(zos,?tempFile,?relativePath);
?96????????????????}
?97????????????}
?98????????}?else?{
?99????????????zipFile(zos,?file,?relativePath);
100????????}
101????}
102
103????/**
104?????*?壓縮文件
105?????*?
106?????*?@param?zos
107?????*????????????壓縮輸出流
108?????*?@param?file
109?????*????????????文件對象
110?????*?@param?relativePath
111?????*????????????相對路徑
112?????*?@throws?IOException
113?????*?@author?yayagepei
114?????*?@date?2008-8-26
115?????*/
116????private?static?void?zipFile(ZipOutputStream?zos,?File?file,
117????????????String?relativePath)?throws?IOException?{
118????????ZipEntry?entry?=?new?ZipEntry(relativePath?+?file.getName());
119????????zos.putNextEntry(entry);
120????????InputStream?is?=?null;
121????????try?{
122????????????is?=?new?FileInputStream(file);
123????????????int?BUFFERSIZE?=?2?<<?10;
124????????????int?length?=?0;
125????????????byte[]?buffer?=?new?byte[BUFFERSIZE];
126????????????while?((length?=?is.read(buffer,?0,?BUFFERSIZE))?>=?0)?{
127????????????????zos.write(buffer,?0,?length);
128????????????}
129????????????zos.flush();
130????????????zos.closeEntry();
131????????}?catch?(IOException?ex)?{
132????????????throw?ex;
133????????}?finally?{
134????????????if?(null?!=?is)?{
135????????????????is.close();
136????????????}
137????????}
138????}
139
140????/**
141?????*?創建目錄
142?????*?
143?????*?@param?zos
144?????*????????????zip輸出流
145?????*?@param?relativePath
146?????*????????????相對路徑
147?????*?@throws?IOException
148?????*?@author?yayagepei
149?????*?@date?2008-8-26
150?????*/
151????private?static?void?createZipNode(ZipOutputStream?zos,?String?relativePath)
152????????????throws?IOException?{
153????????ZipEntry?zipEntry?=?new?ZipEntry(relativePath);
154????????zos.putNextEntry(zipEntry);
155????????zos.closeEntry();
156????}
157
158????/**
159?????*?解壓縮zip包
160?????*?
161?????*?@param?zipFilePath
162?????*????????????zip文件路徑
163?????*?@param?targetPath
164?????*????????????解壓縮到的位置,如果為null或空字符串則默認解壓縮到跟zip包同目錄跟zip包同名的文件夾下
165?????*?@throws?IOException
166?????*?@author?yayagepei
167?????*?@date?2008-9-28
168?????*/
169????public?static?void?unzip(String?zipFilePath,?String?targetPath)
170????????????throws?IOException?{
171????????OutputStream?os?=?null;
172????????InputStream?is?=?null;
173????????ZipFile?zipFile?=?null;
174????????try?{
175????????????zipFile?=?new?ZipFile(zipFilePath);
176????????????String?directoryPath?=?"";
177????????????if?(null?==?targetPath?||?"".equals(targetPath))?{
178????????????????directoryPath?=?zipFilePath.substring(0,?zipFilePath
179????????????????????????.lastIndexOf("."));
180????????????}?else?{
181????????????????directoryPath?=?targetPath;
182????????????}
183????????????Enumeration?entryEnum?=?zipFile.getEntries();
184????????????if?(null?!=?entryEnum)?{
185????????????????ZipEntry?zipEntry?=?null;
186????????????????while?(entryEnum.hasMoreElements())?{
187????????????????????zipEntry?=?(ZipEntry)?entryEnum.nextElement();
188????????????????????if?(zipEntry.isDirectory())?{
189????????????????????????directoryPath?=?directoryPath?+?File.separator
190????????????????????????????????+?zipEntry.getName();
191????????????????????????System.out.println(directoryPath);
192????????????????????????continue;
193????????????????????}
194????????????????????if?(zipEntry.getSize()?>?0)?{
195????????????????????????//?文件
196????????????????????????File?targetFile?=?FileUtil.buildFile(directoryPath
197????????????????????????????????+?File.separator?+?zipEntry.getName(),?false);
198????????????????????????os?=?new?BufferedOutputStream(new?FileOutputStream(
199????????????????????????????????targetFile));
200????????????????????????is?=?zipFile.getInputStream(zipEntry);
201????????????????????????byte[]?buffer?=?new?byte[4096];
202????????????????????????int?readLen?=?0;
203????????????????????????while?((readLen?=?is.read(buffer,?0,?4096))?>=?0)?{
204????????????????????????????os.write(buffer,?0,?readLen);
205????????????????????????}
206
207????????????????????????os.flush();
208????????????????????????os.close();
209????????????????????}?else?{
210????????????????????????//?空目錄
211????????????????????????FileUtil.buildFile(directoryPath?+?File.separator
212????????????????????????????????+?zipEntry.getName(),?true);
213????????????????????}
214????????????????}
215????????????}
216????????}?catch?(IOException?ex)?{
217????????????throw?ex;
218????????}?finally?{
219????????????if(null?!=?zipFile){
220????????????????zipFile?=?null;
221????????????}
222????????????if?(null?!=?is)?{
223????????????????is.close();
224????????????}
225????????????if?(null?!=?os)?{
226????????????????os.close();
227????????????}
228????????}
229????}
230}
?? ? * 生產文件 如果文件所在路徑不存在則生成路徑
?? ? *
?? ? * @param fileName
?? ? *??????????? 文件名 帶路徑
?? ? * @param isDirectory 是否為路徑
?? ? * @return
?? ? * @author yayagepei
?? ? * @date 2008-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;
?? ?}
? ? 廢話少說,直接上代碼。
??1 package ?com.resoft.util;??2
??3 import ?java.io.BufferedOutputStream;
??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.io.InputStream;
?10 import ?java.io.OutputStream;
?11 import ?java.util.Enumeration;
?12
?13 import ?org.apache.tools.zip.ZipEntry;
?14 import ?org.apache.tools.zip.ZipFile;
?15 import ?org.apache.tools.zip.ZipOutputStream;
?16
?17 /**
?18?*?壓縮/解壓縮zip包處理類
?19?*?
?20?*?@author?yayagepei
?21?*?@date?2008-8-25
?22?*/
?23 public ? class ?ZipUtil? {
?24
?25????/**
?26?????*?壓縮
?27?????*?
?28?????*?@param?zipFileName
?29?????*????????????壓縮產生的zip包文件名--帶路徑,如果為null或空則默認按文件名生產壓縮文件名
?30?????*?@param?relativePath
?31?????*????????????相對路徑,默認為空
?32?????*?@param?directory
?33?????*????????????文件或目錄的絕對路徑
?34?????*?@throws?FileNotFoundException
?35?????*?@throws?IOException
?36?????*?@author?yayagepei
?37?????*?@date?2008-8-26
?38?????*/
?39????public?static?void?zip(String?zipFileName,?String?relativePath,
?40????????????String?directory)?throws?FileNotFoundException,?IOException?{
?41????????String?fileName?=?zipFileName;
?42????????if?(fileName?==?null?||?fileName.trim().equals(""))?{
?43????????????File?temp?=?new?File(directory);
?44????????????if?(temp.isDirectory())?{
?45????????????????fileName?=?directory?+?".zip";
?46????????????}?else?{
?47????????????????if?(directory.indexOf(".")?>?0)?{
?48????????????????????fileName?=?directory.substring(0,?directory
?49????????????????????????????.lastIndexOf("."))
?50????????????????????????????+?"zip";
?51????????????????}?else?{
?52????????????????????fileName?=?directory?+?".zip";
?53????????????????}
?54????????????}
?55????????}
?56????????ZipOutputStream?zos?=?new?ZipOutputStream(
?57????????????????new?FileOutputStream(fileName));
?58????????try?{
?59????????????zip(zos,?relativePath,?directory);
?60????????}?catch?(IOException?ex)?{
?61????????????throw?ex;
?62????????}?finally?{
?63????????????if?(null?!=?zos)?{
?64????????????????zos.close();
?65????????????}
?66????????}
?67????}
?68
?69????/**
?70?????*?壓縮
?71?????*?
?72?????*?@param?zos
?73?????*????????????壓縮輸出流
?74?????*?@param?relativePath
?75?????*????????????相對路徑
?76?????*?@param?absolutPath
?77?????*????????????文件或文件夾絕對路徑
?78?????*?@throws?IOException
?79?????*?@author?yayagepei
?80?????*?@date?2008-8-26
?81?????*/
?82????private?static?void?zip(ZipOutputStream?zos,?String?relativePath,
?83????????????String?absolutPath)?throws?IOException?{
?84????????File?file?=?new?File(absolutPath);
?85????????if?(file.isDirectory())?{
?86????????????File[]?files?=?file.listFiles();
?87????????????for?(int?i?=?0;?i?<?files.length;?i++)?{
?88????????????????File?tempFile?=?files[i];
?89????????????????if?(tempFile.isDirectory())?{
?90????????????????????String?newRelativePath?=?relativePath?+?tempFile.getName()
?91????????????????????????????+?File.separator;
?92????????????????????createZipNode(zos,?newRelativePath);
?93????????????????????zip(zos,?newRelativePath,?tempFile.getPath());
?94????????????????}?else?{
?95????????????????????zipFile(zos,?tempFile,?relativePath);
?96????????????????}
?97????????????}
?98????????}?else?{
?99????????????zipFile(zos,?file,?relativePath);
100????????}
101????}
102
103????/**
104?????*?壓縮文件
105?????*?
106?????*?@param?zos
107?????*????????????壓縮輸出流
108?????*?@param?file
109?????*????????????文件對象
110?????*?@param?relativePath
111?????*????????????相對路徑
112?????*?@throws?IOException
113?????*?@author?yayagepei
114?????*?@date?2008-8-26
115?????*/
116????private?static?void?zipFile(ZipOutputStream?zos,?File?file,
117????????????String?relativePath)?throws?IOException?{
118????????ZipEntry?entry?=?new?ZipEntry(relativePath?+?file.getName());
119????????zos.putNextEntry(entry);
120????????InputStream?is?=?null;
121????????try?{
122????????????is?=?new?FileInputStream(file);
123????????????int?BUFFERSIZE?=?2?<<?10;
124????????????int?length?=?0;
125????????????byte[]?buffer?=?new?byte[BUFFERSIZE];
126????????????while?((length?=?is.read(buffer,?0,?BUFFERSIZE))?>=?0)?{
127????????????????zos.write(buffer,?0,?length);
128????????????}
129????????????zos.flush();
130????????????zos.closeEntry();
131????????}?catch?(IOException?ex)?{
132????????????throw?ex;
133????????}?finally?{
134????????????if?(null?!=?is)?{
135????????????????is.close();
136????????????}
137????????}
138????}
139
140????/**
141?????*?創建目錄
142?????*?
143?????*?@param?zos
144?????*????????????zip輸出流
145?????*?@param?relativePath
146?????*????????????相對路徑
147?????*?@throws?IOException
148?????*?@author?yayagepei
149?????*?@date?2008-8-26
150?????*/
151????private?static?void?createZipNode(ZipOutputStream?zos,?String?relativePath)
152????????????throws?IOException?{
153????????ZipEntry?zipEntry?=?new?ZipEntry(relativePath);
154????????zos.putNextEntry(zipEntry);
155????????zos.closeEntry();
156????}
157
158????/**
159?????*?解壓縮zip包
160?????*?
161?????*?@param?zipFilePath
162?????*????????????zip文件路徑
163?????*?@param?targetPath
164?????*????????????解壓縮到的位置,如果為null或空字符串則默認解壓縮到跟zip包同目錄跟zip包同名的文件夾下
165?????*?@throws?IOException
166?????*?@author?yayagepei
167?????*?@date?2008-9-28
168?????*/
169????public?static?void?unzip(String?zipFilePath,?String?targetPath)
170????????????throws?IOException?{
171????????OutputStream?os?=?null;
172????????InputStream?is?=?null;
173????????ZipFile?zipFile?=?null;
174????????try?{
175????????????zipFile?=?new?ZipFile(zipFilePath);
176????????????String?directoryPath?=?"";
177????????????if?(null?==?targetPath?||?"".equals(targetPath))?{
178????????????????directoryPath?=?zipFilePath.substring(0,?zipFilePath
179????????????????????????.lastIndexOf("."));
180????????????}?else?{
181????????????????directoryPath?=?targetPath;
182????????????}
183????????????Enumeration?entryEnum?=?zipFile.getEntries();
184????????????if?(null?!=?entryEnum)?{
185????????????????ZipEntry?zipEntry?=?null;
186????????????????while?(entryEnum.hasMoreElements())?{
187????????????????????zipEntry?=?(ZipEntry)?entryEnum.nextElement();
188????????????????????if?(zipEntry.isDirectory())?{
189????????????????????????directoryPath?=?directoryPath?+?File.separator
190????????????????????????????????+?zipEntry.getName();
191????????????????????????System.out.println(directoryPath);
192????????????????????????continue;
193????????????????????}
194????????????????????if?(zipEntry.getSize()?>?0)?{
195????????????????????????//?文件
196????????????????????????File?targetFile?=?FileUtil.buildFile(directoryPath
197????????????????????????????????+?File.separator?+?zipEntry.getName(),?false);
198????????????????????????os?=?new?BufferedOutputStream(new?FileOutputStream(
199????????????????????????????????targetFile));
200????????????????????????is?=?zipFile.getInputStream(zipEntry);
201????????????????????????byte[]?buffer?=?new?byte[4096];
202????????????????????????int?readLen?=?0;
203????????????????????????while?((readLen?=?is.read(buffer,?0,?4096))?>=?0)?{
204????????????????????????????os.write(buffer,?0,?readLen);
205????????????????????????}
206
207????????????????????????os.flush();
208????????????????????????os.close();
209????????????????????}?else?{
210????????????????????????//?空目錄
211????????????????????????FileUtil.buildFile(directoryPath?+?File.separator
212????????????????????????????????+?zipEntry.getName(),?true);
213????????????????????}
214????????????????}
215????????????}
216????????}?catch?(IOException?ex)?{
217????????????throw?ex;
218????????}?finally?{
219????????????if(null?!=?zipFile){
220????????????????zipFile?=?null;
221????????????}
222????????????if?(null?!=?is)?{
223????????????????is.close();
224????????????}
225????????????if?(null?!=?os)?{
226????????????????os.close();
227????????????}
228????????}
229????}
230}
231
?
?補充一下里面用到的一個自己寫的FileUtil的一個方法
/**?? ? * 生產文件 如果文件所在路徑不存在則生成路徑
?? ? *
?? ? * @param fileName
?? ? *??????????? 文件名 帶路徑
?? ? * @param isDirectory 是否為路徑
?? ? * @return
?? ? * @author yayagepei
?? ? * @date 2008-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;
?? ?}
總結
以上是生活随笔為你收集整理的java压缩/解压缩zip格式文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 程序员常挂在嘴边的10句话:刚刚还是好的
- 下一篇: 想要摆脱手工报表困境?1个工具+5个场景