Beispiel #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();
   }
 }
Beispiel #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 static void generateFile(File destination, ZipEntry entry, ZipFile owner)
      throws IOException {
    InputStream in = null;
    OutputStream out = null;
    try {
      InputStream rawIn = owner.getInputStream(entry);
      in = new BufferedInputStream(rawIn);

      FileOutputStream rawOut = new FileOutputStream(destination);
      out = new BufferedOutputStream(rawOut);

      // pump data from zip file into new files
      byte[] buf = new byte[1024];
      int len;
      while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
      }
    } finally {
      if (in != null) {
        in.close();
      }
      if (out != null) {
        out.close();
      }
    }
  }
 public static void closeQuietly(final ZipFile zipfile) {
   if (zipfile != null) {
     try {
       zipfile.close();
     } catch (IOException ex) {
     }
   }
 }
  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();
    }
  }
Beispiel #7
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();
   }
 }
Beispiel #8
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();
  }
Beispiel #9
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;
  }
Beispiel #10
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();
     }
   }
 }
  /**
   * Parses the project file, configuring the project as it goes.
   *
   * @param project the current project
   * @param source the xml source
   * @param handler the root handler to use (contains the current context)
   * @exception BuildException if the configuration is invalid or cannot be read
   */
  public void parse(Project project, Object source, RootHandler handler) throws BuildException {

    AntXMLContext context = handler.context;

    File buildFile = null;
    URL url = null;
    String buildFileName = null;

    if (source instanceof File) {
      buildFile = (File) source;
    } else if (source instanceof URL) {
      url = (URL) source;
    } else if (source instanceof Resource) {
      FileProvider fp = (FileProvider) ((Resource) source).as(FileProvider.class);
      if (fp != null) {
        buildFile = fp.getFile();
      } else {
        URLProvider up = (URLProvider) ((Resource) source).as(URLProvider.class);
        if (up != null) {
          url = up.getURL();
        }
      }
    }
    if (buildFile != null) {
      buildFile = FILE_UTILS.normalize(buildFile.getAbsolutePath());
      context.setBuildFile(buildFile);
      buildFileName = buildFile.toString();
    } else if (url != null) {
      try {
        context.setBuildFile((File) null);
        context.setBuildFile(url);
      } catch (java.net.MalformedURLException ex) {
        throw new BuildException(ex);
      }
      buildFileName = url.toString();
    } else {
      throw new BuildException(
          "Source " + source.getClass().getName() + " not supported by this plugin");
    }
    InputStream inputStream = null;
    InputSource inputSource = null;
    ZipFile zf = null;

    try {
      /** SAX 2 style parser used to parse the given file. */
      XMLReader parser = JAXPUtils.getNamespaceXMLReader();

      String uri = null;
      if (buildFile != null) {
        uri = FILE_UTILS.toURI(buildFile.getAbsolutePath());
        inputStream = new FileInputStream(buildFile);
      } else {
        uri = url.toString();
        int pling = -1;
        if (uri.startsWith("jar:file") && (pling = uri.indexOf("!/")) > -1) {
          zf = new ZipFile(org.apache.tools.ant.launch.Locator.fromJarURI(uri), "UTF-8");
          inputStream = zf.getInputStream(zf.getEntry(uri.substring(pling + 1)));
        } else {
          inputStream = url.openStream();
        }
      }

      inputSource = new InputSource(inputStream);
      if (uri != null) {
        inputSource.setSystemId(uri);
      }
      project.log(
          "parsing buildfile "
              + buildFileName
              + " with URI = "
              + uri
              + (zf != null ? " from a zip file" : ""),
          Project.MSG_VERBOSE);

      DefaultHandler hb = handler;

      parser.setContentHandler(hb);
      parser.setEntityResolver(hb);
      parser.setErrorHandler(hb);
      parser.setDTDHandler(hb);
      parser.parse(inputSource);
    } catch (SAXParseException exc) {
      Location location =
          new Location(exc.getSystemId(), exc.getLineNumber(), exc.getColumnNumber());

      Throwable t = exc.getException();
      if (t instanceof BuildException) {
        BuildException be = (BuildException) t;
        if (be.getLocation() == Location.UNKNOWN_LOCATION) {
          be.setLocation(location);
        }
        throw be;
      }
      throw new BuildException(exc.getMessage(), t == null ? exc : t, location);
    } catch (SAXException exc) {
      Throwable t = exc.getException();
      if (t instanceof BuildException) {
        throw (BuildException) t;
      }
      throw new BuildException(exc.getMessage(), t == null ? exc : t);
    } catch (FileNotFoundException exc) {
      throw new BuildException(exc);
    } catch (UnsupportedEncodingException exc) {
      throw new BuildException("Encoding of project file " + buildFileName + " is invalid.", exc);
    } catch (IOException exc) {
      throw new BuildException(
          "Error reading project file " + buildFileName + ": " + exc.getMessage(), exc);
    } finally {
      FileUtils.close(inputStream);
      ZipFile.closeQuietly(zf);
    }
  }
Beispiel #12
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();
 }