IO Foundation 6-解压缩zip文件

 

需求:

这一步也非常简单,主要是demo下ZipEntry的用法,就是把一个以zip扩展名结尾的文件进行解压缩。

所以我就直接贴上代码了:

 

实现:

 

  
  
  
  
  1. /** 
  2.  
  3. * This class will handle the folder zipper related operations 
  4.  
  5. * 
  6.  
  7.  * @author cwang58 
  8.  
  9. * @created date: Aug 4, 2012 
  10.  
  11. */ 
  12.  
  13. public class FolderZipper implements IFolderZipper { 
  14.  
  15.   
  16.  
  17.          private static Logger logger = LoggerFactory.getLogger(FolderZipper.class); 
  18.  
  19.   
  20.  
  21.          /** 
  22.  
  23.          * to unzip a zip file ,it will read all the entries in the zip file if the 
  24.  
  25.          * entry is a folder ,then it will created a folder structure (including the 
  26.  
  27.          * parent folder structure) if the entry is a file ,then it use file output 
  28.  
  29.          * stream to output this file 
  30.  
  31.          * 
  32.  
  33.           * @param zipFileName 
  34.  
  35.          *            the zip file name 
  36.  
  37.          * @param destPath 
  38.  
  39.          *            the path that we want to zip to 
  40.  
  41.          */ 
  42.  
  43.          public void unzip(String zipFileName, String destPath) throws Exception { 
  44.  
  45.   
  46.  
  47.                  // verify the parameters 
  48.  
  49.                  if (zipFileName == null || zipFileName.trim().equals(EMPTY_STRING)) { 
  50.  
  51.                           if (logger.isErrorEnabled()) { 
  52.  
  53.                                    logger.error("zipFileName parameter can't be null"); 
  54.  
  55.                           } 
  56.  
  57.                           throw new ParameterNullException(ZIP_UNZIP_ERROR); 
  58.  
  59.                  } 
  60.  
  61.   
  62.  
  63.                  if (destPath == null) { 
  64.  
  65.   
  66.  
  67.                           if (logger.isErrorEnabled()) { 
  68.  
  69.                                    logger.error("destPath parameter can't be null"); 
  70.  
  71.                           } 
  72.  
  73.                           throw new ParameterNullException("ZIP_UNZIP_ERROR"); 
  74.  
  75.                  } 
  76.  
  77.   
  78.  
  79.                  // verify the zip file 's file extension (only support ZIP) 
  80.  
  81.                  if (!zipFileName.trim().endsWith(ZIP_EXTENSION)) { 
  82.  
  83.   
  84.  
  85.                           if (logger.isErrorEnabled()) { 
  86.  
  87.                                    logger.error("the zip file MUST BE end with zip suffix"); 
  88.  
  89.                           } 
  90.  
  91.                           throw new ParameterInvalidException(ZIP_UNZIP_ERROR); 
  92.  
  93.                  } 
  94.  
  95.   
  96.  
  97.                  try { 
  98.  
  99.   
  100.  
  101.                           FileInputStream fis = new FileInputStream(zipFileName); 
  102.  
  103.                           ZipInputStream zis = new ZipInputStream(fis); 
  104.  
  105.                           ZipEntry ze = null
  106.  
  107.   
  108.  
  109.                           if (destPath.equals(DOT)) 
  110.  
  111.                                    destPath = EMPTY_STRING; 
  112.  
  113.   
  114.  
  115.                           while ((ze = zis.getNextEntry()) != null) { 
  116.  
  117.   
  118.  
  119.                                    // if the zipEntry is a folder 
  120.  
  121.                                    // then must create the folder structure 
  122.  
  123.                                    if (ze.isDirectory()) { 
  124.  
  125.   
  126.  
  127.                                             if (logger.isDebugEnabled()) { 
  128.  
  129.                                                      logger.debug("Processing folder:" + ze.getName()); 
  130.  
  131.                                             } 
  132.  
  133.   
  134.  
  135.                                             // construct the dirName by string concating the destpath 
  136.  
  137.                                             // and the foldername 
  138.  
  139.                                             String dirName = destPath + ze.getName(); 
  140.  
  141.   
  142.  
  143.                                             // creating the folder structure 
  144.  
  145.                                             File f = new File(dirName); 
  146.  
  147.                                             f.mkdirs(); 
  148.  
  149.   
  150.  
  151.                                             if (logger.isDebugEnabled()) { 
  152.  
  153.                                                     logger.debug("The folder " + f.getAbsolutePath() 
  154.  
  155.                                                                       + " is created"); 
  156.  
  157.                                             } 
  158.  
  159.                                    } 
  160.  
  161.   
  162.  
  163.                                    // if the zipEntry file is a file 
  164.  
  165.                                    // then use a FileOutputStream to write this file 
  166.  
  167.                                    else { 
  168.  
  169.                                             if (logger.isDebugEnabled()) { 
  170.  
  171.                                                     logger.debug("Processing file: " + ze.getName()); 
  172.  
  173.                                             } 
  174.  
  175.   
  176.  
  177.                                             // construct the fileName by string concating the destpath 
  178.  
  179.                                             // and the foldername 
  180.  
  181.                                            String fileName = destPath + ze.getName(); 
  182.  
  183.                                             File f = new File(fileName); 
  184.  
  185.   
  186.  
  187.                                             // open a FileOutputStream to write this file 
  188.  
  189.                                             FileOutputStream fos = new FileOutputStream(f); 
  190.  
  191.                                             int tmp = -1
  192.  
  193.                                             while ((tmp = zis.read()) != -1) { 
  194.  
  195.                                                     fos.write(tmp); 
  196.  
  197.                                             } 
  198.  
  199.                                             zis.closeEntry(); 
  200.  
  201.                                             fos.close(); 
  202.  
  203.   
  204.  
  205.                                             if (logger.isDebugEnabled()) { 
  206.  
  207.                                                     logger.debug("The file " + f.getAbsolutePath() 
  208.  
  209.                                                                       + " is created"); 
  210.  
  211.                                             } 
  212.  
  213.                                    } 
  214.  
  215.                           } 
  216.  
  217.                           zis.close(); 
  218.  
  219.   
  220.  
  221.                  } catch (FileNotFoundException ex) { 
  222.  
  223.   
  224.  
  225.                           if (logger.isErrorEnabled()) { 
  226.  
  227.                                    logger.error("file not found"); 
  228.  
  229.                           } 
  230.  
  231.                           throw new InvalidFolderZipperException(ZIP_UNZIP_ERROR, ex); 
  232.  
  233.   
  234.  
  235.                  } catch (IOException ex) { 
  236.  
  237.   
  238.  
  239.                           if (logger.isErrorEnabled()) { 
  240.  
  241.                                    logger.error("io exception occured"); 
  242.  
  243.                           } 
  244.  
  245.   
  246.  
  247.                           throw new InvalidFolderZipperException(ZIP_UNZIP_ERROR, 
  248.  
  249.                                             ex); 
  250.  
  251.                  } 
  252.  
  253.          } 
  254.  
  255.   
  256.  

 

(1)在47-93行还是对入参进行严格的检查。

(2)第103行打开了一个ZipInputStream,然后在第115行对于这个zip文件里的所有的条目进行遍历。

(3)第119-159行对于条目是一个目录的情况进行了处理,它必须去相应的创建目录结构

(4)第163-215行对于条目是个文件的情况进行了处理,它打开一个文件输出流然后写这个Zip的文件条目到目标目录下相应的位置。

 

 

 

你可能感兴趣的:(java,IO,zip)