发布网友 发布时间:2022-04-21 19:37
共2个回答
热心网友 时间:2024-04-08 23:37
给你我自己用的代码吧~~~
package com.wanghe;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 脚本处理类
* 1.实现压缩单个js文件--去除注释换行和多个空格,最终输出一行字符串
* 2.实现批量js压缩功能,压缩目录下的所有js文件到对应的输出目录下
* 3.实现js合并功能 根据需要压缩的列表和输出文件路径及是否压缩生成对应文件
* @author zheng
* @version 1.0
*/
public class ScriptHelp
{
private static final String ENCODE = "GBK";
public static void main(String[] args)
{
//批量压缩js文件
String baseScriptPath = "D:/easy-tab.js";
String miniScriptPath = "D:/mini/easy-tab-mini.js";
//batchCompressJS(baseScriptPath,miniScriptPath);
compressSingleJS(baseScriptPath,miniScriptPath);
//压缩单个js文件
//compressSingleJS("D:/workspace/coos/WebRoot/scripts/coos.js","D:/workspace/coos/WebRoot/scripts/mini/coos.js");
/*
//合并js文件
List<String> fileList = new ArrayList<String>();
fileList.add("D:/workspace/coos/WebRoot/scripts/coos.js");
fileList.add("D:/workspace/coos/WebRoot/scripts/coos.extend.ajax.js");
String toFile = "D:/workspace/coos/WebRoot/scripts/mini/coos.js";
mergeJS(fileList,toFile,true);
*/
}
/**
* 批量压缩js,压缩当前目录下的所有js
* @param baseScriptPath 要压缩的文件目录
* @param miniScriptPath 输出压缩后对应的目录
*/
@SuppressWarnings("unchecked")
public static void batchCompressJS(String baseScriptPath,String miniScriptPath)
{
//获取当前目录下所以js文件路径(不包括子目录)
List fileList = getListFiles(baseScriptPath,"js",false);
for (Iterator i = fileList.iterator(); i.hasNext();)
{
String fromFile = (String) i.next();
String toFile = miniScriptPath +fromFile.substring(fromFile.lastIndexOf("/"), fromFile.length());
compressSingleJS(fromFile,toFile);
}
}
/**
* 压缩单个js文件
* @param fromFile
* @param toFile
*/
public static void compressSingleJS(String fromFile,String toFile)
{
String content = readFile(fromFile);
writeFile(compressJS(content),toFile);
}
/**
* 合并js文件
* @param fileList 文件全路径的list,需要按顺序
* @param toFile 输出文件的全路径
* @param isCompress 是否压缩
*
*/
@SuppressWarnings("unchecked")
public static void mergeJS(List fileList,String toFile,Boolean isCompress)
{
String content = "";
for (Iterator i = fileList.iterator(); i.hasNext();)
{
String fromFile = (String) i.next();
content += readFile(fromFile);
}
if(isCompress == true)
writeFile(compressJS(content),toFile);
else
writeFile(content,toFile);
}
/**
* 去除注释、多个空格和换行,最终形成一行的字符串
* @param content 要压缩的内容
* @return 压缩后的内容
*/
public static String compressJS(String content)
{
//去掉/*some code*/的注释 注意alert()里不要有/**/
content = content.replaceAll("//.*[\\r\\n]","");
//去掉/*some code*/的注释 注意alert()里不要有/**/
content = content.replaceAll("\\/\\*(a|[^a])*?\\*\\/","");
/*多余的空格*/
content = content.replaceAll("\\s{2,}"," ");
//等号两边的空格去掉
content = content.replaceAll("\\s*=\\s*","=");
//}两边的空格去掉
content = content.replaceAll("\\s*}\\s*","}");
//{两边的空格去掉
content = content.replaceAll("\\s*\\{\\s*","\\{");
//冒号两边的空格去掉
content = content.replaceAll("\\s*:\\s*",":");
//逗号两边的空格去掉
content = content.replaceAll("\\s*,\\s*",",");
//分号两边的空格去掉
content = content.replaceAll("\\s*;\\s*",";");
//与两边的空格去掉
content = content.replaceAll("\\s*&&\\s*","&&");
//或两边的空格去掉
content = content.replaceAll("\\s*\\|\\|\\s*","\\|\\|");
/*替换换行和回车*/
content = content.replaceAll("\\r\\n","").replaceAll("\\n", "");
return content;
}
/**
* 输出文件,编码为UTF-8 用记事本另存为:fileContent 全部为英文则为ansi 包含中文则为UTF-8
* @param content 要输出的文件内容
* @param comspec 全路径名
*/
public static void writeFile(String content,String comspec)
{
try
{
int i = comspec.lastIndexOf("/");
String dirs = comspec.substring(0,i);
File file = new File(dirs);
if(!file.exists()){
file.mkdir();
}
file = new File(comspec);
if(!file.exists()){
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
Writer out = new OutputStreamWriter(fos,ENCODE);
out.write(content);
System.out.println("成功输出文件:" + comspec);
out.close();
fos.close();
} catch (IOException e)
{
System.out.println("写文件操作出错!");
e.printStackTrace();
}
}
/**
* 读取文件内容
* @param filePath
* @return String
*/
public static String readFile(String filePath)
{
StringBuilder sb = new StringBuilder();
try
{
File file = new File(filePath);
InputStreamReader read = new InputStreamReader (new FileInputStream(file),ENCODE);
BufferedReader reader=new BufferedReader(read);
String s = reader.readLine();
while (s != null)
{
sb.append(s);
sb.append("\r\n");
s = reader.readLine();
}
reader.close();
} catch (IOException e)
{
e.printStackTrace();
}
return sb.toString();
}
public static List<String> fileList = new ArrayList<String>();
/**
* @param path 文件路径
* @param suffix 后缀名
* @param isdepth 是否遍历子目录
* @return fileList
*/
@SuppressWarnings("unchecked")
public static List getListFiles(String path, String suffix, boolean isdepth)
{
File file = new File(path);
return listFile(path,file ,suffix, isdepth);
}
/**
* 获取当前目录下文件路径
* @param path
* @param f
* @param suffix
* @param isdepth
* @return
*/
@SuppressWarnings("unchecked")
public static List listFile(String path, File f, String suffix, boolean isdepth)
{
//是目录,同时需要遍历子目录
String temp = path.replaceAll("/","\\\\");
if ((f.isDirectory() && isdepth == true) || temp.equals(f.getAbsolutePath()))
{
File[] t = f.listFiles();
for (int i = 0; i < t.length; i++)
{
listFile(path,t[i], suffix, isdepth);
}
}
else
{
addFilePath(f ,suffix, isdepth);
}
return fileList;
}
/**
* 添加文件路径到list中
* @param f
* @param suffix
* @param isdepth
* @return
*/
@SuppressWarnings("unchecked")
public static List addFilePath(File f, String suffix, boolean isdepth)
{
String filePath = f.getAbsolutePath().replaceAll("\\\\", "/");
if(suffix !=null)
{
int begIndex = filePath.lastIndexOf(".");
String tempsuffix = "";
if(begIndex != -1)//防止是文件但却没有后缀名结束的文件
{
tempsuffix = filePath.substring(begIndex + 1, filePath.length());
}
if(tempsuffix.equals(suffix))
{
fileList.add(filePath);
}
}
else
{
fileList.add(filePath);//后缀名为null则为所有文件
}
return fileList;
}
}追问最好是提供一个可以直接用的js压缩工具 这些代码该怎么用呢
热心网友 时间:2024-04-08 23:38
yahoo的 yui compressors
网上有下的。