/*
* 作成日: 2004/05/22
* License Apache 2.0
* この生成されたコメントの挿入されるテンプレートを変更するため
* ウィンドウ > 設定 > Java > コード生成 > コードとコメント
*/
package org.jpn.xucker.optipngwrapper;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Vector;
import org.apache.commons.io.FileUtils;
/**
* @author ak License Apache 2.0
*
* この生成されたコメントの挿入されるテンプレートを変更するため ウィンドウ > 設定 > Java > コード生成 > コードとコメント
*/
public class OptiPNGExecuter{
private int finish;
private String level = "5";
private File output;
private List fileList=new Vector();
private String optipngPath="optipng.exe";
private String backupExtension=".bk";
private boolean doBackup;
private List resultVector=new Vector();
public ResultData[] getResultData(){
return (ResultData[])resultVector.toArray(new ResultData[resultVector.size()]);
}
public String getBackupExtension() {
return backupExtension;
}
public void setBackupExtension(String backupExtension) {
this.backupExtension = backupExtension;
}
public boolean isDoBackup() {
return doBackup;
}
public void setDoBackup(boolean doBackup) {
this.doBackup = doBackup;
}
public String getOptipngPath() {
return optipngPath;
}
public void setOptipngPath(String optipngPath) {
this.optipngPath = optipngPath;
}
public void addFile(String path){
fileList.add(path);
}
public void execute() throws IOException{
File f = new File(optipngPath);
if (!f.exists()) {
throw new IOException("file not exsits :" + optipngPath);
}
String optiLevel="7";
try {
int v = Integer.parseInt(level);
if (v < 0 || v > 7) {
v=7;
}
optiLevel = "" + v;
} catch(Exception e){
e.printStackTrace();
}
String paths[]=(String[])fileList.toArray(new String[fileList.size()]);
for (int i = 0; i < paths.length; i++) {
preExecute(new File(paths[i]));
}
}
/**
* @param file
*/
private void preExecute(File file) {
//System.out.println("pre "+file.getAbsolutePath());
if(output!=null){
String rootFolder=file.getParent();
doExecute(rootFolder,output,file);
}else{
doExecute(null,null,file);
}
}
/**
* @param rootFolder
* @param output2
* @param file
*/
private String getRelativePath(File file, String basePath) {
if(!file.getAbsolutePath().startsWith(basePath)){
throw new RuntimeException("not start basePath");
}
if(!basePath.endsWith(System.getProperty("file.separator"))){
throw new RuntimeException("basepath not ends with file separator");
}
if(file.isDirectory()){
throw new RuntimeException("file is must file");
}
return file.getAbsolutePath().substring(basePath.length());
}
private void doExecute(String rootFolder, File output, File file) {
//System.out.println("doexecute "+file.getAbsolutePath());
if(file.isDirectory()){
String list[]=file.list();
for (int i = 0; i < list.length; i++) {
File tmp=new File(file,list[i]);
if(tmp.isDirectory() || tmp.getName().toLowerCase().endsWith(".png")){
doExecute(rootFolder,output,tmp);
}
}
}else{
long pre=file.length();
//do backup
File optimizeFile=null;
if(output!=null){
// System.out.println("output not null "+file.getAbsolutePath().substring(rootFolder.length()+1));
optimizeFile=new File(output, getRelativePath(file,rootFolder));
if(!file.getAbsolutePath().equals(optimizeFile.getAbsolutePath())){
try {
FileUtils.copyFile(file,optimizeFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else{
optimizeFile=file;
}
if(optimizeFile.getAbsolutePath().equals(file.getAbsolutePath()) && isDoBackup()){
try {
FileUtils.copyFile(file,new File(optimizeFile.getParentFile(),optimizeFile.getName()+getBackupExtension()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
callOptiPng(optimizeFile,level);
//do optimize
//add result
long end=optimizeFile.length();
ResultData result=new ResultData();
result.setFile(optimizeFile.getName());
result.setDirectory(optimizeFile.getParent());
// System.out.println("opt "+optimizeFile.getAbsolutePath()+","+optimizeFile.getName()+","+optimizeFile.getParent());
result.setSize(pre,end);
resultVector.add(result);
}
}
/**
* @param inputFile
* @param outputFile
* @param level
*/
private void callOptiPng(File outputFile, String level) {
// System.out.println("callOptiPng:"+outputFile.getAbsolutePath());
//log(outputFile.getAbsolutePath());
Runtime runtime = Runtime.getRuntime();
if(!outputFile.exists()){
outputFile.getParentFile().mkdirs();
}
String args[] = { optipngPath, "-q","-v", "-o", level,
outputFile.getAbsolutePath() };
// System.out.println(optipngPath+" "+outputFile.getAbsolutePath());
//log(outputFile.getAbsolutePath(), Project.MSG_VERBOSE);
try {
//System.out.println(outputFile.getAbsolutePath());
Process process = runtime.exec(args);
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
finish++;
}
public static String readAll(InputStream reader) {
StringBuffer text = new StringBuffer();
int c;
try {
while ((c = reader.read()) != -1) {
System.out.println((char)c);
text.append((char) c);
}
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
return text.toString();
}
/**
* @return
*/
public String getLevel() {
return level;
}
/**
* @return
*/
public File getOutput() {
return output;
}
/**
* @param string
*/
public void setLevel(String string) {
level = string;
}
/**
* @param file
*/
public void setOutput(File file) {
output = file;
}
}
|