zoukankan      html  css  js  c++  java
  • JAVA如何解压缩ZIP文档

    代码片段:

      1 package org.yu.units;
      2 
      3 import java.io.Closeable;
      4 import java.io.File;
      5 import java.io.FileInputStream;
      6 import java.io.FileOutputStream;
      7 import java.io.IOException;
      8 import java.io.InputStream;
      9 import java.util.zip.ZipEntry;
     10 import java.util.zip.ZipInputStream;
     11 
     12 
     13 /**
     14  * @author Hai E-mail:256051@qq.com
     15  * @version 创建时间:2017年10月20日 上午10:30:03 类说明
     16  */
     17 /**
     18  * @author HH
     19  *
     20  */
     21 public class zipFile {
     22 
     23     public static void main(String... args) {
     24         extractZipFile("e:\xx\nbproject.zip","e:\xx", true);
     25     }
     26 
     27     public static boolean extractZipFile(String zipFilePath, String path, boolean overwrite) {
     28         return extractZipFile(new File(zipFilePath), path, overwrite);
     29     }
     30 
     31     public static boolean extractZipFile(File zipFilePath, String destDirectory, boolean overwrite) {
     32         InputStream inputStream = null;
     33         ZipInputStream zipInputStream = null;
     34         boolean status = true;
     35 
     36         try {
     37             inputStream = new FileInputStream(zipFilePath);
     38 
     39             zipInputStream = new ZipInputStream(inputStream);
     40             final byte[] data = new byte[1024];
     41 
     42             while (true) {
     43                 ZipEntry zipEntry = null;
     44                 FileOutputStream outputStream = null;
     45 
     46                 try {
     47                     zipEntry = zipInputStream.getNextEntry();
     48 
     49                     if (zipEntry == null) {
     50                         break;
     51                     }
     52 
     53                     final String destination;
     54                     if (destDirectory.endsWith(File.separator)) {
     55                         destination = destDirectory + zipEntry.getName();
     56                     } else {
     57                         destination = destDirectory + File.separator + zipEntry.getName();
     58                     }
     59 
     60                     if (overwrite == false) {
     61                         if (isFileOrDirectoryExist(destination)) {
     62                             continue;
     63                         }
     64                     }
     65 
     66                     if (zipEntry.isDirectory()) {
     67                         createCompleteDirectoryHierarchyIfDoesNotExist(destination);
     68                     } else {
     69                         final File file = new File(destination);
     70                         // Ensure directory is there before we write the file.
     71                         createCompleteDirectoryHierarchyIfDoesNotExist(file.getParentFile());
     72 
     73                         int size = zipInputStream.read(data);
     74 
     75                         if (size > 0) {
     76                             outputStream = new FileOutputStream(destination);
     77 
     78                             do {
     79                                 outputStream.write(data, 0, size);
     80                                 size = zipInputStream.read(data);
     81                             } while (size >= 0);
     82                         }
     83                     }
     84                 } catch (IOException exp) {
     85                     exp.printStackTrace();
     86                     status = false;
     87                     break;
     88                 } finally {
     89                     close(outputStream);
     90                     closeEntry(zipInputStream);
     91                 }
     92 
     93             } // while(true)
     94         } catch (IOException exp) {
     95             exp.printStackTrace();
     96             status = false;
     97         } finally {
     98             close(zipInputStream);
     99             close(inputStream);
    100         }
    101         return status;
    102     }
    103 
    104     public static boolean createCompleteDirectoryHierarchyIfDoesNotExist(String directory) {
    105         return createCompleteDirectoryHierarchyIfDoesNotExist(new File(directory));
    106     }
    107 
    108     private static boolean createCompleteDirectoryHierarchyIfDoesNotExist(File f) {
    109         if (f == null)
    110             return true;
    111 
    112         if (false == createCompleteDirectoryHierarchyIfDoesNotExist(f.getParentFile())) {
    113             return false;
    114         }
    115 
    116         final String path = f.getAbsolutePath();
    117 
    118         return createDirectoryIfDoesNotExist(path);
    119     }
    120 
    121     private static boolean createDirectoryIfDoesNotExist(String directory) {
    122         java.io.File f = new java.io.File(directory);
    123 
    124         if (f.exists() == false) {
    125             if (f.mkdir()) {
    126                 return true;
    127             } else {
    128                 return false;
    129             }
    130         }
    131 
    132         return true;
    133     }
    134 
    135     /**
    136     * Performs close operation on Closeable stream, without the need of
    137     * writing cumbersome try...catch block.
    138     *
    139     * @param closeable The closeable stream.
    140     */
    141     public static void close(Closeable closeable) {
    142         // Instead of returning boolean, we will just simply swallow any
    143         // exception silently. This is because this method will usually be
    144         // invoked within finally block. If we are having control statement
    145         // (return, break, continue) within finally block, a lot of surprise may
    146         // happen.
    147         // http://stackoverflow.com/questions/48088/returning-from-a-finally-block-in-java
    148         if (null != closeable) {
    149             try {
    150                 closeable.close();
    151             } catch (IOException ex) {
    152                 ex.printStackTrace();
    153             }
    154         }
    155     }
    156     
    157        /**
    158      * Performs close operation on ZIP input stream, without the need of
    159      * writing cumbersome try...catch block.
    160      *
    161      * @param zipInputStream The ZIP input stream.
    162      */
    163     public static void closeEntry(ZipInputStream zipInputStream) {
    164         // Instead of returning boolean, we will just simply swallow any
    165         // exception silently. This is because this method will usually be
    166         // invoked within finally block. If we are having control statement
    167         // (return, break, continue) within finally block, a lot of surprise may
    168         // happen.
    169         // http://stackoverflow.com/questions/48088/returning-from-a-finally-block-in-java
    170         if (null != zipInputStream) {
    171             try {
    172                 zipInputStream.closeEntry();
    173             } catch (IOException ex) {
    174                 ex.printStackTrace();
    175             }
    176         }
    177     }
    178     
    179     public static boolean isFileOrDirectoryExist(String fileOrDirectory) {
    180         java.io.File f = new java.io.File(fileOrDirectory);
    181         return f.exists();
    182     }
    183 }
  • 相关阅读:
    .net core ELK
    mongodb基础
    .net core FluentValidation
    使用Identity Server 4建立Authorization Server
    .net core JWT应用
    .net core action过滤器的普通应用
    matplotlib
    python-13:文件操作 之二
    python-13:文件操作 open
    python-12:内置 函数之一 filter map sorted reduce
  • 原文地址:https://www.cnblogs.com/CnKker/p/javazip.html
Copyright © 2011-2022 走看看