paulwong

          解壓ZIP文件

          在pom.xml中加入JAR包
          <dependency>
              <groupId>commons-io</groupId>
              <artifactId>commons-io</artifactId>
          </dependency>

          ZipUtil.java
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.OutputStream;
          import java.nio.charset.Charset;
          import java.util.UUID;
          import java.util.zip.ZipEntry;
          import java.util.zip.ZipInputStream;

          import org.apache.commons.io.IOUtils;
          import org.slf4j.Logger;
          import org.slf4j.LoggerFactory;

          public class ZipUtil {
              
              private static Logger logger = LoggerFactory.getLogger(ZipUtil.class);
              
              public static void extractFolder(InputStream inputStream, String outputFolder) throws IOException 
              {
                  ZipInputStream zis = null;
                  try {

                      Charset GBK = Charset.forName("GBK");
                      zis = new ZipInputStream(inputStream, GBK);
                      ZipEntry entry;

                      while ((entry = zis.getNextEntry()) != null) {

                          // Create a file on HDD in the destinationPath directory
                          
          // destinationPath is a "root" folder, where you want to extract your ZIP file
                          String encoding = System.getProperty("file.encoding");
                          logger.info("encoding:"+encoding); 
                          
                          String fileName = new String(entry.getName().getBytes("GBK"), encoding);
                          File entryFile = new File(outputFolder, fileName);
          //                File entryFile = new File(outputFolder, entry.getName());
                          if (entry.isDirectory()) {

                              if (entryFile.exists()) {
                                  logger.warn("Directory {0} already exists!", entryFile);
                              } else {
                                  entryFile.mkdirs();
                              }

                          } else {

                              // Make sure all folders exists (they should, but the safer, the better ;-))
                              if (entryFile.getParentFile() != null && !entryFile.getParentFile().exists()) {
                                  entryFile.getParentFile().mkdirs();
                              }

                              // Create file on disk
                              if (!entryFile.exists()) {
                                  entryFile.createNewFile();
                              }

                              // and rewrite data from stream
                              OutputStream os = null;
                              try {
                                  os = new FileOutputStream(entryFile);
                                  IOUtils.copy(zis, os);
                              } finally {
          //                        os.close();
                                  IOUtils.closeQuietly(os);
                                  zis.closeEntry();
                              }
                          }
                      }
                  } finally {
                      IOUtils.closeQuietly(zis);
                  }
              }
              
              public static void main(String [] args) throws IOException
              {
                  final String INPUT_ZIP_FILE = "D:TESTING-FILE/ZIP/INPUT/應(yīng)用.zip";
                  String OUTPUT_FOLDER = "D:/TESTING-FILE/ZIP/OUTPUT";
                  
                  OUTPUT_FOLDER += File.separator + UUID.randomUUID().toString();
                  
                  InputStream inputStream = new FileInputStream(new File(INPUT_ZIP_FILE));
                  
                  extractFolder(inputStream, OUTPUT_FOLDER);
                  
                  /*File file = new File(OUTPUT_FOLDER);
                  FileUtil.deleteFolder(file);
          */
              }

          }

          posted on 2014-10-29 17:34 paulwong 閱讀(364) 評(píng)論(0)  編輯  收藏 所屬分類: J2SE

          主站蜘蛛池模板: 乌拉特后旗| 司法| 扎鲁特旗| 宾川县| 横山县| 河曲县| 安吉县| 彭泽县| 汤阴县| 泸西县| 莱阳市| 越西县| 耒阳市| 田阳县| 洛宁县| 光泽县| 仙居县| 交口县| 库车县| 普定县| 枝江市| 固安县| 庆城县| 隆昌县| 霍林郭勒市| 哈巴河县| 新乐市| 织金县| 岢岚县| 淮阳县| 连南| 玉门市| 宁津县| 德保县| 策勒县| 河北省| 靖宇县| 咸丰县| 永顺县| 昭通市| 双牌县|