IO Foundation 6-解压缩zip文件

作者:佚名 上传时间:2019-05-28 版权申诉

需求:

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

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

实现:


  
  1. /**
  2. * This class will handle the folder zipper related operations
  3. *
  4. * @author cwang58
  5. * @created date: Aug 4, 2012
  6. */
  7. public class FolderZipper implements IFolderZipper {
  8. private static Logger logger = LoggerFactory.getLogger(FolderZipper. class );
  9. /**
  10. * to unzip a zip file ,it will read all the entries in the zip file if the
  11. * entry is a folder ,then it will created a folder structure (including the
  12. * parent folder structure) if the entry is a file ,then it use file output
  13. * stream to output this file
  14. *
  15. * @param zipFileName
  16. *            the zip file name
  17. * @param destPath
  18. *            the path that we want to zip to
  19. */
  20. public void unzip(String zipFileName, String destPath) throws Exception {
  21. // verify the parameters
  22. if (zipFileName == null || zipFileName.trim().equals(EMPTY_STRING)) {
  23. if (logger.isErrorEnabled()) {
  24. logger.error( "zipFileName parameter can't be null" );
  25. }
  26. throw new ParameterNullException(ZIP_UNZIP_ERROR);
  27. }
  28. if (destPath == null ) {
  29. if (logger.isErrorEnabled()) {
  30. logger.error( "destPath parameter can't be null" );
  31. }
  32. throw new ParameterNullException( "ZIP_UNZIP_ERROR" );
  33. }
  34. // verify the zip file 's file extension (only support ZIP)
  35. if (!zipFileName.trim().endsWith(ZIP_EXTENSION)) {
  36. if (logger.isErrorEnabled()) {
  37. logger.error( "the zip file MUST BE end with zip suffix" );
  38. }
  39. throw new ParameterInvalidException(ZIP_UNZIP_ERROR);
  40. }
  41. try {
  42. FileInputStream fis = new FileInputStream(zipFileName);
  43. ZipInputStream zis = new ZipInputStream(fis);
  44. ZipEntry ze = null ;
  45. if (destPath.equals(DOT))
  46. destPath = EMPTY_STRING;
  47. while ((ze = zis.getNextEntry()) != null ) {
  48. // if the zipEntry is a folder
  49. // then must create the folder structure
  50. if (ze.isDirectory()) {
  51. if (logger.isDebugEnabled()) {
  52. logger.debug( "Processing folder:" + ze.getName());
  53. }
  54. // construct the dirName by string concating the destpath
  55. // and the foldername
  56. String dirName = destPath + ze.getName();
  57. // creating the folder structure
  58. File f = new File(dirName);
  59. f.mkdirs();
  60. if (logger.isDebugEnabled()) {
  61. logger.debug( "The folder " + f.getAbsolutePath()
  62. + " is created" );
  63. }
  64. }
  65. // if the zipEntry file is a file
  66. // then use a FileOutputStream to write this file
  67. else {
  68. if (logger.isDebugEnabled()) {
  69. logger.debug( "Processing file: " + ze.getName());
  70. }
  71. // construct the fileName by string concating the destpath
  72. // and the foldername
  73. String fileName = destPath + ze.getName();
  74. File f = new File(fileName);
  75. // open a FileOutputStream to write this file
  76. FileOutputStream fos = new FileOutputStream(f);
  77. int tmp = - 1 ;
  78. while ((tmp = zis.read()) != - 1 ) {
  79. fos.write(tmp);
  80. }
  81. zis.closeEntry();
  82. fos.close();
  83. if (logger.isDebugEnabled()) {
  84. logger.debug( "The file " + f.getAbsolutePath()
  85. + " is created" );
  86. }
  87. }
  88. }
  89. zis.close();
  90. } catch (FileNotFoundException ex) {
  91. if (logger.isErrorEnabled()) {
  92. logger.error( "file not found" );
  93. }
  94. throw new InvalidFolderZipperException(ZIP_UNZIP_ERROR, ex);
  95. } catch (IOException ex) {
  96. if (logger.isErrorEnabled()) {
  97. logger.error( "io exception occured" );
  98. }
  99. throw new InvalidFolderZipperException(ZIP_UNZIP_ERROR,
  100. ex);
  101. }
  102. }
  103. }

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

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

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

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





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

免责申明:文章和图片全部来源于公开网络,如有侵权,请通知删除 server@dude6.com

用户评论
相关推荐