Example #1
0
 /**
  * 解压缩功能. 将zipFilePath解压到saveDir目录
  *
  * @throws Exception
  */
 public static void upZipFile(String zipFilePath, String saveDir) throws Exception {
   ZipFile zfile = null;
   try {
     zfile = new ZipFile(zipFilePath);
     Enumeration zList = zfile.getEntries();
     ZipEntry ze = null;
     byte[] buf = new byte[1024];
     while (zList.hasMoreElements()) {
       ze = (ZipEntry) zList.nextElement();
       if (ze.isDirectory()) {
         File f = new File(saveDir + File.separatorChar + ze.getName());
         f.mkdir();
         continue;
       }
       OutputStream os = null;
       InputStream is = null;
       try {
         os =
             new BufferedOutputStream(
                 new FileOutputStream(
                     getRealFileName(saveDir + File.separatorChar, ze.getName())));
         is = new BufferedInputStream(zfile.getInputStream(ze));
         int readLen = 0;
         while ((readLen = is.read(buf, 0, 1024)) != -1) {
           os.write(buf, 0, readLen);
         }
       } finally {
         is.close();
         os.close();
       }
     }
   } finally {
     zfile.close();
   }
 }
Example #2
0
 /**
  * 解压缩ZIP文件,将ZIP文件里的内容解压到descFileName目录下
  *
  * @param zipFileName 需要解压的ZIP文件
  * @param descFileName 目标文件
  */
 public static boolean unZipFiles(String zipFileName, String descFileName) {
   String descFileNames = descFileName;
   if (!descFileNames.endsWith(File.separator)) {
     descFileNames = descFileNames + File.separator;
   }
   try {
     // 根据ZIP文件创建ZipFile对象
     ZipFile zipFile = new ZipFile(zipFileName);
     ZipEntry entry = null;
     String entryName = null;
     String descFileDir = null;
     byte[] buf = new byte[4096];
     int readByte = 0;
     // 获取ZIP文件里所有的entry
     @SuppressWarnings("rawtypes")
     Enumeration enums = zipFile.getEntries();
     // 遍历所有entry
     while (enums.hasMoreElements()) {
       entry = (ZipEntry) enums.nextElement();
       // 获得entry的名字
       entryName = entry.getName();
       descFileDir = descFileNames + entryName;
       if (entry.isDirectory()) {
         // 如果entry是一个目录,则创建目录
         new File(descFileDir).mkdirs();
         continue;
       } else {
         // 如果entry是一个文件,则创建父目录
         new File(descFileDir).getParentFile().mkdirs();
       }
       File file = new File(descFileDir);
       // 打开文件输出流
       OutputStream os = new FileOutputStream(file);
       // 从ZipFile对象中打开entry的输入流
       InputStream is = zipFile.getInputStream(entry);
       while ((readByte = is.read(buf)) != -1) {
         os.write(buf, 0, readByte);
       }
       os.close();
       is.close();
     }
     zipFile.close();
     log.debug("文件解压成功!");
     return true;
   } catch (Exception e) {
     log.debug("文件解压失败:" + e.getMessage());
     return false;
   }
 }
  private void handleZipFile(File f, File uploadDir) throws IOException {
    ZipFile zipFile = new ZipFile(f, "UTF-8", true);
    Enumeration entries = zipFile.getEntries();

    while (entries.hasMoreElements()) {
      ZipEntry entry = (ZipEntry) entries.nextElement();

      InputStream zipin = zipFile.getInputStream(entry);
      File dest = new File(uploadDir, entry.getName());
      OutputStream fileout = new FileOutputStream(dest);
      StreamUtils.copyThenClose(zipin, fileout);

      viewer.add(new UploadItem(zipin, "", dest.getName(), ""));
      if (dest.getName().endsWith("shp")) {
        list.add(dest.toURI().toURL());
      }
    }
    f.delete();
  }
  public void putData(File file) throws SQLException {
    System.out.println(file.getAbsolutePath());
    try {
      ZipFile zipFile;
      try {
        zipFile = new ZipFile(file, "euc-kr");
      } catch (java.nio.charset.UnmappableCharacterException e) { // IOException e) {
        zipFile = new ZipFile(file);
      }

      // create a directory named the same as the zip file in the
      // same directory as the zip file.
      File zipDir = new File(file.getParentFile(), "ZipFile");
      zipDir.mkdir();

      @SuppressWarnings("unchecked")
      Enumeration<? extends ZipEntry> entries = zipFile.getEntries();
      while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        String name = entry.getName();
        // File for current file or directory
        File entryDestination = new File(zipDir, name);

        //				System.out.println(entryDestination);
        // This file may be in a subfolder in the Zip bundle
        // This line ensures the parent folders are all
        // created.
        entryDestination.getParentFile().mkdirs();

        // Directories are included as seperate entries
        // in the zip file.
        if (!entry.isDirectory()) {
          System.out.println(entry.getName() + ",size = " + entry.getSize());
          generateFile(entryDestination, entry, zipFile);
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Example #5
0
 /**
  * zip解压缩
  *
  * @param zipfile File 需要解压缩的文件
  * @param descDir String 解压后的目标目录
  */
 public static void unZipFiles(java.io.File zipfile, String descDir) {
   try {
     ZipFile zf = new ZipFile(zipfile);
     for (Enumeration entries = zf.getEntries(); entries.hasMoreElements(); ) {
       ZipEntry entry = ((ZipEntry) entries.nextElement());
       String zipEntryName = entry.getName();
       InputStream in = zf.getInputStream(entry);
       OutputStream out = new FileOutputStream(descDir + zipEntryName);
       byte[] buf1 = new byte[1024];
       int len;
       while ((len = in.read(buf1)) > 0) {
         out.write(buf1, 0, len);
       }
       in.close();
       out.close();
       // System.out.println("解压缩完成.");
     }
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Example #6
0
  /**
   * @param archive 解压文件名
   * @param decompressDir
   * @throws IOException
   * @throws FileNotFoundException
   * @throws ZipException
   */
  public static void unZipFile(String archive, String decompressDir)
      throws IOException, FileNotFoundException, ZipException {
    BufferedInputStream bi;

    ZipFile zf = new ZipFile(archive, "UTF-8");
    Enumeration e = zf.getEntries();

    while (e.hasMoreElements()) {
      ZipEntry ze2 = (ZipEntry) e.nextElement();
      String entryName = ze2.getName();
      String path = decompressDir + "/" + entryName;
      if (ze2.isDirectory()) {
        System.out.println("正在创建解压目录 - " + entryName);
        File decompressDirFile = new File(path);
        if (!decompressDirFile.exists()) {
          decompressDirFile.mkdirs();
        }
      } else {
        System.out.println("正在创建解压文件 - " + entryName);
        String fileDir = path.substring(0, path.lastIndexOf("/"));
        File fileDirFile = new File(fileDir);
        if (!fileDirFile.exists()) {
          fileDirFile.mkdirs();
        }
        BufferedOutputStream bos =
            new BufferedOutputStream(new FileOutputStream(decompressDir + "/" + entryName));
        bi = new BufferedInputStream(zf.getInputStream(ze2));
        byte[] readContent = new byte[1024];
        int readCount = bi.read(readContent);
        while (readCount != -1) {
          bos.write(readContent, 0, readCount);
          readCount = bi.read(readContent);
        }
        bos.close();
      }
    }

    zf.close();
  }
Example #7
0
  /**
   * 解压文件名包含传入文字的文件
   *
   * @param zipFile 压缩文件
   * @param folderPath 目标文件夹
   * @param nameContains 传入的文件匹配名
   * @throws ZipException 压缩格式有误时抛出
   * @throws IOException IO错误时抛出
   */
  public static ArrayList<File> upZipSelectedFile(
      File zipFile, String folderPath, String nameContains) throws ZipException, IOException {
    ArrayList<File> fileList = new ArrayList<File>();

    File desDir = new File(folderPath);
    if (!desDir.exists()) {
      desDir.mkdirs();
    }

    ZipFile zf = new ZipFile(zipFile);
    for (Enumeration<?> entries = zf.getEntries(); entries.hasMoreElements(); ) {
      ZipEntry entry = ((ZipEntry) entries.nextElement());
      if (entry.getName().contains(nameContains)) {
        InputStream in = zf.getInputStream(entry);
        String str = folderPath + File.separator + entry.getName();
        str = new String(str.getBytes("8859_1"), "GB2312");
        // str.getBytes("GB2312"),"8859_1" 输出
        // str.getBytes("8859_1"),"GB2312" 输入
        File desFile = new File(str);
        if (!desFile.exists()) {
          File fileParentDir = desFile.getParentFile();
          if (!fileParentDir.exists()) {
            fileParentDir.mkdirs();
          }
          desFile.createNewFile();
        }
        OutputStream out = new FileOutputStream(desFile);
        byte buffer[] = new byte[BUFF_SIZE];
        int realLength;
        while ((realLength = in.read(buffer)) > 0) {
          out.write(buffer, 0, realLength);
        }
        in.close();
        out.close();
        fileList.add(desFile);
      }
    }
    return fileList;
  }
Example #8
0
 /**
  * 解压缩一个文件
  *
  * @param zipFile 压缩文件
  * @param folderPath 解压缩的目标目录
  * @throws IOException 当解压缩过程出错时抛出
  */
 public static void upZipFile(File zipFile, String folderPath) throws ZipException, IOException {
   File desDir = new File(folderPath);
   if (!desDir.exists()) {
     desDir.mkdirs();
   }
   ZipFile zf = new ZipFile(zipFile, "GBK");
   for (Enumeration<?> entries = zf.getEntries(); entries.hasMoreElements(); ) {
     ZipEntry entry = ((ZipEntry) entries.nextElement());
     InputStream in = zf.getInputStream(entry);
     String str = folderPath + File.separator + entry.getName();
     // str = new String(str.getBytes("8859_1"), "GB2312");
     File desFile = new File(str);
     if (!desFile.exists()) {
       if (!entry.isDirectory()) {
         File fileParentDir = desFile.getParentFile();
         if (!fileParentDir.exists()) {
           fileParentDir.mkdirs();
         }
         desFile.createNewFile();
       } else {
         desFile.mkdirs();
         continue;
       }
     }
     if (!entry.isDirectory()) {
       OutputStream out = new FileOutputStream(desFile);
       byte buffer[] = new byte[BUFF_SIZE];
       int realLength;
       while ((realLength = in.read(buffer)) > 0) {
         out.write(buffer, 0, realLength);
       }
       in.close();
       out.close();
     }
   }
 }
Example #9
0
 /**
  * 获得压缩文件内压缩文件对象以取得其属性
  *
  * @param zipFile 压缩文件
  * @return 返回一个压缩文件列表
  * @throws ZipException 压缩文件格式有误时抛出
  * @throws IOException IO操作有误时抛出
  */
 public static Enumeration<?> getEntriesEnumeration(File zipFile)
     throws ZipException, IOException {
   ZipFile zf = new ZipFile(zipFile);
   return zf.getEntries();
 }