Skip to content

Instantly share code, notes, and snippets.

@matutter
Created June 2, 2015 02:19
Show Gist options
  • Select an option

  • Save matutter/4c9c3e05b379467d1bc4 to your computer and use it in GitHub Desktop.

Select an option

Save matutter/4c9c3e05b379467d1bc4 to your computer and use it in GitHub Desktop.
Extract resources from a jar from jar
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication23;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Properties;
import java.util.jar.JarFile;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import org.apache.commons.io.FileUtils;
/**
*
* @author Mat
*/
public class JavaApplication23 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
String path = "C:\\Users\\Mat\\Desktop\\sdf";
String s = System.getProperty("java.class.path");
JarFile jar = new JarFile( new File( s ) );
jar.stream().forEach(e->{
if( e.getName().contains("stuff") ) {
InputStream is = ClassLoader.getSystemResourceAsStream(e.getName());
File f = new File( path + File.separator + e.getName() );
if( e.isDirectory() ) {
f.mkdirs();
System.out.println( "making dir " + e.getName() );
} else {
try {
f.createNewFile();
System.out.println( e.getName() );
try (FileOutputStream fos = new FileOutputStream( f )) {
byte[] buff = new byte[1024];
while( is.available()>0 ) {
is.read(buff);
fos.write(buff);
}
is.close();
}
} catch (FileNotFoundException ex) {
Logger.getLogger(JavaApplication23.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(JavaApplication23.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
} catch (IOException ex) {
Logger.getLogger(JavaApplication23.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment