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的文件条目到目标目录下相应的位置。

 





本文转自 charles_wang888 51CTO博客,原文链接:http://blog.51cto.com/supercharles888/980320,如需转载请自行联系原作者

目录
相关文章
|
13天前
|
Java 大数据 API
Java 流(Stream)、文件(File)和IO的区别
Java中的流(Stream)、文件(File)和输入/输出(I/O)是处理数据的关键概念。`File`类用于基本文件操作,如创建、删除和检查文件;流则提供了数据读写的抽象机制,适用于文件、内存和网络等多种数据源;I/O涵盖更广泛的输入输出操作,包括文件I/O、网络通信等,并支持异常处理和缓冲等功能。实际开发中,这三者常结合使用,以实现高效的数据处理。例如,`File`用于管理文件路径,`Stream`用于读写数据,I/O则处理复杂的输入输出需求。
|
1月前
|
Linux C语言
C语言 文件IO (系统调用)
本文介绍了Linux系统调用中的文件I/O操作,包括文件描述符、`open`、`read`、`write`、`lseek`、`close`、`dup`、`dup2`等函数,以及如何获取文件属性信息(`stat`)、用户信息(`getpwuid`)和组信息(`getgrgid`)。此外还介绍了目录操作函数如`opendir`、`readdir`、`rewinddir`和`closedir`,并提供了相关示例代码。系统调用直接与内核交互,没有缓冲机制,效率相对较低,但实时性更高。
|
2月前
|
存储 监控 Linux
性能分析之从 IO 高定位到具体文件
【8月更文挑战第21天】性能分析之从 IO 高定位到具体文件
30 0
性能分析之从 IO 高定位到具体文件
|
2月前
IO流拷贝文件的几种方式
IO流拷贝文件的几种方式
29 1
|
3月前
|
Linux 数据处理 C语言
【Linux】基础IO----系统文件IO & 文件描述符fd & 重定向(下)
【Linux】基础IO----系统文件IO & 文件描述符fd & 重定向(下)
59 0
|
3月前
|
Linux C语言 C++
【Linux】基础IO----系统文件IO & 文件描述符fd & 重定向(上)
【Linux】基础IO----系统文件IO & 文件描述符fd & 重定向(上)
39 0
|
4月前
|
C++
Open3D File Io 文件IO
Open3D File Io 文件IO
|
4月前
|
存储 缓存 Unix
【嵌入式软件工程师面经】Linux文件IO
【嵌入式软件工程师面经】Linux文件IO
41 1
|
4月前
|
存储 安全 Unix
【.Net Core】深入理解IO之文件和目录
【.Net Core】深入理解IO之文件和目录
45 4
|
4月前
|
Java
io读两个文件,生成list 排重后写本地文件(Java)
io读两个文件,生成list 排重后写本地文件(Java)
28 2