转载 使用ant.jar进行文件zip压缩

来源:互联网 发布:丁威特电子狗升级软件 编辑:程序博客网 时间:2024/04/27 17:28
转载 至 http://log-cd.iteye.com/blog/585647

Java代码  收藏代码
  1. import java.io.BufferedInputStream;  
  2. import java.io.BufferedOutputStream;  
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.io.OutputStream;  
  9. import java.util.Enumeration;  
  10. import org.apache.tools.zip.ZipEntry;  
  11. import org.apache.tools.zip.ZipFile;  
  12. import org.apache.tools.zip.ZipOutputStream;  
  13.   
  14. /** 
  15.  * 功能:  
  16.  * 1 、实现把指定文件夹下的所有文件压缩为指定文件夹下指定 zip 文件  
  17.  * 2 、实现把指定文件夹下的 zip 文件解压到指定目录下 
  18.  */  
  19.   
  20. public class ZipUtils {  
  21.   
  22.     public static void main(String[] args) {  
  23.   
  24.         zip ("D:\\zip测试""D:\\测试结果.zip");  
  25.   
  26.         unZip("D:\\测试结果.zip""D:\\解压结果");  
  27.   
  28.     }  
  29.   
  30.     /** 
  31.      * 功能:把 sourceDir 目录下的所有文件进行 zip 格式的压缩,保存为指定 zip 文件 
  32.      * @param sourceDir 
  33.      * @param zipFile 
  34.      */  
  35.   
  36.     public static void zip(String sourceDir, String zipFile) {  
  37.   
  38.         OutputStream os;  
  39.   
  40.         try {  
  41.   
  42.             os = new FileOutputStream(zipFile);  
  43.   
  44.             BufferedOutputStream bos = new BufferedOutputStream(os);  
  45.   
  46.             ZipOutputStream zos = new ZipOutputStream(bos);  
  47.   
  48.             File file = new File(sourceDir);  
  49.   
  50.             String basePath = null;  
  51.   
  52.             if (file.isDirectory()) {  
  53.   
  54.                 basePath = file.getPath();  
  55.   
  56.             } else {//直接压缩单个文件时,取父目录  
  57.   
  58.                 basePath = file.getParent();  
  59.   
  60.             }  
  61.   
  62.             zipFile(file, basePath, zos);  
  63.   
  64.             zos.closeEntry();  
  65.   
  66.             zos.close();  
  67.   
  68.         } catch (Exception e) {  
  69.   
  70.             e.printStackTrace();  
  71.   
  72.         }  
  73.   
  74.     }  
  75.   
  76.     /** 
  77.      * 功能:执行文件压缩成zip文件 
  78.      * @param source 
  79.      * @param basePath  待压缩文件根目录 
  80.      * @param zos 
  81.      */  
  82.   
  83.     private static void zipFile(File source, String basePath,  
  84.   
  85.     ZipOutputStream zos) {  
  86.   
  87.         File[] files = new File[0];  
  88.   
  89.         if (source.isDirectory()) {  
  90.   
  91.             files = source.listFiles();  
  92.   
  93.         } else {  
  94.   
  95.             files = new File[1];  
  96.   
  97.             files[0] = source;  
  98.   
  99.         }  
  100.   
  101.         String pathName;//存相对路径(相对于待压缩的根目录)  
  102.   
  103.         byte[] buf = new byte[1024];  
  104.   
  105.         int length = 0;  
  106.   
  107.         try {  
  108.   
  109.             for (File file : files) {  
  110.   
  111.                 if (file.isDirectory()) {  
  112.   
  113.                     pathName = file.getPath().substring(basePath.length() + 1)  
  114.   
  115.                     + "/";  
  116.   
  117.                     zos.putNextEntry(new ZipEntry(pathName));  
  118.                       
  119.                     zipFile(file, basePath, zos);  
  120.   
  121.                 } else {  
  122.   
  123.                     pathName = file.getPath().substring(basePath.length() + 1);  
  124.   
  125.                     InputStream is = new FileInputStream(file);  
  126.   
  127.                     BufferedInputStream bis = new BufferedInputStream(is);  
  128.   
  129.                     zos.putNextEntry(new ZipEntry(pathName));  
  130.   
  131.                     while ((length = bis.read(buf)) > 0) {  
  132.   
  133.                         zos.write(buf, 0, length);  
  134.   
  135.                     }  
  136.   
  137.                     is.close();  
  138.   
  139.                 }  
  140.   
  141.             }  
  142.   
  143.         } catch (Exception e) {  
  144.   
  145.             e.printStackTrace();  
  146.   
  147.         }  
  148.   
  149.     }  
  150.   
  151.     /** 
  152.      * 功能:解压 zip 文件,只能解压 zip 文件 
  153.      * @param zipfile 
  154.      * @param destDir 
  155.      */  
  156.   
  157.     public static void unZip(String zipfile, String destDir) {  
  158.   
  159.         destDir = destDir.endsWith("\\") ? destDir : destDir + "\\";  
  160.   
  161.         byte b[] = new byte[1024];  
  162.   
  163.         int length;  
  164.   
  165.         ZipFile zipFile;  
  166.   
  167.         try {  
  168.   
  169.             zipFile = new ZipFile(new File(zipfile));  
  170.   
  171.             Enumeration enumeration = zipFile.getEntries();  
  172.   
  173.             ZipEntry zipEntry = null;  
  174.   
  175.             while (enumeration.hasMoreElements()) {  
  176.   
  177.                 zipEntry = (ZipEntry) enumeration.nextElement();  
  178.   
  179.                 File loadFile = new File(destDir + zipEntry.getName());  
  180.   
  181.                 if (zipEntry.isDirectory()) {  
  182.   
  183.                     loadFile.mkdirs();  
  184.   
  185.                 } else {  
  186.   
  187.                     if (!loadFile.getParentFile().exists()){  
  188.   
  189.                         loadFile.getParentFile().mkdirs();  
  190.                           
  191.                     }  
  192.   
  193.                     OutputStream outputStream = new FileOutputStream(loadFile);  
  194.   
  195.                     InputStream inputStream = zipFile.getInputStream(zipEntry);  
  196.   
  197.                     while ((length = inputStream.read(b)) > 0)  
  198.   
  199.                         outputStream.write(b, 0, length);  
  200.   
  201.                 }  
  202.   
  203.             }  
  204.   
  205.         } catch (IOException e) {  
  206.   
  207.             e.printStackTrace();  
  208.   
  209.         }  
  210.   
  211.     }  
  212.   
  213. }  
  • ant.jar (1.2 MB)
  • 下载次数: 321
原创粉丝点击