1 package edu.jiangxin.apktoolbox.file.core;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.FileOutputStream;
8 import java.io.InputStreamReader;
9 import java.io.OutputStreamWriter;
10 import java.util.Collection;
11 import java.util.Iterator;
12
13 import org.apache.commons.io.FileUtils;
14 import org.apache.commons.lang3.StringUtils;
15 import org.apache.logging.log4j.LogManager;
16 import org.apache.logging.log4j.Logger;
17
18
19
20
21
22
23
24
25 public class EncoderConvert {
26
27 private static final String TMP_SUFFIX = ".temp";
28 private static final Logger logger = LogManager.getLogger(EncoderDetector.class.getSimpleName());
29
30
31
32
33
34
35
36
37
38 public static void encodeFile(String srcFileString, String srcEncoder, String desFileString, String desEncoder) {
39 if (srcFileString.equals(desFileString)) {
40 srcFileString = srcFileString + EncoderConvert.TMP_SUFFIX;
41 FileProcess.copyFile(desFileString, srcFileString);
42 }
43 File srcFileFile = new File(srcFileString);
44 File desFileFile = new File(desFileString);
45
46 File parentDir = desFileFile.getParentFile();
47 if (!parentDir.exists()) {
48 boolean success = parentDir.mkdirs();
49 if (!success) {
50 logger.error("delete srcFileFile failed");
51 return;
52 }
53 }
54
55 try (BufferedReader reader = new BufferedReader(
56 new InputStreamReader(new FileInputStream(srcFileFile), srcEncoder));
57 BufferedWriter writer = new BufferedWriter(
58 new OutputStreamWriter(new FileOutputStream(desFileFile), desEncoder))) {
59 int ch = 0;
60 while ((ch = reader.read()) != -1) {
61 writer.write(ch);
62 }
63 } catch (Exception e) {
64 logger.error("Exception", e);
65 return;
66 }
67
68 if (srcFileString.equals(desFileString + EncoderConvert.TMP_SUFFIX)) {
69 boolean success = srcFileFile.delete();
70 if (!success) {
71 logger.error("delete srcFileFile failed");
72 }
73 }
74 }
75
76
77
78
79
80
81
82
83 public static void encodeFile(String fileString, String srcEncoder, String desEncoder) {
84 encodeFile(fileString, srcEncoder, fileString, desEncoder);
85 }
86
87
88
89
90
91
92
93
94
95
96 public static void encodeDir(String srcDirString, String srcEncoder, String desDirString, String desEncoder,
97 String suffix) {
98
99 File desDirFile = new File(desDirString);
100
101 String[] extensions = null;
102 if (StringUtils.isNotEmpty(suffix)) {
103 extensions = suffix.split(",");
104 }
105 Collection<File> files = FileUtils.listFiles(new File(srcDirString), extensions, true);
106 Iterator<File> it = files.iterator();
107 while (it.hasNext()) {
108 File tempFile = it.next();
109 String desFileString = desDirFile.getAbsolutePath() + File.separator + tempFile.getName();
110 String srcFileString = tempFile.getAbsolutePath();
111 encodeFile(srcFileString, srcEncoder, desFileString, desEncoder);
112 }
113 }
114
115
116
117
118
119
120
121
122
123 public static void encodeDir(String srcDirString, String srcEncoder, String desDirString, String desEncoder) {
124 encodeDir(srcDirString, srcEncoder, desDirString, desEncoder, null);
125 }
126 }