Beispiel #1
0
 private static void chmod(Map<File, Map<Integer, List<String>>> chmodMap) throws IOException {
   for (Map.Entry<File, Map<Integer, List<String>>> entry : chmodMap.entrySet())
     for (Map.Entry<Integer, List<String>> dirEntry : entry.getValue().entrySet())
       for (List<String> files : splitList(dirEntry.getValue(), MAX_FILES_PER_COMMAND))
         OsUtil.chmod(
             entry.getKey(),
             dirEntry.getKey().intValue(),
             files.toArray(new String[files.size()]));
 }
Beispiel #2
0
  /**
   * Unpack the content read from <i>source</i> into <i>targetFolder</i>. If the
   * <i>skipTopFolder</i> is set, then don't assume that the archive contains one single folder and
   * unpack the content of that folder, not including the folder itself.
   *
   * @param source The input source. Must be in <i>TAR</i> format.
   * @param targetFolder The destination folder for the unpack. Not used when a <tt>fileCatcher</tt>
   *     is provided
   * @param skipTopFolder Set to <code>true</code> to unpack beneath the top folder of the archive.
   *     The archive must consist of one single folder and nothing else in order for this to work.
   * @param fileCatcher Used when specific files should be picked from the archive without writing
   *     them to disk. Can be <tt>null</tt>.
   * @throws IOException
   */
  public static void unpack(
      InputStream source, File targetFolder, boolean skipTopFolder, FileCatcher fileCatcher)
      throws IOException {
    String topFolderName = null;
    Map<File, Map<Integer, List<String>>> chmodMap =
        new HashMap<File, Map<Integer, List<String>>>();
    TarArchiveInputStream in = new TarArchiveInputStream(source);
    try {
      TarArchiveEntry te = in.getNextTarEntry();
      if (te == null) {
        throw new IOException("No entry in the tar file");
      }
      do {
        if (te.isGlobalPaxHeader()) continue;

        String name = te.getName();
        if (name.startsWith("./._"))
          // MacOS specific extended attributes addition. Just skip it
          continue;

        if (skipTopFolder) {
          int firstSlash = name.indexOf('/');
          if (firstSlash < 0) throw new IOException("Archive doesn't contain one single folder");

          String tfName = name.substring(0, firstSlash);
          if (topFolderName == null) topFolderName = tfName;
          else if (!tfName.equals(topFolderName))
            throw new IOException("Archive doesn't contain one single folder");
          name = name.substring(firstSlash + 1);
        }
        if (name.length() == 0) continue;

        String linkName = te.getLinkName();
        if (linkName != null) {
          if (linkName.trim().equals("")) linkName = null;
        }

        if (fileCatcher != null) {
          if (linkName == null && !te.isDirectory() && fileCatcher.accept(name)) {
            if (fileCatcher.catchData(name, in))
              // We're done here
              return;
          }
          continue;
        }

        File outFile = new File(targetFolder, name);
        if (linkName != null) {
          if (!OsUtil.link(targetFolder, name, te.getLinkName()))
            throw new IOException(
                "Archive contains links but they are not supported on this platform");
        } else {
          if (te.isDirectory()) {
            outFile.mkdirs();
          } else {
            outFile.getParentFile().mkdirs();
            OutputStream target = new FileOutputStream(outFile);
            StreamUtil.copy(in, target);
            target.close();
            outFile.setLastModified(te.getModTime().getTime());
          }
          registerChmodFile(chmodMap, targetFolder, Integer.valueOf(te.getMode()), name);
        }
      } while ((te = in.getNextTarEntry()) != null);
    } finally {
      StreamUtil.close(in);
    }
    chmod(chmodMap);
  }