发表新主题 回复该帖子
主题:Java压缩文件和文件夹代码示例(生成zip文件)
jastby
帖子档案  楼主 Java压缩文件和文件夹代码示例(生成zip文件)   Post by : 2010-09-17 18:21:17.0
  • 幼儿园
  • 幼儿园
  • UID:2
  • 主题:126
  • 帖子:219
  • 加为好友 加为好友    发送短信 发送短信
   java代码
  1. package test;  
  2.  
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.util.zip.ZipEntry;  
  9. import java.util.zip.ZipOutputStream;  
  10.  
  11. public class Zip {  
  12.  
  13.     /**  
  14.      * 压缩文件夹  
  15.      * @param zipPath   生成的zip文件路径  
  16.      * @param filePath  需要压缩的文件夹路径  
  17.      * @throws Exception  
  18.      */ 
  19.     public void zipFolder(String zipPath, String filePath) throws Exception {  
  20.         ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipPath));  
  21.         File f = new File(filePath);  
  22.         zipFiles(out, f, "");  
  23.         out.close();  
  24.     }     
  25.  
  26.     /**  
  27.      * 压缩单一个文件  
  28.      * @param zipPath   生成的zip文件路径  
  29.      * @param filePath  需要压缩的文件路径  
  30.      * @throws Exception  
  31.      */ 
  32.     public void zipFile(String zipPath, String filePath) throws Exception {  
  33.         File f = new File(filePath);  
  34.         FileInputStream fis = new FileInputStream(f);  
  35.         BufferedInputStream bis = new BufferedInputStream(fis);  
  36.         byte[] buf = new byte[1024];  
  37.         int len;  
  38.         FileOutputStream fos = new FileOutputStream(zipPath);  
  39.         BufferedOutputStream bos = new BufferedOutputStream(fos);  
  40.         ZipOutputStream zos = new ZipOutputStream(bos);//压缩包  
  41.         ZipEntry ze = new ZipEntry(f.getName());//这是压缩包名里的文件名  
  42.         zos.putNextEntry(ze);// 写入新的ZIP文件条目并将流定位到条目数据的开始处  
  43.  
  44.         while ((len = bis.read(buf)) != -1) {  
  45.             zos.write(buf, 0, len);  
  46.             zos.flush();  
  47.         }  
  48.         bis.close();  
  49.         zos.close();  
  50.  
  51.     }  
  52.       
  53.     /**  
  54.      * 递归调用,压缩文件夹和子文件夹的所有文件  
  55.      * @param out  
  56.      * @param f  
  57.      * @param base  
  58.      * @throws Exception  
  59.      */ 
  60.     private void zipFiles(ZipOutputStream out, File f, String base) throws Exception {  
  61.         if (f.isDirectory()) {  
  62.             File[] fl = f.listFiles();  
  63.             out.putNextEntry(new ZipEntry(base + "/"));  
  64.             base = base.length() == 0 ? "" : base + "/";  
  65.             for (int i = 0; i < fl.length; i++) {  
  66.                 zipFiles(out, fl[i], base + fl[i].getName());//递归压缩子文件夹  
  67.             }  
  68.         } else {  
  69.             out.putNextEntry(new ZipEntry(base));  
  70.             FileInputStream in = new FileInputStream(f);  
  71.             int b;  
  72.             //System.out.println(base);  
  73.             while ((b = in.read()) != -1) {  
  74.                 out.write(b);  
  75.             }  
  76.             in.close();  
  77.         }  
  78.     }  
  79.  
  80.     /**  
  81.      * @param args  
  82.      */ 
  83.     public static void main(String[] args) {  
  84.         System.out.println("Zip File Begin");  
  85.  
  86.         Zip zip = new Zip();  
  87.         String zipPath = "E:\\test.zip";  
  88.         String filePath = "E:\\index.pdf";  
  89.         try {  
  90.             zip.zipFile(zipPath, filePath);//压缩文件  
  91.             //zip.zipFolder(zipPath, filePath);//压缩文件夹  
  92.         } catch (Exception ex) {  
  93.             ex.printStackTrace();  
  94.         }  
  95.         System.out.println("Zip File Done");  
  96.     }  
  97.  
返回页面顶部  

唧唧
2F Re:~   Post by : 2010-11-12 14:00:21.0
  • 幼儿园
  • 幼儿园
  • UID:3
  • 主题:342
  • 帖子:781
  • 加为好友 加为好友    发送短信 发送短信
em09,好东西,谢谢分享,mark一下!
签名
 ★★★★★★★★
 纵里寻她千百度,蓦然回首,那人却在,灯火阑珊处!
 MyBlog :http://blog.javawind.net
返回页面顶部  


CopyRight © 2008-2009 JavaWind.Net Studio All Rights Reserved
Powered By JWind.BBS Vesion 1.0.0 Beta1 Processed in 31 ms,0 (Queries)  Gzip enabled

WAP - 清除Cookies - 联系我们 - JavaWind.Net Studio - Archiver - TOP Valid XHTML 1.0 Transitional Valid CSS! 粤ICP备07511478号