Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileFilter;
Import java.io.IOException;
Import Java.io.InputStreamReader;
/**
* Compression JS
* Java-jar Yuicompressor-2.4.jar--type js xxx.js-o xxx.js--charset utf-8
* Compress CSS
* Java-jar yuicompressor-2.4.jar--type css xxx.css-o xxx.css--charset utf-8
* Java Runtime.getruntime (). EXEC (COMMANDSTR) can invoke the Execute CMD command.
* (1) cmd/c dir closes the command window after executing the dir command.
* (2) cmd/k dir does not close the command window after executing the dir command.
* (3) cmd/c start dir will open a new window and execute the dir instruction, the original window will be closed.
* (4) cmd/k start dir will open a new window and execute the dir instruction, the original window will not be closed.
* @author Ligang
* PS: When compressing CSS, it will affect JS and other types of files!!!
* Compression js, no problem, it is recommended to compress which type of file when the type of file placed!!!
*/
public class Zipjs {
public static void Main (string[] args) throws Exception {
File exists directory
String dir = "D:\\js";
string[] FileNames = GetFileNames (dir);
System.out.println ("The total number of files:" +filenames.length);
for (String string:filenames) {
if (String.Equals ("Yuicompressor-2.4.jar")) {
Continue;
}
String cmd = "Java-jar yuicompressor-2.4.jar--type js" +string+ "-o" +string+ "--charset utf-8";
String cmd = "Java-jar yuicompressor-2.4.jar--type css" +string+ "-o" +string+ "--charset utf-8";
Runtime.getruntime (). EXEC ("cmd/c D: & CD" +dir+ "&" +cmd);
}
}
Get all file names under the current folder
private static string[] GetFileNames (String dir) {
File File = new file (dir);
file[] files = file.listfiles (new Fileonlyfilter ());
string[] ary = new String[files.length];
for (int i = 0; i < files.length; i++) {
Ary[i] = Files[i].getname ();
}
return ary;
}
}
Class Fileonlyfilter implements FileFilter {
Public boolean accept (file file) {
return File.isfile ();
}
}
This article is from the "Li Gang Learning Journey" blog, please make sure to keep this source http://381510688.blog.51cto.com/4623168/1570495
Compression Js-yuicompressor-2.4.jar