生活随笔
收集整理的這篇文章主要介紹了
java实现zip压缩文件 (一)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
網(wǎng)上查了許久,最后發(fā)現(xiàn)三種不錯(cuò)的方法:
1、jdk自帶的包java.util.zip.ZipOutputStream,不足之處,文件(夾)名稱帶中文時(shí),
出現(xiàn)亂碼問題,實(shí)現(xiàn)代碼如下:
/**
? * 功能:把 sourceDir 目錄下的所有文件進(jìn)行 zip 格式的壓縮,保存為指定 zip 文件
? * @param sourceDir 如果是目錄,eg:D:\\MyEclipse\\first\\testFile,則壓縮目錄下所有文件;
? * ?????如果是文件,eg:D:\\MyEclipse\\first\\testFile\\aa.zip,則只壓縮本文件
? * @param zipFile 最后壓縮的文件路徑和名稱,eg:D:\\MyEclipse\\first\\testFile\\aa.zip
? */
?public File doZip(String sourceDir, String zipFilePath)
?throws IOException {
??
??File file = new File(sourceDir);
??File zipFile = new File(zipFilePath);
??ZipOutputStream zos = null;
??try {
???// 創(chuàng)建寫出流操作
???OutputStream os = new FileOutputStream(zipFile);
???BufferedOutputStream bos = new BufferedOutputStream(os);
???zos = new ZipOutputStream(bos);
???
???String basePath = null;
???
???// 獲取目錄
???if(file.isDirectory()) {
????basePath = file.getPath();
???}else {
????basePath = file.getParent();
???}
???
???zipFile(file, basePath, zos);
??}finally {
???if(zos != null) {
????zos.closeEntry();
????zos.close();
???}
??}
??
??return zipFile;
?}
?/**
? * @param source 源文件
? * @param basePath
? * @param zos
? */
?private void zipFile(File source, String basePath, ZipOutputStream zos)
?throws IOException {
??File[] files = null;
??if (source.isDirectory()) {
???files = source.listFiles();
??} else {
???files = new File[1];
???files[0] = source;
??}
??
??InputStream is = null;
??String pathName;
??byte[] buf = new byte[1024];
??int length = 0;
??try{
???for(File file : files) {
????if(file.isDirectory()) {
?????pathName = file.getPath().substring(basePath.length() + 1) + "/";
?????zos.putNextEntry(new ZipEntry(pathName));
?????zipFile(file, basePath, zos);
????}else {
?????pathName = file.getPath().substring(basePath.length() + 1);
?????is = new FileInputStream(file);
?????BufferedInputStream bis = new BufferedInputStream(is);
?????zos.putNextEntry(new ZipEntry(pathName));
?????while ((length = bis.read(buf)) > 0) {
??????zos.write(buf, 0, length);
?????}
????}
???}
??}finally {
???if(is != null) {
????is.close();
???}
??}
?}
2、使用org.apache.tools.zip.ZipOutputStream,代碼如下,
Java代碼:
package?net.szh.zip; ????import?java.io.BufferedInputStream; ??import?java.io.File; ??import?java.io.FileInputStream; ??import?java.io.FileOutputStream; ??import?java.util.zip.CRC32; ??import?java.util.zip.CheckedOutputStream; ????import?org.apache.tools.zip.ZipEntry; ??import?org.apache.tools.zip.ZipOutputStream; ????public?class?ZipCompressor?{ ??????static?final?int?BUFFER?=?8192; ????????private?File?zipFile; ????????public?ZipCompressor(String?pathName)?{ ??????????zipFile?=?new?File(pathName); ??????} ????????public?void?compress(String?srcPathName)?{ ??????????File?file?=?new?File(srcPathName); ??????????if?(!file.exists()) ??????????????throw?new?RuntimeException(srcPathName?+?"不存在!"); ??????????try?{ ??????????????FileOutputStream?fileOutputStream?=?new?FileOutputStream(zipFile); ??????????????CheckedOutputStream?cos?=?new?CheckedOutputStream(fileOutputStream, ??????????????????????new?CRC32()); ??????????????ZipOutputStream?out?=?new?ZipOutputStream(cos); ??????????????String?basedir?=?""; ??????????????compress(file,?out,?basedir); ??????????????out.close(); ??????????}?catch?(Exception?e)?{ ??????????????throw?new?RuntimeException(e); ??????????} ??????} ????????private?void?compress(File?file,?ZipOutputStream?out,?String?basedir)?{ ??????????/*?判斷是目錄還是文件?*/??????????if?(file.isDirectory())?{ ??????????????System.out.println("壓縮:"?+?basedir?+?file.getName()); ??????????????this.compressDirectory(file,?out,?basedir); ??????????}?else?{ ??????????????System.out.println("壓縮:"?+?basedir?+?file.getName()); ??????????????this.compressFile(file,?out,?basedir); ??????????} ??????} ????????/**?壓縮一個(gè)目錄?*/??????private?void?compressDirectory(File?dir,?ZipOutputStream?out,?String?basedir)?{ ??????????if?(!dir.exists()) ??????????????return; ????????????File[]?files?=?dir.listFiles(); ??????????for?(int?i?=?0;?i?<?files.length;?i++)?{ ??????????????/*?遞歸?*/??????????????compress(files[i],?out,?basedir?+?dir.getName()?+?"/"); ??????????} ??????} ????????/**?壓縮一個(gè)文件?*/??????private?void?compressFile(File?file,?ZipOutputStream?out,?String?basedir)?{ ??????????if?(!file.exists())?{ ??????????????return; ??????????} ??????????try?{ ??????????????BufferedInputStream?bis?=?new?BufferedInputStream( ??????????????????????new?FileInputStream(file)); ??????????????ZipEntry?entry?=?new?ZipEntry(basedir?+?file.getName()); ??????????????out.putNextEntry(entry); ??????????????int?count; ??????????????byte?data[]?=?new?byte[BUFFER]; ??????????????while?((count?=?bis.read(data,?0,?BUFFER))?!=?-1)?{ ??????????????????out.write(data,?0,?count); ??????????????} ??????????????bis.close(); ??????????}?catch?(Exception?e)?{ ??????????????throw?new?RuntimeException(e); ??????????} ??????} ??}?
?
3、可以用ant中的org.apache.tools.ant.taskdefs.Zip來實(shí)現(xiàn),更加簡單。
Java代碼
package?net.szh.zip; ????import?java.io.File; ????import?org.apache.tools.ant.Project; ??import?org.apache.tools.ant.taskdefs.Zip; ??import?org.apache.tools.ant.types.FileSet; ????public?class?ZipCompressorByAnt?{ ????????private?File?zipFile; ????????public?ZipCompressorByAnt(String?pathName)?{ ??????????zipFile?=?new?File(pathName); ??????} ?????? ??????public?void?compress(String?srcPathName)?{ ??????????File?srcdir?=?new?File(srcPathName); ??????????if?(!srcdir.exists()) ??????????????throw?new?RuntimeException(srcPathName?+?"不存在!"); ?????????? ??????????Project?prj?=?new?Project(); ??????????Zip?zip?=?new?Zip(); ??????????zip.setProject(prj); ??????????zip.setDestFile(zipFile); ??????????FileSet?fileSet?=?new?FileSet(); ??????????fileSet.setProject(prj); ??????????fileSet.setDir(srcdir); ??????????//fileSet.setIncludes("**/*.java");?包括哪些文件或文件夾?eg:zip.setIncludes("*.java"); ??????????//fileSet.setExcludes(...);?排除哪些文件或文件夾 ??????????zip.addFileset(fileSet); ?????????? ??????????zip.execute(); ??????} ??}??
測(cè)試一下
Java代碼
package?net.szh.zip; ????public?class?TestZip?{ ??????public?static?void?main(String[]?args)?{ ??????????ZipCompressor?zc?=?new??ZipCompressor("E:\\szhzip.zip"); ??????????zc.compress("E:\\test"); ?????????? ??????????ZipCompressorByAnt?zca?=?new?ZipCompressorByAnt("E:\\szhzipant.zip"); ??????????zca.compress("E:\\test"); ??????} ??}?
總結(jié)
以上是生活随笔為你收集整理的java实现zip压缩文件 (一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。