Exemple #1
0
  public static void copyJavaToZip(String warFile, String toZip, String javafile, String jarName) {
    ZipFile zipfile = null;
    InputStream isJar = null;
    try {
      isJar = getFileStreamFromZip(warFile, getFileNameFromZIP(new ZipFile(warFile), jarName));
      zipfile = genZipFileFromStream(isJar);

      // java文件中可能会有子类,需要检查,并生成list
      String javaName = javafile.substring(0, javafile.lastIndexOf(".java"));
      List<FileHeader> listJavaFile = searchZipFiles(zipfile, javaName);

      for (FileHeader fileheader : listJavaFile) {
        InputStream isfile = zipfile.getInputStream(fileheader);

        AddStreamToZip(toZip, isfile, jarName + "/" + fileheader.getFileName());
        // "com.foresee.etax.bizfront/com/foresee/etax/bizfront/constant/EtaxBizFrontConstant.class"

        isfile.close();
      }

    } catch (ZipException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (isJar != null) isJar.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
Exemple #2
0
  /**
   * 从ZIP的压缩文件中,搜索指定文件名的文件路径
   *
   * @param zipfile
   * @param filename
   * @return
   * @throws ZipException
   */
  public static String getFileNameFromZIP(ZipFile zipfile, String filename) throws ZipException {
    // Get the list of file headers from the zip file
    @SuppressWarnings("unchecked")
    List<FileHeader> fileHeaderList = zipfile.getFileHeaders();

    for (FileHeader fh : fileHeaderList) {
      if (fh.getFileName().contains(filename)) {

        return fh.getFileName();
      }
    }

    return "";
  }
Exemple #3
0
  public static List<FileHeader> searchZipFiles(ZipFile zipfile, String filename)
      throws ZipException {
    // Get the list of file headers from the zip file
    @SuppressWarnings("unchecked")
    List<FileHeader> fileHeaderList = zipfile.getFileHeaders();
    List<FileHeader> retList = new ArrayList<FileHeader>();
    for (FileHeader fh : fileHeaderList) {

      if (fh.getFileName().contains(filename)) {
        retList.add(fh);
      }
    }

    return retList;
  }
Exemple #4
0
  public static void removeDirFromZipArchive(String file, String removeDir) throws ZipException {
    // 创建ZipFile并设置编码
    ZipFile zipFile = new ZipFile(file);
    zipFile.setFileNameCharset("GBK");

    // 给要删除的目录加上路径分隔符
    if (!removeDir.endsWith(File.separator)) removeDir += File.separator;

    // 如果目录不存在, 直接返回
    FileHeader dirHeader = zipFile.getFileHeader(removeDir);
    if (null == dirHeader) return;

    // 遍历压缩文件中所有的FileHeader, 将指定删除目录下的子文件名保存起来
    @SuppressWarnings("unchecked")
    List<FileHeader> headersList = zipFile.getFileHeaders();
    List<String> removeHeaderNames = new ArrayList<String>();
    for (int i = 0, len = headersList.size(); i < len; i++) {
      FileHeader subHeader = (FileHeader) headersList.get(i);
      if (subHeader.getFileName().startsWith(dirHeader.getFileName())
          && !subHeader.getFileName().equals(dirHeader.getFileName())) {
        removeHeaderNames.add(subHeader.getFileName());
      }
    }
    // 遍历删除指定目录下的所有子文件, 最后删除指定目录(此时已为空目录)
    for (String headerNameString : removeHeaderNames) {
      zipFile.removeFile(headerNameString);
    }
    zipFile.removeFile(dirHeader);
  }
Exemple #5
0
  public void ExtractSelectFilesWithInputStream(
      String zipFileName, String fileName, String destinationPath) {

    ZipInputStream is = null;
    OutputStream os = null;

    try {
      // Initiate the ZipFile
      ZipFile zipFile = new ZipFile(zipFileName);
      // String destinationPath = "c:\\ZipTest";

      // If zip file is password protected then set the password
      if (zipFile.isEncrypted()) {
        zipFile.setPassword("password");
      }

      // Get the FileHeader of the File you want to extract from the
      // zip file. Input for the below method is the name of the file
      // For example: 123.txt or abc/123.txt if the file 123.txt
      // is inside the directory abc
      FileHeader fileHeader = zipFile.getFileHeader(fileName);

      if (fileHeader != null) {

        // Build the output file
        String outFilePath =
            destinationPath + System.getProperty("file.separator") + fileHeader.getFileName();
        File outFile = new File(outFilePath);

        // Checks if the file is a directory
        if (fileHeader.isDirectory()) {
          // This functionality is up to your requirements
          // For now I create the directory
          outFile.mkdirs();
          return;
        }

        // Check if the directories(including parent directories)
        // in the output file path exists
        File parentDir = outFile.getParentFile();
        if (!parentDir.exists()) {
          parentDir.mkdirs(); // If not create those directories
        }

        // Get the InputStream from the ZipFile
        is = zipFile.getInputStream(fileHeader);
        // Initialize the output stream
        os = new FileOutputStream(outFile);

        int readLen = -1;
        byte[] buff = new byte[BUFF_SIZE];

        // Loop until End of File and write the contents to the output
        // stream
        while ((readLen = is.read(buff)) != -1) {
          os.write(buff, 0, readLen);
        }

        // Closing inputstream also checks for CRC of the the just
        // extracted file.
        // If CRC check has to be skipped (for ex: to cancel the unzip
        // operation, etc)
        // use method is.close(boolean skipCRCCheck) and set the flag,
        // skipCRCCheck to false
        // NOTE: It is recommended to close outputStream first because
        // Zip4j throws
        // an exception if CRC check fails
        is.close();

        // Close output stream
        os.close();

        // To restore File attributes (ex: last modified file time,
        // read only flag, etc) of the extracted file, a utility class
        // can be used as shown below
        UnzipUtil.applyFileAttributes(fileHeader, outFile);

        System.out.println("Done extracting: " + fileHeader.getFileName());
      } else {
        System.err.println("FileHeader does not exist-->" + fileName);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }