Explode source JAR files into a target directory

@param jarFiles
list of source files
@param destDir
target directory name (should already exist)
@exception IOException
error creating a target file
503766
public void processJars(File[] jarFiles, File destDir) throws IOException {
        for (int i = 0; i < jarFiles.length; i++) {
            processFile(jarFiles[i], destDir);
        }
    }
425959
public static void splitFile(String filePath, long[] splitPoints, String storageFolder) throws IOException {
        splitFile(new File(filePath), splitPoints, storageFolder);
    }
671784
public static void zip(File[] sources, File target) throws IOException {
		zip(sources, target, null);
	}
897496
public static void copyFiles(String[] files, String storageFolder) throws IOException {
        copyFiles(getFiles(files), storageFolder);
    }
189132
@SuppressWarnings("deprecation")
  private void addJars(Path jarFileDir, String jarFileList, Configuration conf) throws IOException {
    LocalFileSystem lfs = FileSystem.getLocal(conf);
    for (String jarFile : SPLITTER.split(jarFileList)) {
      Path srcJarFile = new Path(jarFile);
      FileStatus[] fileStatusList = lfs.globStatus(srcJarFile);

      for (FileStatus status : fileStatusList) {
        // For each FileStatus there are chances it could fail in copying at the first attempt, due to file-existence
        // or file-copy is ongoing by other job instance since all Gobblin jobs share the same jar file directory.
        // the retryCount is to avoid cases (if any) where retry is going too far and causes job hanging.
        int retryCount = 0;
        boolean shouldFileBeAddedIntoDC = true;
        Path destJarFile = calculateDestJarFile(status, jarFileDir);
        // Adding destJarFile into HDFS until it exists and the size of file on targetPath matches the one on local path.
        while (!this.fs.exists(destJarFile) || fs.getFileStatus(destJarFile).getLen() != status.getLen()) {
          try {
            if (this.fs.exists(destJarFile) && fs.getFileStatus(destJarFile).getLen() != status.getLen()) {
              Thread.sleep(WAITING_TIME_ON_IMCOMPLETE_UPLOAD);
              throw new IOException("Waiting for file to complete on uploading ... ");
            }
            // Set the first parameter as false for not deleting sourceFile
            // Set the second parameter as false for not overwriting existing file on the target, by default it is true.
            // If the file is preExisted but overwrite flag set to false, then an IOException if thrown.
            this.fs.copyFromLocalFile(false, false, status.getPath(), destJarFile);
          } catch (IOException | InterruptedException e) {
            LOG.warn("Path:" + destJarFile + " is not copied successfully. Will require retry.");
            retryCount += 1;
            if (retryCount >= this.jarFileMaximumRetry) {
              LOG.error("The jar file:" + destJarFile + "failed in being copied into hdfs", e);
              // If retry reaches upper limit, skip copying this file.
              shouldFileBeAddedIntoDC = false;
              break;
            }
          }
        }
        if (shouldFileBeAddedIntoDC) {
          // Then add the jar file on HDFS to the classpath
          LOG.info(String.format("Adding %s to classpath", destJarFile));
          DistributedCache.addFileToClassPath(destJarFile, conf, this.fs);
        }
      }
    }
  }
380859
def Jar(env, target = None, source = [], *args, **kw):
    

    # jar target should not be a list so assume they passed
    # no target and want implicit target to be made and the arg
    # was actaully the list of sources
    if SCons.Util.is_List(target) and source == []:
        SCons.Warnings.Warning("Making implicit target jar file, " +
                              "and treating the list as sources")
        source = target
        target = None

    # mutiple targets pass so build each target the same from the 
    # same source
    #TODO Maybe this should only be done once, and the result copied
    #     for each target since it should result in the same?
    if SCons.Util.is_List(target) and SCons.Util.is_List(source):
        jars = []
        for single_target in target:
            jars += env.Jar( target = single_target, source = source, *args, **kw)
        return jars

    # they passed no target so make a target implicitly
    if target == None:
        try:
            # make target from the first source file
            target = os.path.splitext(str(source[0]))[0] + env.subst('$JARSUFFIX')
        except:
            # something strange is happening but attempt anyways
            SCons.Warnings.Warning("Could not make implicit target from sources, using directory")
            target = os.path.basename(str(env.Dir('.'))) + env.subst('$JARSUFFIX')

    # make lists out of our target and sources
    if not SCons.Util.is_List(target):
        target = [target]
    if not SCons.Util.is_List(source):
        source = [source]

    # setup for checking through all the sources and handle accordingly
    java_class_suffix = env.subst('$JAVACLASSSUFFIX')
    java_suffix = env.subst('$JAVASUFFIX')
    target_classes = []

    # function for determining what to do with a file and not a directory
    # if its already a class file then it can be used as a
    # source for jar, otherwise turn it into a class file then
    # return the source
    def file_to_class(s):
        if(str(_my_normcase(s)).endswith(java_suffix)):
            return env.JavaClassFile(source = s, *args, **kw)
        else:
            return [env.fs.File(s)]

    # In the case that we are passed just string to a node which is directory
    # but does not exist, we need to check all the current targets to see if
    # that directory is going to exist so we can add it as a source to Jar builder
    def get_all_targets(env, node='.'):
        def get_all_targets_iter(env, node):
            if node.has_builder():
                yield node
            for kid in node.all_children():
                for kid in get_all_targets(env, kid):
                    yield kid
        node = env.arg2nodes(node, env.fs.Entry)[0]
        return list(get_all_targets_iter(env, node))

    # loop through the sources and handle each accordingly
    # the goal here is to get all the source files into a class
    # file or a directory that contains class files
    for s in source:
        s = env.subst(s)
        if isinstance(s, SCons.Node.FS.Base):
            if isinstance(s, SCons.Node.FS.File):
                # found a file so make sure its a class file
                target_classes.extend(file_to_class(s))
            else:
                # found a dir so make sure its a dir of class files
                target_classes.extend(env.JavaClassDir(source = env.fs.Dir(s), *args, **kw))
        else:
            if os.path.isfile(s):
                # found a file that exists on the FS, make sure its a class file
                target_classes.extend(file_to_class(s))
            elif os.path.isdir(s):
                # found a dir on the FS, add it as a dir of class files
                target_classes.append(env.fs.Dir(s))
            elif s[-len(java_suffix):] == java_suffix or s[-len(java_class_suffix):] == java_class_suffix:
                # found a file that may not exists and is only a string
                # so add it after converting it to a class file
                target_classes.extend(file_to_class(s))
            else:
                # found a swig file so add it after converting it to class files
                if(os.path.splitext(str(s))[1] == ".i"):
                    target_classes.extend(env.JavaClassFile(source = s, *args, **kw))
                else:
                    # found a directory that does not yet exist, but can exist as a node
                    # check the target nodes to make sure it will be built, then add
                    # it as a source
                    for node in get_all_targets(env):
                        if(s in str(node) and os.path.splitext(str(node))[1] == ""):
                            target_classes.append(node)
    # at this point all our sources have been converted to classes or directories of class
    # so pass it to the Jar builder
    return env.JarFile(target = target, source = target_classes, *args, **kw)
143959
boolean doJarFile(String jarname) throws IOException {
        try (JarFile jf = new JarFile(jarname)) {
            Stream<String> files =
                jf.stream()
                  .map(JarEntry::getName);
            return doFileNames(files);
        }
    }
57835
public static void copyDirectories(File[] directories, String storageFolder) throws IOException {
        storageFolder = checkFolder(storageFolder) + ValueConsts.SEPARATOR;
        for (File directory : directories) {
            copyDirectory(directory, new File(storageFolder + directory.getName()));
        }
    }
861258
public static void unzip(InputStream zipFile, String dest) throws IOException {
        byte[] buffer = new byte[1024];

        //create output directory is not exists
        File folder = new File(dest);

        if (folder.exists()) {
            FileUtil.delete(folder);
        }

        folder.mkdir();

        //get the zip file content
        try (ZipInputStream zis = new ZipInputStream(zipFile)) {
            //get the zipped file list entry
            ZipEntry ze = zis.getNextEntry();
            File parent = new File(dest);
            while (ze != null) {
                String fileName = ze.getName();
                if (ze.isDirectory()) {
                    File newFolder = new File(parent, fileName);
                    newFolder.mkdir();
                } else {
                    File newFile = new File(parent, fileName);
                    try (FileOutputStream fos = new FileOutputStream(newFile)) {
                        int len;
                        while ((len = zis.read(buffer)) > 0) {
                            fos.write(buffer, 0, len);
                        }
                    }
                }
                ze = zis.getNextEntry();
            }

            zis.closeEntry();
        }
    }
11576
protected void scanDir(File srcDir, File destDir, String[] files, String ext)
	{
		// If no source path was specified, we effectively created one by adding the generation
		// path.   Because of this, we need to be sure and add all source dirs to the path too.
		if (!_hasSourcepath)
		{
			Path srcPath = new Path(getProject());
			srcPath.setLocation(srcDir);
			setSourcepath(srcPath);
		}

		GlobPatternMapper m = new GlobPatternMapper();
		m.setFrom(ext);
		m.setTo("*.class");
		SourceFileScanner sfs = new SourceFileScanner(this);
		if (ext.equals("*.java"))
		{
			File[] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m);
			if (newFiles.length > 0)
			{
				File[] newCompileList = new File[compileList.length + newFiles.length];
				System.arraycopy(compileList, 0, newCompileList, 0, compileList.length);
				System.arraycopy(newFiles, 0, newCompileList, compileList.length,
						newFiles.length);
				compileList = newCompileList;
			}
		}
		else
		{
			String [] newSources = sfs.restrict(files, srcDir, destDir, m);
			int extLen = ext.length() - 1;  // strip wildcard
			if (newSources.length > 0)
			{
				File[] newCompileList = new File[compileList.length + newSources.length];
				System.arraycopy(compileList, 0, newCompileList, 0, compileList.length);
				try
				{
					FileUtils fileUtils = FileUtils.newFileUtils();
					for (int j = 0; j < newSources.length; j++)
					{
						String toName =
								newSources[j].substring(0, newSources[j].length() - extLen) +
										".java";

						File srcFile = new File(srcDir, newSources[j]);
						File dstFile = new File(_genDir, toName);
						fileUtils.copyFile(srcFile, dstFile, null, true, true);
						newCompileList[compileList.length + j] = dstFile;
					}
				}
				catch (IOException ioe)
				{
					throw new BuildException("Unable to copy " + ext + " file", ioe,
							getLocation());
				}
				compileList = newCompileList;
			}
		}
	}
249883
public void processFile(File zipFile, File destDir) throws IOException {
        if (!destDir.exists()) {
            throw new IOException("Destionation directory '" + destDir + "' does not exists, or can't be read!");
        }
        ZipFile f = null;
        try {
            f = new ZipFile(zipFile);
            Map<String, ZipEntry> fEntries = getEntries(f);
            String[] names = fEntries.keySet().toArray(new String[] {});
            if (sortNames) {
                Arrays.sort(names);
            }
            // copy all files
            for (int i = 0; i < names.length; i++) {
                String name = names[i];
                ZipEntry e = fEntries.get(name);
                copyFileEntry(destDir, f, e);
            }
        } catch (IOException ioe) {
            String msg = ioe.getMessage();
            if (msg.indexOf(zipFile.toString()) < 0) {
                msg += " - " + zipFile.toString();
            }
            throw new IOException(msg);
        } finally {
            if (f != null) {
                try {
                    f.close();
                } catch (IOException ioe) {
                }
            }
        }
    }
839716
public static void zipFiles(List<File> files, File zipFile) throws IOException {
        OutputStream os = new BufferedOutputStream(new FileOutputStream(zipFile));
        try {
            zipFiles(files, os);
        } finally {
            IOUtils.closeQuietly(os);
        }
    }
1215582
public static void copy(File source, File dest) throws IOException {
        if (dest.isDirectory()) {
            dest = new File(dest + File.separator + source.getName());
        }

        FileChannel in = null, out = null;
        try {
            in = new FileInputStream(source).getChannel();
            out = new FileOutputStream(dest).getChannel();

            long size = in.size();
            MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);

            out.write(buf);
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
1038306
public static Set<Path> getDirectoriesFromJar(Path pathToJarFile) throws IOException {
        Set<Path> result = new HashSet<Path>();
        ZipFile jarfile = new ZipFile(pathToJarFile.toFile());
        try {
            final Enumeration<? extends ZipEntry> entries = jarfile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                if (entry.isDirectory()) {
                    result.add(Paths.get(entry.getName()));
                }
            }
            jarfile.close();
        } finally {
            IOUtils.closeQuietly(jarfile);
        }
        return result;
    }
1186341
public static void copyFile(File src, File dest) throws IOException {

        if (src == null)
            throw new NullPointerException("Source must not be null");
        if (dest == null)
            throw new NullPointerException("Destination must not be null");
        if (!src.exists())
            throw new FileNotFoundException("Source '" + src + "' does not exist");
        if (!src.isFile())
            throw new IOException("Source '" + src + "' is not a file");
        if (dest.exists())
            throw new IOException("Destination '" + dest + "' is already exists");

        FileChannel in = null, out = null;
        try {
            in = new FileInputStream(src).getChannel();
            out = new FileOutputStream(dest).getChannel();

            in.transferTo(0, in.size(), out);
        } finally {
            try {
                in.close();
                out.close();
            } catch (IOException e) {
                // Ignore.
            }
        }

        if (src.length() != dest.length()) {
            throw new IOException("Failed to copy full contents from '" +
                src + "' to '" + dest + "'");
        }

        dest.setLastModified(src.lastModified());
    }
877873
public static boolean unpackJar (JarFile jar, File target)
    {
        boolean failure = false;
        Enumeration<?> entries = jar.entries();
        while (!failure && entries.hasMoreElements()) {
            JarEntry entry = (JarEntry)entries.nextElement();
            File efile = new File(target, entry.getName());

            // if we're unpacking a normal jar file, it will have special path
            // entries that allow us to create our directories first
            if (entry.isDirectory()) {
                if (!efile.exists() && !efile.mkdir()) {
                    log.warning("Failed to create jar entry path", "jar", jar, "entry", entry);
                }
                continue;
            }

            // but some do not, so we want to ensure that our directories exist
            // prior to getting down and funky
            File parent = new File(efile.getParent());
            if (!parent.exists() && !parent.mkdirs()) {
                log.warning("Failed to create jar entry parent", "jar", jar, "parent", parent);
                continue;
            }

            BufferedOutputStream fout = null;
            InputStream jin = null;
            try {
                fout = new BufferedOutputStream(new FileOutputStream(efile));
                jin = jar.getInputStream(entry);
                StreamUtil.copy(jin, fout);
            } catch (Exception e) {
                log.warning("Failure unpacking", "jar", jar, "entry", efile, "error", e);
                failure = true;
            } finally {
                StreamUtil.close(jin);
                StreamUtil.close(fout);
            }
        }

        try {
            jar.close();
        } catch (Exception e) {
            log.warning("Failed to close jar file", "jar", jar, "error", e);
        }

        return !failure;
    }
993481
public static void copyDirs(Path source, Path target, CopyOption... options) throws IOException {
        if (Files.notExists(target)) {
            Files.createDirectory(target);
        }

        logger.debug("  --> Copying resources from [{}]", source);
        if (Files.notExists(source)) {
            throw new RuntimeException(source + " doesn't seem to exist.");
        }

        Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
                new InternalFileVisitor(source, target, options));

        logger.debug("  --> Resources ready in [{}]", target);
    }