/**
  * Get an entry from the ZIP, trying both types of slashes.
  *
  * @param file
  * @return
  */
 private ZipEntry getEntry(String file) {
   ZipEntry entry = zip.getEntry(file);
   if (entry != null) {
     return entry;
   }
   return zip.getEntry(file.replace("/", "\\"));
 }
Example #2
0
  public List<File> unpack(File archiveFile, File targetDir) throws ArchiveException {
    // 首先判断下对应的目标文件是否存在,如存在则执行删除
    if (false == archiveFile.exists()) {
      throw new ArchiveException(String.format("[%s] not exist", archiveFile.getAbsolutePath()));
    }
    if (false == targetDir.exists() && false == NioUtils.create(targetDir, false, 3)) {
      throw new ArchiveException(
          String.format("[%s] not exist and create failed", targetDir.getAbsolutePath()));
    }

    List<File> result = new ArrayList<File>();
    ZipFile zipFile = null;
    try {
      zipFile = new ZipFile(archiveFile);
      Enumeration entries = zipFile.entries();

      while (entries.hasMoreElements()) {
        // entry
        ZipEntry entry = (ZipEntry) entries.nextElement();
        String entryName = entry.getName();
        // target
        File targetFile = new File(targetDir, entryName);
        NioUtils.create(targetFile.getParentFile(), false, 3); // 尝试创建父路径
        InputStream input = null;
        OutputStream output = null;
        try {
          output = new FileOutputStream(targetFile);
          input = zipFile.getInputStream(entry);
          NioUtils.copy(input, output);
        } finally {
          IOUtils.closeQuietly(input);
          IOUtils.closeQuietly(output);
        }
        result.add(targetFile);
      }

    } catch (Exception e) {
      throw new ArchiveException(e);
    } finally {
      if (zipFile != null) {
        try {
          zipFile.close();
        } catch (IOException ex) {
        }
      }
    }

    return result;
  }
  /**
   * Get the input stream for a chunk file.
   *
   * @param name
   * @return
   * @throws IOException
   * @throws DataException
   */
  @Override
  @SuppressWarnings("unchecked")
  protected InputStream getInputStream(String name, String worldname)
      throws IOException, DataException {

    // Detect subfolder for the world's files
    if (folder != null) {
      if (!folder.equals("")) {
        name = folder + "/" + name;
      }
    } else {
      Pattern pattern = Pattern.compile(".*\\.mcr$");
      // World pattern
      Pattern worldPattern = Pattern.compile(worldname + "\\$");
      for (Enumeration<? extends ZipEntry> e = zip.entries(); e.hasMoreElements(); ) {
        ZipEntry testEntry = (ZipEntry) e.nextElement();
        // Check for world
        if (worldPattern.matcher(worldname).matches()) {
          // Check for file
          if (pattern.matcher(testEntry.getName()).matches()) {
            folder = testEntry.getName().substring(0, testEntry.getName().lastIndexOf("/"));
            name = folder + "/" + name;
            break;
          }
        }
      }

      // Check if world is found
      if (folder == null) {
        throw new MissingWorldException("Target world is not present in ZIP.", worldname);
      }
    }

    ZipEntry entry = getEntry(name);
    if (entry == null) {
      throw new MissingChunkException();
    }
    try {
      return zip.getInputStream(entry);
    } catch (ZipException e) {
      throw new IOException("Failed to read " + name + " in ZIP");
    }
  }
  @Override
  @SuppressWarnings("unchecked")
  public boolean isValid() {
    for (Enumeration<? extends ZipEntry> e = zip.entries(); e.hasMoreElements(); ) {

      ZipEntry testEntry = e.nextElement();

      if (testEntry.getName().matches(".*\\.mcr$")) {
        return true;
      }
    }

    return false;
  }
 /**
  * Close resources.
  *
  * @throws IOException
  */
 @Override
 public void close() throws IOException {
   zip.close();
 }