对jar或者war进行加密解密
whycxzp
2021-07-15 cfdf00d3357268fa6de56db849e40aeaf714cff3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.whyc.util;
 
import java.io.File;
 
public class HtmlUtils {
    public static void main(String[] args) throws Exception {
        String txt = IoUtils.readTxtFile(new File("/Users/roseboy/work-yiyon/易用框架/yiyon-framework/jeee-importer/src/main/resources/templates/importer-upload-form-pro.ftl"));
        long t1 = System.currentTimeMillis();
        txt = removeComments(txt);
        txt = removeBlank(txt);
        //txt = removeBr(txt);
 
        long t2 = System.currentTimeMillis();
        System.out.println(txt);
        //System.out.println(t2 - t1);
    }
 
    /**
     * 去除代码中的注释
     *
     * @param code 代码
     * @return 代码
     */
    public static String removeBlank(String code) {
        StringBuilder result = new StringBuilder();
 
        int quot = 0;//单引号
        int quots = 0;//双引号
        boolean inScript = false;//进入script标签
        boolean inStyle = false;//进入style标签
        boolean inTextArea = false;//进入textaera标签
        char[] chars = code.replace("\r\n", "\n").toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (inStyle) {
                if (isElementEnd(i, chars, "</style>")) {
                    inStyle = false;
                }
                if (chars[i] != ' ' && chars[i] != '\n') {
                    result.append(chars[i]);
                }
                continue;
            } else if (inScript) {
                if (isElementEnd(i, chars, "</script>")) {
                    inScript = false;
                }
                if (chars[i] != '\n') {
                    result.append(chars[i]);
                }
                continue;
            } else if (inTextArea) {
                if (isElementEnd(i, chars, "</textarea>")) {
                    inTextArea = false;
                }
                result.append(chars[i]);
                continue;
            }
 
            //判断是不是注释开始
            if (chars[i] == '"' && chars[i - 1] != '\\' && quot % 2 == 0) {//不是转义,不在单引号内的双引号
                quots++;
                result.append(chars[i]);
            } else if (chars[i] == '\'' && chars[i - 1] != '\\' && quots % 2 == 0) {//不是转义,不在双引号内的单引号
                quot++;
                result.append(chars[i]);
            } else if (quot % 2 == 0 && quots % 2 == 0) {//不在引号内
                // <script
                if (isElementBegin(i, chars, "<script>")) {
                    i = append(i, chars, result, '>');
                    inScript = true;
                }
                // <style
                else if (isElementBegin(i, chars, "<style>")) {
                    i = append(i, chars, result, '>');
                    inStyle = true;
                }
                // <textarea
                else if (isElementBegin(i, chars, "<textarea>")) {
                    i = append(i, chars, result, '>');
                    inTextArea = true;
                }
                //html标签内
                else if (chars[i] == '<') {
                    i = append(i, chars, result, '>');
                } else {
                    result.append(chars[i]);
                }
            } else {
                result.append(chars[i]);
            }
        }
        return result.toString();
    }
 
 
    /**
     * 去除代码中的注释
     *
     * @param code 代码
     * @return 代码
     */
    public static String removeComments(String code) {
        StringBuilder result = new StringBuilder();
 
        int quot = 0;//单引号
        int quots = 0;//双引号
        boolean jumpToNextLine = false;//跳过当前行
        boolean jumpToComment1 = false;//跳到注释类型1结束   /*  xxx */
        boolean jumpToComment2 = false;//跳到注释类型2结束   <!--  xxx -->
        char[] chars = code.replace("\r\n", "\n").toCharArray();
        for (int i = 0; i < chars.length; i++) {
            //跳过注释中的内容
            if (jumpToNextLine) {
                if (chars[i] == '\n') {
                    jumpToNextLine = false;
                    result.append(chars[i]);
                }
                continue;
            } else if (jumpToComment1) {
                if (isElementEnd(i, chars, "*/")) {
                    jumpToComment1 = false;
                }
                continue;
            } else if (jumpToComment2) {
                if (isElementEnd(i, chars, "-->")) {
                    jumpToComment2 = false;
                }
                continue;
            }
 
            //判断是不是注释开始
            if (chars[i] == '"' && chars[i - 1] != '\\' && quot % 2 == 0) {//不是转义,不在单引号内的双引号
                quots++;
                result.append(chars[i]);
            } else if (chars[i] == '\'' && chars[i - 1] != '\\' && quots % 2 == 0) {//不是转义,不在双引号内的单引号
                quot++;
                result.append(chars[i]);
            } else if (quot % 2 == 0 && quots % 2 == 0) {//不在引号内
                //双斜杠注释
                if (isElementBegin(i, chars, "// ")) {
                    jumpToNextLine = true;
                }
                // /*注释*/
                else if (isElementBegin(i, chars, "/* ")) {
                    jumpToComment1 = true;
                }
                // <!--注释-->
                else if (isElementBegin(i, chars, "<!-- ")) {
                    jumpToComment2 = true;
                } else {
                    result.append(chars[i]);
                }
            } else {
                result.append(chars[i]);
            }
        }
        return result.toString();
    }
 
    private static boolean isElementBegin(int index, char[] chars, String element) {
        element = element.toLowerCase();
        for (int i = 0; i < element.length() - 1; i++) {
            if (Character.toLowerCase(chars[index + i]) != element.charAt(i)) {
                return false;
            }
        }
        if (element.charAt(element.length() - 1) == ' ' || chars[index + element.length() - 1] == ' '
                || chars[index + element.length() - 1] == element.charAt(element.length() - 1)) {
            return true;
        }
        return false;
    }
 
    private static boolean isElementEnd(int index, char[] chars, String element) {
        element = element.toLowerCase();
        for (int i = index; i > index - element.length(); i--) {
            if (Character.toLowerCase(chars[i]) != element.charAt(element.length() - index + i - 1)) {
                return false;
            }
        }
        return true;
    }
 
    private static int append(int i, char[] chars, StringBuilder result, char endChar) {
        while (chars[i] != endChar) {
            chars[i] = chars[i] == '\n' ? ' ' : chars[i];
            if (chars[i] == ' ' && chars[i - 1] == ' ') {
                i++;
                continue;
            }
            result.append(chars[i++]);
        }
        result.append(chars[i]);
        return i;
    }
 
    /**
     * 移除空白行和行前空格
     *
     * @param code 代码
     * @return 代码
     */
    public static String removeBlankLine(String code) {
        StringBuilder result = new StringBuilder();
        String[] lines = code.split("\n");
        for (String line : lines) {
            line = line.trim();
            if (line.length() == 0) {
                continue;
            }
            result.append(line).append("\n");
        }
        return result.toString();
    }
 
 
}