java实现将ZIP压缩文件解压的工具类

来源:互联网 发布:华为网络机顶盒价格 编辑:程序博客网 时间:2024/05/29 19:52
[java] view plaincopyprint?
  1. package com.lanp;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.util.zip.ZipEntry;  
  8. import java.util.zip.ZipException;  
  9. import java.util.zip.ZipFile;  
  10. import java.util.zip.ZipInputStream;  
  11.   
  12. /** 
  13.  * 解压ZIP压缩文件到指定的目录 
  14.  * @author LanP 
  15.  * @since 2012-3-13 8:57:18 
  16.  */  
  17. public final class ZipToFile {  
  18.     /** 
  19.      * 缓存区大小默认20480 
  20.      */  
  21.     private final static int FILE_BUFFER_SIZE = 20480;  
  22.       
  23.     private ZipToFile() {  
  24.           
  25.     }  
  26.       
  27.     /** 
  28.      * 将指定目录的ZIP压缩文件解压到指定的目录 
  29.      * @param zipFilePath       ZIP压缩文件的路径 
  30.      * @param zipFileName       ZIP压缩文件名字 
  31.      * @param targetFileDir     ZIP压缩文件要解压到的目录 
  32.      * @return flag             布尔返回值 
  33.      */  
  34.     public static boolean unzip(String zipFilePath, String zipFileName, String targetFileDir){  
  35.         boolean flag = false;  
  36.         //1.判断压缩文件是否存在,以及里面的内容是否为空  
  37.         File file = null;           //压缩文件(带路径)  
  38.         ZipFile zipFile = null;  
  39.         file = new File(zipFilePath + "/" + zipFileName);  
  40.         System.out.println(">>>>>>解压文件【" + zipFilePath + "/" + zipFileName + "】到【" + targetFileDir + "】目录下<<<<<<");  
  41.         if(false == file.exists()) {  
  42.             System.out.println(">>>>>>压缩文件【" + zipFilePath + "/" + zipFileName + "】不存在<<<<<<");  
  43.             return false;  
  44.         } else if(0 == file.length()) {  
  45.             System.out.println(">>>>>>压缩文件【" + zipFilePath + "/" + zipFileName + "】大小为0不需要解压<<<<<<");  
  46.             return false;  
  47.         } else {  
  48.             //2.开始解压ZIP压缩文件的处理  
  49.             byte[] buf = new byte[FILE_BUFFER_SIZE];  
  50.             int readSize = -1;  
  51.             ZipInputStream zis = null;  
  52.             FileOutputStream fos = null;  
  53.             try {  
  54.                 // 检查是否是zip文件  
  55.                 zipFile = new ZipFile(file);  
  56.                 zipFile.close();  
  57.                 // 判断目标目录是否存在,不存在则创建  
  58.                 File newdir = new File(targetFileDir);  
  59.                 if (false == newdir.exists()) {  
  60.                     newdir.mkdirs();  
  61.                     newdir = null;  
  62.                 }  
  63.                 zis = new ZipInputStream(new FileInputStream(file));  
  64.                 ZipEntry zipEntry = zis.getNextEntry();  
  65.                 // 开始对压缩包内文件进行处理  
  66.                 while (null != zipEntry) {  
  67.                     String zipEntryName = zipEntry.getName().replace('\\', '/');  
  68.                     //判断zipEntry是否为目录,如果是,则创建  
  69.                     if(zipEntry.isDirectory()) {  
  70.                         int indexNumber = zipEntryName.lastIndexOf('/');  
  71.                         File entryDirs = new File(targetFileDir + "/" + zipEntryName.substring(0, indexNumber));  
  72.                         entryDirs.mkdirs();  
  73.                         entryDirs = null;  
  74.                     } else {  
  75.                         try {  
  76.                             fos = new FileOutputStream(targetFileDir + "/" + zipEntryName);  
  77.                             while ((readSize = zis.read(buf, 0, FILE_BUFFER_SIZE)) != -1) {  
  78.                                 fos.write(buf, 0, readSize);  
  79.                             }  
  80.                         } catch (Exception e) {  
  81.                             e.printStackTrace();  
  82.                             throw new RuntimeException(e.getCause());   
  83.                         } finally {  
  84.                             try {  
  85.                                 if (null != fos) {  
  86.                                     fos.close();  
  87.                                 }  
  88.                             } catch (IOException e) {  
  89.                                 e.printStackTrace();  
  90.                                 throw new RuntimeException(e.getCause());   
  91.                             }  
  92.                         }  
  93.                     }  
  94.                     zipEntry = zis.getNextEntry();  
  95.                 }  
  96.                 flag = true;  
  97.             } catch (ZipException e) {  
  98.                 e.printStackTrace();  
  99.                 throw new RuntimeException(e.getCause());   
  100.             } catch (IOException e) {  
  101.                 e.printStackTrace();  
  102.                 throw new RuntimeException(e.getCause());   
  103.             } finally {  
  104.                 try {  
  105.                     if (null != zis) {  
  106.                         zis.close();  
  107.                     }  
  108.                     if (null != fos) {  
  109.                         fos.close();  
  110.                     }  
  111.                 } catch (IOException e) {  
  112.                     e.printStackTrace();  
  113.                     throw new RuntimeException(e.getCause());   
  114.                 }  
  115.             }  
  116.         }  
  117.         return flag;  
  118.     }  
  119.   
  120.     /** 
  121.      * 测试用的Main方法 
  122.      */  
  123.     public static void main(String[] args) {  
  124.         String zipFilePath = "C:\\home";  
  125.         String zipFileName = "lp20120301.zip";  
  126.         String targetFileDir = "C:\\home\\lp20120301";  
  127.         boolean flag = ZipToFile.unzip(zipFilePath, zipFileName, targetFileDir);  
  128.         if(flag) {  
  129.             System.out.println(">>>>>>解压成功<<<<<<");  
  130.         } else {  
  131.             System.out.println(">>>>>>解压失败<<<<<<");  
  132.         }  
  133.     }  
  134.   
  135. }  

0 0
原创粉丝点击