大部分代碼我是從網上找的,
只有對文件的遍歷是我自己寫的;
其實原理十分簡單
利用yuicompressor已經編好的方法,我們寫一個Java程序來調用即可;
yuicompressor-2.4.8.jar封裝好的兩個類:
JavaScriptCompressor;
CssCompressor;
然后就是底下的程序了:
提醒一句最好把項目插件去掉再來,畢竟那不是自己寫的,壓縮可能出毛病。
里面的filepath變量是你要執行的目錄
另外自己需要new一個新的下面這個類,并執行壓縮方法;
ready perfectly!
--------》
package com.cmd;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Arrays;
import java.util.Date;
import java.util.List;import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.EvaluatorException;import com.yahoo.platform.yui.compressor.CssCompressor;
import com.yahoo.platform.yui.compressor.JavaScriptCompressor;public class ZipJs {private static final String encoding = "utf-8";private static final String[] suffixArray = { ".js", ".css" };// 需要壓縮的文件夾(會壓縮此文件夾下的所有js、css文件)private String filePath = "D:\\myfront";static int linebreakpos = 1000;// -1;換行:負數表示不換行static boolean munge = true;static boolean verbose = false;static boolean preserveAllSemiColons = false;static boolean disableOptimizations = false;static Long count = 0L;/*** 壓縮方法*/public void compress() {init(filePath);}// 遞歸遍歷文件夾public static File[] getAllFile(File f) {File[] files = null;if (f.isDirectory()) {files = f.listFiles();for (File fx : files) {if (fx.isDirectory()) {getAllFile(fx);continue;}compress(fx);count++;System.out.println(fx.getName() + "====\n");}}return files;}// 初始化,獲取文件目錄下的所有需要壓縮的文件 js csspublic static void init(String filePath) {Date startTime = new Date();File file = new File(filePath);getAllFile(file);Date endTime = new Date();Long cost = endTime.getTime() - startTime.getTime();System.out.println("壓縮完成,耗時:" + cost + "ms,共壓縮文件個數:" + count);}// 壓縮public static void compress(File file) {try {String fileName = file.getName();String suffix = fileName.substring(fileName.lastIndexOf("."), fileName.length());System.out.println(fileName + "======\n" + suffix);List<String> suffixList = Arrays.asList(suffixArray);if (suffixList.contains(suffix) && !fileName.endsWith("-min" + suffix)) {Reader in = new InputStreamReader(new FileInputStream(file), encoding);// 以UTF-8格式讀取,否則壓縮出來會亂碼String filePath = file.getAbsolutePath();File tempFile = new File(filePath + ".tempFile");Writer out = new OutputStreamWriter(new FileOutputStream(tempFile), encoding);if (fileName.endsWith(".js")) {// js compressorJavaScriptCompressor jscompressor = new JavaScriptCompressor(in, new ErrorReporter() {// 壓縮出錯后的操作public void warning(String message, String sourceName, int line, String lineSource,int lineOffset) {if (line < 0) {System.err.println("\n[WARNING] " + message);} else {System.err.println("\n[WARNING] " + line + ':' + lineOffset + ':' + message);}}public void error(String message, String sourceName, int line, String lineSource,int lineOffset) {if (line < 0) {System.err.println("\n[ERROR] " + sourceName + "-" + message);} else {System.err.println("\n[ERROR] " + line + ':' + lineOffset + ':' + sourceName + "-" + message);}}public EvaluatorException runtimeError(String message, String sourceName, int line,String lineSource, int lineOffset) {error(message, sourceName, line, lineSource, lineOffset);return new EvaluatorException(message);}});// 開始壓縮jscompressor.compress(out, linebreakpos, munge, verbose, preserveAllSemiColons,disableOptimizations);} else if (fileName.endsWith(".css")) {// css compressorCssCompressor csscompressor = new CssCompressor(in);csscompressor.compress(out, linebreakpos);}in.close();out.close();file.delete();tempFile.renameTo(file);tempFile.delete();}} catch (Exception e) {e.printStackTrace();}}}
總結
以上是生活随笔為你收集整理的Java批量完成对文件夹下全部的css与js压缩,利用yuicompressor的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。