コード例 #1
0
ファイル: Zip4jUtils.java プロジェクト: cocoweb/svndeploy
  public static int ZipCopyFile2Zip(
      ZipFile fromZipFile, String fromFile, ZipFile toZipFile, String toFile) {
    int retint = 0;
    ZipInputStream is = null;

    try {

      // 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 = fromZipFile.getFileHeader(fromFile);

      if (fileHeader != null) {

        // Get the InputStream from the ZipFile
        is = fromZipFile.getInputStream(fileHeader);

        AddStreamToZip(toZipFile, is, toFile);

        retint++;

      } else {
        System.err.println("   !!未找到:" + fromFile + "@" + fromZipFile.getFile().getName());
        retint = -1;
      }
    } catch (Exception e) {
      e.printStackTrace();
      retint = -1;
    } finally {
      if (is != null) {
        try {
          is.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return retint;
  }
コード例 #2
0
ファイル: Zip4jUtils.java プロジェクト: cocoweb/svndeploy
  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();
    }
  }