天天看點

utf-8 html轉chm,檔案轉碼 GBK UTF-8互轉,添删BOM,Easy CHM合成前轉碼

CharsetUnits.java

package linwancheng.charset;

import java.io.File;

import java.io.FileFilter;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.util.ArrayList;

public class CharsetUnits {

final static String BOM = new String(new byte[] { -17, -69, -65 });

public static void src2desc(FileFilter filter, String inCharset, String outCharset) {

src2desc(filter, "srcfile", "descfile", inCharset, outCharset, false, -1);

}

public static void src2desc(FileFilter filter, String inCharset, String outCharset, boolean addH4charset, int addBOM) {

src2desc(filter, "srcfile", "descfile", inCharset, outCharset, addH4charset, addBOM);

}

public static void src2desc(FileFilter filter, String srcPath, String descPath, String inCharset,

String outCharset, boolean addH4charset, int addBOM) {

ArrayList

fileList = new ArrayList

(); listDeep(fileList, filter, false, true, new File(srcPath)); for (int i = 0; i < fileList.size(); i++) { String inPath = fileList.get(i).getPath(); System.out.println((i + 1) + "/" + fileList.size());// 顯示進度 String outPath = descPath + inPath.substring(inPath.indexOf("\\")); new File(outPath.substring(0, outPath.lastIndexOf("\\"))).mkdirs(); ToUTF8(inPath, outPath, inCharset, outCharset, addH4charset, addBOM); } } public static void ToUTF8(String inPath, String outPath, String inCharset, String outCharset, boolean addH4charset, int addBOM) { try { BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(inPath), inCharset)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outPath, addBOM > 0), outCharset)); String readLine; if ((readLine = in.readLine()) != null) { if (addBOM != 0) { boolean hasBOM = BOM.equals(readLine.substring(0, 2)); if (!hasBOM && addBOM > 0) { readLine = BOM + readLine; } if (hasBOM && addBOM < 0) { readLine = readLine.substring(2); } } out.write(readLine); out.newLine(); } while ((readLine = in.readLine()) != null) { out.write(readLine); out.newLine(); // 添加HTML4标簽相容舊CHM閱讀器 if (addH4charset && readLine.startsWith("

"); out.newLine(); } } in.close(); out.close(); } catch (Exception e) { throw new RuntimeException(e); } } public static boolean hasBOM(String path) { boolean result; try { FileInputStream in = new FileInputStream(path); byte[] b = new byte[3]; in.read(b); in.close(); result = (b[0] == -17 && b[1] == -69 && b[2] == -65); } catch (Exception e) { throw new RuntimeException(e); } return result; } public static void listDeep(ArrayList

fileList, FileFilter filter, boolean dir, boolean file, File... files) { for (File f : files) { if (f.isDirectory()) { if (dir) fileList.add(f); listDeep(fileList, filter, dir, file, f.listFiles(filter)); } else if (file) fileList.add(f); } } } class NameEnd implements FileFilter { String[] nameEnds; public NameEnd(final String... nameEnds) { this.nameEnds = nameEnds; } @Override public boolean accept(final File pathname) { if (pathname.isDirectory()) return true; String name = pathname.getName(); int index = name.lastIndexOf(".") + 1; String en = index == 0 ? "" : name.substring(index); for (String e : nameEnds) { if (en.equalsIgnoreCase(e)) return true; } return false; } }

CharsetTest.java

package linwancheng.charset;

import org.junit.Test;

public class CharsetTest {

@Test

public void DelBOM() {

CharsetUnits.src2desc(null, "UTF-8", "UTF-8", false, -1);

}

@Test

public void GBKToUTF8() {

CharsetUnits.src2desc(null, "GBK", "UTF-8", false, -1);

}

@Test

public void ForEasyCHM() {

CharsetUnits.src2desc(new NameEnd("htm", "html"), "GBK", "UTF-8", true, +1);

}

@Test

public void UTF8ToGBK() {

CharsetUnits.src2desc(null, "UTF-8", "GBK", false, -1);

}

}