`
kobe学java
  • 浏览: 249856 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

利于apache提供的ant包压缩文件

 
阅读更多

Java代码   收藏代码
  1. /** 
  2.  *  
  3.  */  
  4. package cn.edu.calis.zipfiletest;  
  5.   
  6. import java.io.File;  
  7. import java.io.FileInputStream;  
  8. import java.io.FileOutputStream;  
  9. import java.io.IOException;  
  10. import java.io.InputStream;  
  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.  * 利于ant包来压缩文件 可对空文件夹进行压缩<br> 
  19.  *  
  20.  * 注:压缩文件的关键步骤在于对文件的读取,存放的时特别要注意文件的路径,最好的方式用相对路径, 
  21.  *  
  22.  *  
  23.  * @author Administrator 
  24.  *  
  25.  */  
  26. public class AntZipFile4 {  
  27.     boolean flag = true// 压缩成功与否标志  
  28.     private ZipFile zipFile;  
  29.     private ZipEntry zipEntry;  
  30.     private ZipOutputStream zipOut; // 压缩Zip  
  31.     private static int bufSize; // size of bytes  
  32.     private byte[] buf;  
  33.     private int readedBytes;  
  34.   
  35.     public AntZipFile4() {  
  36.         this(1024);  
  37.     }  
  38.   
  39.     public AntZipFile4(int size) {  
  40.         this.bufSize = size;  
  41.         this.buf = new byte[this.bufSize];  
  42.     }  
  43.   
  44.     public boolean doZip(String srcStr, String targetStr, String targetName) {  
  45.         File zipDir = new File(srcStr);  
  46.         // 未指定压缩文件名,默认为"ZipFile"  
  47.         if (targetName == null || "".equals(targetName))  
  48.             targetName = "ZipFile";  
  49.         // 添加".zip"后缀  
  50.         if (!targetName.endsWith(".zip"))  
  51.             targetName += ".zip";  
  52.         try {  
  53.             this.zipOut = new ZipOutputStream(new FileOutputStream(new File(  
  54.                     targetStr + File.separator + targetName)));  
  55.             // 压缩文件  
  56.             compressFile(zipDir, "", zipOut);  
  57.             zipOut.close();  
  58.         } catch (IOException e) {  
  59.             flag = false;  
  60.             e.printStackTrace();  
  61.         }  
  62.         return flag;  
  63.     }  
  64.   
  65.     /** 
  66.      * 压缩目录(包含空目录)与文件 
  67.      *  
  68.      * @param srcFile 
  69.      *            待压缩的目录与文件 
  70.      * @param oppositePath 
  71.      *            压缩的相对路径,用于按照原文件的目录结构压缩文件 
  72.      * @param zipOut 
  73.      *            ZipOutputStream压缩流 
  74.      * @throws IOException 
  75.      */  
  76.     public void compressFile(File srcFile, String oppositePath,  
  77.             ZipOutputStream zipOut) throws IOException {  
  78.         /* 
  79.          * 因为是空目录,所以要在结尾加一个"/"。 不然就会被当作是空文件。 ZipEntry的isDirectory()方法中,目录以"/"结尾. 
  80.          * org.apache.tools.zip.ZipEntry : public boolean isDirectory() { return 
  81.          * getName().endsWith("/"); } 
  82.          */  
  83.         if (srcFile.isDirectory()) {// 为目录时  
  84.             String newOppositePath = oppositePath + srcFile.getName() + "/"// 得到相对路径  
  85.             compressFolder(srcFile, newOppositePath, zipOut);  
  86.         }  
  87.         // 如果是文件则需读;如果是空目录则无需读,直接转到zipOut.closeEntry()。  
  88.         if (srcFile.isFile()) {// 为文件时  
  89.             ZipEntry zipe = new ZipEntry(oppositePath + srcFile.getName());  
  90.             try {  
  91.                 zipOut.putNextEntry(zipe);  
  92.                 FileInputStream fileIn = new FileInputStream(srcFile);  
  93.                 while ((this.readedBytes = fileIn.read(this.buf)) > 0) {  
  94.                     zipOut.write(this.buf, 0this.readedBytes);  
  95.                 }  
  96.                 fileIn.close();  
  97.                 zipOut.closeEntry();  
  98.             } catch (Exception e) {  
  99.             }  
  100.         }  
  101.   
  102.     }  
  103.   
  104.     /** 
  105.      * 递归完成目录文件读取,由compressFile()方法调用 
  106.      *  
  107.      * @param file 
  108.      *            要读取的文件目录 
  109.      * @param oppositePath 
  110.      *            相对路径,用于按照原文件的目录结构压缩文件 
  111.      * @param zipOut 
  112.      *            ZipOutputStream压缩流 
  113.      * @throws IOException 
  114.      */  
  115.     private void compressFolder(File file, String oppositePath,  
  116.             ZipOutputStream zipOut) throws IOException {  
  117.         File[] files = file.listFiles();  
  118.   
  119.         if (files.length == 0) {// 如果目录为空,则单独压缩空目录。  
  120.             String fileName = files.toString();  
  121.             /* 
  122.              * 因为是空目录,所以要在结尾加一个"/"。 不然就会被当作是空文件。 
  123.              * ZipEntry的isDirectory()方法中,目录以"/"结尾. org.apache.tools.zip.ZipEntry : 
  124.              * public boolean isDirectory() { return getName().endsWith("/"); } 
  125.              * 这里oppositePath已经为相对路径,故zipOut.putNextEntry(new 
  126.              * ZipEntry(oppositePath)); 
  127.              */  
  128.             if (file.isDirectory())  
  129.                 fileName = fileName + "/";// 此处不能用"\\"  
  130.             zipOut.putNextEntry(new ZipEntry(oppositePath));  
  131.             // 如果是文件则需读;如果是空目录则无需读,直接转到zipOut.closeEntry()。  
  132.             if (file.isFile()) {  
  133.                 FileInputStream fileIn = new FileInputStream(file);  
  134.                 while ((this.readedBytes = fileIn.read(this.buf)) > 0) {  
  135.                     zipOut.write(this.buf, 0this.readedBytes);  
  136.                 }  
  137.                 fileIn.close();  
  138.             }  
  139.             zipOut.closeEntry();  
  140.         } else  
  141.             // 如果目录不为空,则分别处理目录和文件  
  142.             for (File subfile : files) {  
  143.                 compressFile(subfile, oppositePath, zipOut);  
  144.             }  
  145.     }  
  146.   
  147.     /** 
  148.      * 解压指定zip文件 
  149.      *  
  150.      * @param srcZipFile 
  151.      *            待解压的文件(包含路径) 
  152.      * @param targetStr 
  153.      *            解压存放的路径 
  154.      * @param targetName 
  155.      *            解压的存放的目录 
  156.      */  
  157.     public void upZip(String srcZipFile, String targetStr, String targetName) {  
  158.         FileOutputStream fileOut;  
  159.         File file;  
  160.         InputStream inputStream;  
  161.         try {  
  162.             this.zipFile = new ZipFile(srcZipFile);  
  163.             // 遍历每个压缩文件  
  164.             for (Enumeration entries = this.zipFile.getEntries(); entries  
  165.                     .hasMoreElements();) {  
  166.                 zipEntry = (ZipEntry) entries.nextElement();  
  167.                 file = new File(targetStr + "/" + targetName + "/"  
  168.                         + zipEntry.getName());  
  169.   
  170.                 if (zipEntry.isDirectory()) {// 是目录,则创建之  
  171.                     file.mkdirs();  
  172.                 } else {// 是文件  
  173.                     // 如果指定文件的父目录不存在,则创建之  
  174.                     File parent = file.getParentFile();  
  175.                     if (null == parent || !parent.exists()) {  
  176.                         parent.mkdirs();  
  177.                     }  
  178.                     // 写文件  
  179.                     inputStream = zipFile.getInputStream(zipEntry);  
  180.                     fileOut = new FileOutputStream(file);  
  181.                     while ((this.readedBytes = inputStream.read(this.buf)) > 0) {  
  182.                         fileOut.write(this.buf, 0this.readedBytes);  
  183.                     }  
  184.                     fileOut.close();  
  185.                     inputStream.close();  
  186.                 }  
  187.             }  
  188.         } catch (IOException e) {  
  189.             e.printStackTrace();  
  190.         }  
  191.     }  
  192.   
  193.     /** 
  194.      * 设置压缩或解压时缓冲区大小。 
  195.      *  
  196.      * @param bufSize 
  197.      *            缓冲区大小 
  198.      */  
  199.     public static void setBufSize(int bufSize) {  
  200.         AntZipFile4.bufSize = bufSize;  
  201.     }  
  202.   
  203.     /** 
  204.      * @param args 
  205.      */  
  206.     public static void main(String[] args) {  
  207.         AntZipFile4 test = new AntZipFile4();  
  208.   
  209.         test.doZip("D:/乱七八糟""E:/乱七八糟的""aa.zip");  
  210.         test.upZip("E:/乱七八糟的/aa.zip""f:/""aa");  
  211.   
  212.     }  
  213.   
  214. }  


上面的注释写的很说细,应该能看明白的。 
下面说说学习的心得吧: 
1,为什么我会选择apache提代的ant包来进行是压缩文件,其实java.util.zip包下也提供了压缩的类,但我没有先用,因为它对中文的支持不是很好,中文时会出现乱码,当然你可以修改源码后就可以了,而apache提代的ant包可以解决中文问题,也可以自己设定字符编码: 
ZipOutputStream类的setEncoding("")方法 
2,无论你选择何种包来压缩文件,压缩文件时,文件的路径都很重要,特别是当文件存在多级的情况下,一般你所写的文件中有递归方法,而递归方法在调用时,文件的路径需要很好的去把握。 
好了,我只是在这抛砖引玉,欢迎大家叫板。 
下面提供了源码 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics