Skip to content

Instantly share code, notes, and snippets.

@ShubhamS32
Created April 6, 2018 18:05
Show Gist options
  • Select an option

  • Save ShubhamS32/33cb61accae103d1c061ec86e1bfb14f to your computer and use it in GitHub Desktop.

Select an option

Save ShubhamS32/33cb61accae103d1c061ec86e1bfb14f to your computer and use it in GitHub Desktop.
This program copies jar from the source folder to the destination folder.
package jarcopier;
import java.io.*;
import java.util.Enumeration;
import java.util.jar.*;
/*
* @author Shubham Shah
*/
public class copy {
public static void main(String[] args) throws Exception {
File shubhamDir = new File(sourceLocation);
File destDir = new File(destinationLocation);
copy cp = new copy();
if (shubhamDir.isFile()) {
cp.copyJarFile(new JarFile(shubhamDir), destDir);
} else if (shubhamDir.isDirectory()) {
File[] files = shubhamDir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".jar");
}
});
for (File f : files) {
System.out.println("Shubham File f-->"+f);
cp.copyJarFile(new JarFile(f), destDir);
}
}
}
public void copyJarFile(JarFile jarFile, File destDir) throws IOException {
String fileName = jarFile.getName();
String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator));
File destFile = new File(destDir, fileNameLastPart);
JarOutputStream jos = new JarOutputStream(new FileOutputStream(destFile));
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
InputStream is = jarFile.getInputStream(entry);
jos.putNextEntry(new JarEntry(entry.getName()));
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = is.read(buffer)) != -1) {
jos.write(buffer, 0, bytesRead);
}
is.close();
jos.flush();
jos.closeEntry();
}
jos.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment