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,如需转载请自行联系原作者

目录
相关文章
|
1月前
|
存储 缓存 安全
Java 中 IO 流、File文件
Java 中 IO 流、File文件
|
2月前
|
缓存 Linux API
文件IO和标准IO的区别
文件IO和标准IO的区别
22 2
|
3月前
|
存储 Linux API
Linux应用开发基础知识——文件IO操作(三)
Linux应用开发基础知识——文件IO操作(三)
57 2
Linux应用开发基础知识——文件IO操作(三)
|
6月前
|
Linux 测试技术 API
linux系统编程 文件io
linux系统编程 文件io
116 0
|
17天前
|
Java Unix Windows
|
4月前
|
Rust
【一起学Rust · 项目实战】命令行IO项目minigrep——接收命令行参数与读取文件内容
【一起学Rust · 项目实战】命令行IO项目minigrep——接收命令行参数与读取文件内容
59 0
【一起学Rust · 项目实战】命令行IO项目minigrep——接收命令行参数与读取文件内容
|
1月前
|
缓存 Linux
Linux 文件IO简单实例
Linux 文件IO简单实例
14 1
|
1天前
|
Java 开发者
Java一分钟之-Java IO流:文件读写基础
【5月更文挑战第10天】本文介绍了Java IO流在文件读写中的应用,包括`FileInputStream`和`FileOutputStream`用于字节流操作,`BufferedReader`和`PrintWriter`用于字符流。通过代码示例展示了如何读取和写入文件,强调了常见问题如未关闭流、文件路径、编码、权限和异常处理,并提供了追加写入与读取的示例。理解这些基础知识和注意事项能帮助开发者编写更可靠的程序。
8 0
|
10天前
|
C++ 数据格式
【C++】C++中的【文件IO流】使用指南 [手把手代码演示] & [小白秒懂]
【C++】C++中的【文件IO流】使用指南 [手把手代码演示] & [小白秒懂]
【C++】C++中的【文件IO流】使用指南 [手把手代码演示] & [小白秒懂]
|
11天前
|
安全 Go
Golang深入浅出之-Go语言标准库中的文件读写:io/ioutil包
【4月更文挑战第27天】Go语言的`io/ioutil`包提供简单文件读写,适合小文件操作。本文聚焦`ReadFile`和`WriteFile`函数,讨论错误处理、文件权限、大文件处理和编码问题。避免错误的关键在于检查错误、设置合适权限、采用流式读写及处理编码。遵循这些最佳实践能提升代码稳定性。
21 0