//Please don't sue me if the following code causes your pc to crashimport java.io.*;import java.lang.*; import java.util.*;class pngConvert{ //Set (iweb) root directory String rootDir = "testmappe/"; //Don't touch these ArrayList pngFiles = new ArrayList(); ArrayList htmlFiles = new ArrayList(); //and js, they are used the same way ArrayList fileExceptions = new ArrayList(); String launchCommand[] = new String[4]; long lengthBefore = 0; long lengthAfter = 0; //CONSTRUCTOR, MAIN LOGIC public pngConvert() { //Use this command for convertion launchCommand[0] = "open"; launchCommand[1] = "-a"; launchCommand[2] = "jpeg.exe"; //jpeg.exe is an imageready droplet //Set file exceptions (do not convert any of these files). Some of them need the transparency provided with png fileExceptions.add("navbar"); //exclude all images that has "navbar" in its filename fileExceptions.add("shapeimage"); fileExceptions.add("home_image"); fileExceptions.add("IMG_4655.png"); fileExceptions.add("Billede 020_1.png"); //Look for .png, .html and .js. Set arraylists pngFiles and htmlFiles System.out.println("Searching..."); findFiles(rootDir); //Remove files from pngFiles (based on fileExceptions) filterPngFiles(); //Replace image paths in html and js files (ex. files/bg.png -> files/bg.jpg) replacePaths(); //Rename files that contains spaces (replace with ___) renamePngFiles(); //Convert all png files in pngFiles by executing launchCommand (with png file as param) System.out.println("Converting..."); convertPngFiles(); //Wait for user confirmation (Bad idea to delete files if the proces hasn't finished) System.out.println("Press enter when done... (done is the keyword here)"); userConfirm(); //Delete original png files deletePngFiles(); System.out.println("\n\n\n\n\n"); System.out.println("Well done..."); System.out.println("Size before: " + (lengthBefore/1000) + " kb"); System.out.println("Size after: " + (lengthAfter/1000) + " kb"); System.out.println("\n\n\n\n\n"); } //Search for png, html and js files in a given directory (website root) //Method is recursive void findFiles(String dir) { File file = new File (dir); File[] files = file.listFiles(); //Go though all files in dir for(int i=0; i < files.length; i++) { //If its a directory, then call itself if (files[i].isDirectory()) { findFiles(files[i].toString()); } else //Determine if the file is important (png, html, js) { if (files[i].toString().endsWith(".png")) { pngFiles.add(files[i].toString()); } else if (files[i].toString().endsWith(".html") || files[i].toString().endsWith(".js")) { htmlFiles.add(files[i].toString()); } } } //end loop } //end function //Remove png files from pngFiles list (fileExceptions) void filterPngFiles() { //Go though all file exceptions for(int i=0; i < fileExceptions.size(); i++) { for(int c=0; c < pngFiles.size(); c++) { String name = pngFiles.get(c).toString(); //Current png file String ex = fileExceptions.get(i).toString(); //String to match if (name.indexOf(ex) > 0) { pngFiles.remove(pngFiles.get(c).toString()); c--; //took a while to figure out :-) } } } } //Replace paths (make all(most) image paths end with jpg instead of png) void replacePaths() { //Go though all html files one by one for(int i=0; i < htmlFiles.size(); i++) { //Get file content String fileContent = getFileContent(htmlFiles.get(i).toString()); for(int c=0; c < pngFiles.size(); c++) { //Split current png filename (only the last part is used, not the whole path) String[] split = pngFiles.get(c).toString().split("/"); String oldFilename = split[split.length-1]; //Be aware of spaces (%20 in html files) oldFilename = oldFilename.replaceAll(" ", "%20"); //This is the string to replace //Remove png and add jpg String newFilename = oldFilename.substring(0, oldFilename.length()-3) + "jpg"; if (newFilename.length() > 30) newFilename = "_"+newFilename.substring(newFilename.length()-15,newFilename.length()); //imageready has a thing with filenames too big newFilename = newFilename.replaceAll("%20", "__"); //future filnames have _'s instead of spaces, otherwise imageready screws it up //Replace (finally) fileContent = fileContent.replaceAll("/"+oldFilename, "/"+newFilename); } //Writ new and replaced content to file writeFileContent(htmlFiles.get(i).toString(), fileContent); } //end loop } //Used by method above, returns content of a given file as a string String getFileContent(String filename) { String returnContent = ""; try { FileReader file = new FileReader(filename); BufferedReader in = new BufferedReader(file); //Line by line String line = in.readLine(); while (line != null) { returnContent += line + "\n"; line = in.readLine(); } } catch (Exception e) { System.out.println("Holy moly, something went wrong (read)..."); } return returnContent; } //End function void writeFileContent(String filename, String content) { try { FileWriter file = new FileWriter(filename); file.write(content); file.close(); } catch (IOException e) { System.out.println("Holy moly, something went wrong (write)..."); } } //Rename files that contain spaces void renamePngFiles() { for(int i=0; i < pngFiles.size(); i++) { //imageready has a thing with filenames too big String newFilename = pngFiles.get(i).toString(); String[] split = newFilename.split("/"); String fn = split[split.length-1]; if (fn.length() > 30) fn = "_"+fn.substring(fn.length()-15,fn.length()); fn = fn.replaceAll(" ", "__"); fn = newFilename.substring(0, newFilename.indexOf(split[split.length-1])) + fn; //Rename (new File(pngFiles.get(i).toString())).renameTo(new File(fn)); //Update pngFiles pngFiles.set(i, fn); } } void convertPngFiles() { Process a; for(int i=0; i < pngFiles.size(); i++) { String filename = pngFiles.get(i).toString(); //Execute try { launchCommand[3] = filename; a = Runtime.getRuntime().exec(launchCommand); a.waitFor(); } catch(Exception e) { //oh crap System.out.println("Holy moly, something went wrong (converting)..."); e.printStackTrace(); } } } void userConfirm() { //wait for user confirmation BufferedReader waitForInput = new BufferedReader(new InputStreamReader(System.in)); try { waitForInput.readLine(); } catch (IOException ioe) { System.out.println("Holy cow, something went completely wrong"); } } void deletePngFiles() { for(int i=0; i < pngFiles.size(); i++) { String filename = pngFiles.get(i).toString(); //Get old filesize File f = new File(filename); lengthBefore += f.length(); //delete png f.delete(); //Get new filesize String jpgFile = filename.substring(0, filename.length()-3) + "jpg"; File ff = new File(jpgFile); lengthAfter += ff.length(); } }} //End classpublic class converter{ public static void main(String[] args) { pngConvert png = new pngConvert(); }}