1. package com.common.file;
2.
3. import java.io.File;
4. import java.io.FileInputStream;
5. import java.io.FileNotFoundException;
6. import java.io.FileOutputStream;
7. import java.io.IOException;
8. import java.io.InputStream;
9. import java.io.OutputStream;
10. import java.text.DateFormat;
11. import java.util.Date;
12. import java.util.Iterator;
13.
14. import javax.swing.text.html.HTMLDocument.HTMLReader.FormAction;
15.
16. /**
17. *
18. * 功能描述:
19. *
20. * @author Administrator
21. * @Date Jul 19, 2008
22. * @Time 9:46:11 AM
23. * @version 1.0
24. */
25. public class FileUtil {
26.
27. /**
28. * 功能描述:列出某文件夹及其子文件夹下面的文件,并可根据扩展名过滤
29. *
30. * @param path
31. * 文件夹
32. */
33. public static void list(File path) {
34. if (!path.exists()) {
35. "文件名称不存在!");
36. else {
37. if (path.isFile()) {
38. if (path.getName().toLowerCase().endsWith(".pdf")
39. ".doc")
40. ".chm")
41. ".html")
42. ".htm")) {// 文件格式
43. System.out.println(path);
44. System.out.println(path.getName());
45. }
46. else {
47. File[] files = path.listFiles();
48. for (int i = 0; i < files.length; i++) {
49. list(files[i]);
50. }
51. }
52. }
53. }
54.
55. /**
56. * 功能描述:拷贝一个目录或者文件到指定路径下,即把源文件拷贝到目标文件路径下
57. *
58. * @param source
59. * 源文件
60. * @param target
61. * 目标文件路径
62. * @return void
63. */
64. public static void copy(File source, File target) {
65. new File(target, source.getName());
66. if (source.isDirectory()) {
67. tarpath.mkdir();
68. File[] dir = source.listFiles();
69. for (int i = 0; i < dir.length; i++) {
70. copy(dir[i], tarpath);
71. }
72. else {
73. try {
74. new FileInputStream(source); // 用于读取文件的原始字节流
75. new FileOutputStream(tarpath); // 用于写入文件的原始字节的流
76. byte[] buf = new byte[1024];// 存储读取数据的缓冲区大小
77. int len = 0;
78. while ((len = is.read(buf)) != -1) {
79. 0, len);
80. }
81. is.close();
82. os.close();
83. catch (FileNotFoundException e) {
84. e.printStackTrace();
85. catch (IOException e) {
86. e.printStackTrace();
87. }
88. }
89. }
90.
91. /**
92. * @param args
93. */
94. public static void main(String[] args) {
95. // TODO Auto-generated method stub
96. new File("F:\\Tomcat");
97. list(file);
98. new Date();
99. DateFormat df = DateFormat.getDateInstance();
100. System.out.println(df.format(myDate));
101. }
102.
103. }
Java代码
1. package com.common.string;
2.
3. import java.util.ArrayList;
4. import java.util.LinkedHashSet;
5. import java.util.Set;
6. import java.util.regex.Matcher;
7. import java.util.regex.Pattern;
8.
9. /**
10. * 功能描述:关于字符串的一些实用操作
11. *
12. * @author Administrator
13. * @Date Jul 18, 2008
14. * @Time 2:19:47 PM
15. * @version 1.0
16. */
17. public class StringUtil {
18.
19. /**
20. * 功能描述:分割字符串
21. *
22. * @param str
23. * String 原始字符串
24. * @param splitsign
25. * String 分隔符
26. * @return String[] 分割后的字符串数组
27. */
28. @SuppressWarnings("unchecked")
29. public static String[] split(String str, String splitsign) {
30. int index;
31. if (str == null || splitsign == null) {
32. return null;
33. }
34. new ArrayList();
35. while ((index = str.indexOf(splitsign)) != -1) {
36. 0, index));
37. str = str.substring(index + splitsign.length());
38. }
39. al.add(str);
40. return (String[]) al.toArray(new String[0]);
41. }
42.
43. /**
44. * 功能描述:替换字符串
45. *
46. * @param from
47. * String 原始字符串
48. * @param to
49. * String 目标字符串
50. * @param source
51. * String 母字符串
52. * @return String 替换后的字符串
53. */
54. public static String replace(String from, String to, String source) {
55. if (source == null || from == null || to == null)
56. return null;
57. new StringBuffer("");
58. int index = -1;
59. while ((index = source.indexOf(from)) != -1) {
60. 0, index) + to);
61. source = source.substring(index + from.length());
62. index = source.indexOf(from);
63. }
64. str.append(source);
65. return str.toString();
66. }
67.
68. /**
69. * 替换字符串,能能够在HTML页面上直接显示(替换双引号和小于号)
70. *
71. * @param str
72. * String 原始字符串
73. * @return String 替换后的字符串
74. */
75. public static String htmlencode(String str) {
76. if (str == null) {
77. return null;
78. }
79. return replace("\"", """, replace("<", "<", str));
80. }
81.
82. /**
83. * 替换字符串,将被编码的转换成原始码(替换成双引号和小于号)
84. *
85. * @param str
86. * String
87. * @return String
88. */
89. public static String htmldecode(String str) {
90. if (str == null) {
91. return null;
92. }
93.
94. return replace(""", "\"", replace("<", "<", str));
95. }
96.
97. private static final String _BR = "<br/>";
98.
99. /**
100. * 功能描述:在页面上直接显示文本内容,替换小于号,空格,回车,TAB
101. *
102. * @param str
103. * String 原始字符串
104. * @return String 替换后的字符串
105. */
106. public static String htmlshow(String str) {
107. if (str == null) {
108. return null;
109. }
110.
111. "<", "<", str);
112. " ", " ", str);
113. "\r\n", _BR, str);
114. "\n", _BR, str);
115. "\t", " ", str);
116. return str;
117. }
118.
119. /**
120. * 功能描述:返回指定字节长度的字符串
121. *
122. * @param str
123. * String 字符串
124. * @param length
125. * int 指定长度
126. * @return String 返回的字符串
127. */
128. public static String toLength(String str, int length) {
129. if (str == null) {
130. return null;
131. }
132. if (length <= 0) {
133. return "";
134. }
135. try {
136. if (str.getBytes("GBK").length <= length) {
137. return str;
138. }
139. catch (Exception e) {
140. }
141. new StringBuffer();
142.
143. int index = 0;
144. char c;
145. 3;
146. while (length > 0) {
147. c = str.charAt(index);
148. if (c < 128) {
149. length--;
150. else {
151. length--;
152. length--;
153. }
154. buff.append(c);
155. index++;
156. }
157. "...");
158. return buff.toString();
159. }
160.
161. /**
162. * 功能描述:判断是否为整数
163. *
164. * @param str
165. * 传入的字符串
166. * @return 是整数返回true,否则返回false
167. */
168. public static boolean isInteger(String str) {
169. "^[-\\+]?[\\d]+$");
170. return pattern.matcher(str).matches();
171. }
172.
173. /**
174. * 判断是否为浮点数,包括double和float
175. *
176. * @param str
177. * 传入的字符串
178. * @return 是浮点数返回true,否则返回false
179. */
180. public static boolean isDouble(String str) {
181. "^[-\\+]?\\d+\\.\\d+$");
182. return pattern.matcher(str).matches();
183. }
184.
185. /**
186. * 判断是不是合法字符 c 要判断的字符
187. */
188. public static boolean isLetter(String str) {
189. if (str == null || str.length() < 0) {
190. return false;
191. }
192. "[\\w\\.-_]*");
193. return pattern.matcher(str).matches();
194. }
195.
196. /**
197. * 从指定的字符串中提取Email content 指定的字符串
198. *
199. * @param content
200. * @return
201. */
202. public static String parse(String content) {
203. null;
204. if (content == null || content.length() < 1) {
205. return email;
206. }
207. // 找出含有@
208. int beginPos;
209. int i;
210. "@";
211. "";
212. "";
213.
214. beginPos = content.indexOf(token);
215. if (beginPos > -1) {
216. // 前项扫描
217. null;
218. i = beginPos;
219. while (i > 0) {
220. 1, i);
221. if (isLetter(s))
222. preHalf = s + preHalf;
223. else
224. break;
225. i--;
226. }
227. // 后项扫描
228. 1;
229. while (i < content.length()) {
230. 1);
231. if (isLetter(s))
232. sufHalf = sufHalf + s;
233. else
234. break;
235. i++;
236. }
237. // 判断合法性
238. "@" + sufHalf;
239. if (isEmail(email)) {
240. return email;
241. }
242. }
243. return null;
244. }
245.
246. /**
247. * 功能描述:判断输入的字符串是否符合Email样式.
248. *
249. * @param str
250. * 传入的字符串
251. * @return 是Email样式返回true,否则返回false
252. */
253. public static boolean isEmail(String email) {
254. if (email == null || email.length() < 1 || email.length() > 256) {
255. return false;
256. }
257. Pattern pattern = Pattern
258. "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
259. return pattern.matcher(email).matches();
260. }
261.
262. /**
263. * 功能描述:判断输入的字符串是否为纯汉字
264. *
265. * @param str
266. * 传入的字符窜
267. * @return 如果是纯汉字返回true,否则返回false
268. */
269. public static boolean isChinese(String str) {
270. "[\u0391-\uFFE5]+$");
271. return pattern.matcher(str).matches();
272. }
273.
274. /**
275. * 功能描述:是否为空白,包括null和""
276. *
277. * @param str
278. * @return
279. */
280. public static boolean isBlank(String str) {
281. return str == null || str.trim().length() == 0;
282. }
283.
284. /**
285. * 功能描述:判断是否为质数
286. *
287. * @param x
288. * @return
289. */
290. public static boolean isPrime(int x) {
291. if (x <= 7) {
292. if (x == 2 || x == 3 || x == 5 || x == 7)
293. return true;
294. }
295. int c = 7;
296. if (x % 2 == 0)
297. return false;
298. if (x % 3 == 0)
299. return false;
300. if (x % 5 == 0)
301. return false;
302. int end = (int) Math.sqrt(x);
303. while (c <= end) {
304. if (x % c == 0) {
305. return false;
306. }
307. 4;
308. if (x % c == 0) {
309. return false;
310. }
311. 2;
312. if (x % c == 0) {
313. return false;
314. }
315. 4;
316. if (x % c == 0) {
317. return false;
318. }
319. 2;
320. if (x % c == 0) {
321. return false;
322. }
323. 4;
324. if (x % c == 0) {
325. return false;
326. }
327. 6;
328. if (x % c == 0) {
329. return false;
330. }
331. 2;
332. if (x % c == 0) {
333. return false;
334. }
335. 6;
336. }
337. return true;
338. }
339.
340. /**
341. * 功能描述:人民币转成大写
342. *
343. * @param str
344. * 数字字符串
345. * @return String 人民币转换成大写后的字符串
346. */
347. public static String hangeToBig(String str) {
348. double value;
349. try {
350. value = Double.parseDouble(str.trim());
351. catch (Exception e) {
352. return null;
353. }
354. char[] hunit = { '拾', '佰', '仟' }; // 段内位置表示
355. char[] vunit = { '万', '亿' }; // 段名表示
356. char[] digit = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' }; // 数字表示
357. long midVal = (long) (value * 100); // 转化成整形
358. // 转化成字符串
359.
360. 0, valStr.length() - 2); // 取整数部分
361. 2); // 取小数部分
362.
363. ""; // 整数部分转化的结果
364. ""; // 小数部分转化的结果
365. // 处理小数点后面的数
366. if (rail.equals("00")) { // 如果小数部分为0
367. "整";
368. else {
369. 0) - '0'] + "角"
370. 1) - '0'] + "分"; // 否则把角分转化出来
371. }
372. // 处理小数点前面的数
373. char[] chDig = head.toCharArray(); // 把整数部分转化成字符数组
374. char zero = '0'; // 标志'0'表示出现过0
375. byte zeroSerNum = 0; // 连续出现0的次数
376. for (int i = 0; i < chDig.length; i++) { // 循环处理每个数字
377. int idx = (chDig.length - i - 1) % 4; // 取段内位置
378. int vidx = (chDig.length - i - 1) / 4; // 取段位置
379. if (chDig[i] == '0') { // 如果当前字符是0
380. // 连续0次数递增
381. if (zero == '0') { // 标志
382. 0];
383. else if (idx == 0 && vidx > 0 && zeroSerNum < 4) {
384. 1];
385. '0';
386. }
387. continue;
388. }
389. 0; // 连续0次数清零
390. if (zero != '0') { // 如果标志不为0,则加上,例如万,亿什么的
391. prefix += zero;
392. '0';
393. }
394. '0']; // 转化该数字表示
395. if (idx > 0)
396. 1];
397. if (idx == 0 && vidx > 0) {
398. 1]; // 段结束位置应该加上段名如万,亿
399. }
400. }
401.
402. if (prefix.length() > 0)
403. '圆'; // 如果整数部分存在,则有圆的字样
404. return prefix + suffix; // 返回正确表示
405. }
406.
407. /**
408. * 功能描述:去掉字符串中重复的子字符串
409. *
410. * @param str
411. * 原字符串,如果有子字符串则用空格隔开以表示子字符串
412. * @return String 返回去掉重复子字符串后的字符串
413. */
414. @SuppressWarnings("unused")
415. private static String removeSameString(String str) {
416. new LinkedHashSet<String>();// set集合的特征:其子集不可以重复
417. " ");// 根据空格(正则表达式)分割字符串
418. new StringBuffer();
419.
420. for (int i = 0; i < strArray.length; i++) {
421. if (!mLinkedSet.contains(strArray[i])) {
422. mLinkedSet.add(strArray[i]);
423. " ");
424. }
425. }
426. System.out.println(mLinkedSet);
427. return sb.toString();
428. }
429.
430. /**
431. * 功能描述:过滤特殊字符
432. *
433. * @param src
434. * @return
435. */
436. public static String encoding(String src) {
437. if (src == null)
438. return "";
439. new StringBuilder();
440. if (src != null) {
441. src = src.trim();
442. for (int pos = 0; pos < src.length(); pos++) {
443. switch (src.charAt(pos)) {
444. case '\"':
445. """);
446. break;
447. case '<':
448. "<");
449. break;
450. case '>':
451. ">");
452. break;
453. case '\'':
454. "'");
455. break;
456. case '&':
457. "&");
458. break;
459. case '%':
460. "&pc;");
461. break;
462. case '_':
463. "&ul;");
464. break;
465. case '#':
466. "&shap;");
467. break;
468. case '?':
469. "&ques;");
470. break;
471. default:
472. result.append(src.charAt(pos));
473. break;
474. }
475. }
476. }
477. return result.toString();
478. }
479.
480. /**
481. * 功能描述:判断是不是合法的手机号码
482. *
483. * @param handset
484. * @return boolean
485. */
486. public static boolean isHandset(String handset) {
487. try {
488. "^1[\\d]{10}$";
489. Pattern pattern = Pattern.compile(regex);
490. Matcher matcher = pattern.matcher(handset);
491. return matcher.matches();
492.
493. catch (RuntimeException e) {
494. return false;
495. }
496. }
497.
498. /**
499. * 功能描述:反过滤特殊字符
500. *
501. * @param src
502. * @return
503. */
504. public static String decoding(String src) {
505. if (src == null)
506. return "";
507. String result = src;
508. """, "\"").replace("'", "\'");
509. "<", "<").replace(">", ">");
510. "&", "&");
511. "&pc;", "%").replace("&ul", "_");
512. "&shap;", "#").replace("&ques", "?");
513. return result;
514. }
515.
516. /**
517. * @param args
518. */
519. public static void main(String[] args) {
520. "abcdefgabcdefgabcdefgabcdefgabcdefgabcdefg";
521. "efg";
522. "房贺威";
523. "在字符串source中,用to替换from,替换结果为:"
524. + replace(from, to, source));
525. "返回指定字节长度的字符串:"
526. "abcdefgabcdefgabcdefgabcdefgabcdefgabcdefg", 9));
527. "判断是否为整数:" + isInteger("+0"));
528. "判断是否为浮点数,包括double和float:" + isDouble("+0.36"));
529. "判断输入的字符串是否符合Email样式:" +
530. "fhwbj@"));
531. "判断输入的字符串是否为纯汉字:" + isChinese("你好!"));
532. "判断输入的数据是否是质数:" + isPrime(12));
533. "人民币转换成大写:" + hangeToBig("10019658"));
534. "去掉字符串中重复的子字符串:" + removeSameString("100 100 9658"));
535. "过滤特殊字符:" + encoding("100\"s<>fdsd100 9658"));
536. "判断是不是合法的手机号码:" + isHandset("15981807340"));
537. "从字符串中取值Email:" + parse("159818 fwhbj@07340"));
538. }
539. }
Java代码
1. package com.common.time;
2.
3. import java.text.DateFormat;
4. import java.text.SimpleDateFormat;
5. import java.util.Calendar;
6. import java.util.Date;
7.
8. /**
9. *
10. * 功能描述:
11. *
12. * @author Administrator
13. * @Date Jul 19, 2008
14. * @Time 9:47:53 AM
15. * @version 1.0
16. */
17. public class DateUtil {
18.
19. public static Date date = null;
20.
21. public static DateFormat dateFormat = null;
22.
23. public static Calendar calendar = null;
24.
25. /**
26. * 功能描述:格式化日期
27. *
28. * @param dateStr
29. * String 字符型日期
30. * @param format
31. * String 格式
32. * @return Date 日期
33. */
34. public static Date parseDate(String dateStr, String format) {
35. try {
36. new SimpleDateFormat(format);
37. "-", "/");
38. if ((!dt.equals("")) && (dt.length() < format.length())) {
39. "[YyMmDdHhSs]",
40. "0");
41. }
42. date = (Date) dateFormat.parse(dt);
43. catch (Exception e) {
44. }
45. return date;
46. }
47.
48. /**
49. * 功能描述:格式化日期
50. *
51. * @param dateStr
52. * String 字符型日期:YYYY-MM-DD 格式
53. * @return Date
54. */
55. public static Date parseDate(String dateStr) {
56. return parseDate(dateStr, "yyyy/MM/dd");
57. }
58.
59. /**
60. * 功能描述:格式化输出日期
61. *
62. * @param date
63. * Date 日期
64. * @param format
65. * String 格式
66. * @return 返回字符型日期
67. */
68. public static String format(Date date, String format) {
69. "";
70. try {
71. if (date != null) {
72. new SimpleDateFormat(format);
73. result = dateFormat.format(date);
74. }
75. catch (Exception e) {
76. }
77. return result;
78. }
79.
80. /**
81. * 功能描述:
82. *
83. * @param date
84. * Date 日期
85. * @return
86. */
87. public static String format(Date date) {
88. return format(date, "yyyy/MM/dd");
89. }
90.
91. /**
92. * 功能描述:返回年份
93. *
94. * @param date
95. * Date 日期
96. * @return 返回年份
97. */
98. public static int getYear(Date date) {
99. calendar = Calendar.getInstance();
100. calendar.setTime(date);
101. return calendar.get(Calendar.YEAR);
102. }
103.
104. /**
105. * 功能描述:返回月份
106. *
107. * @param date
108. * Date 日期
109. * @return 返回月份
110. */
111. public static int getMonth(Date date) {
112. calendar = Calendar.getInstance();
113. calendar.setTime(date);
114. return calendar.get(Calendar.MONTH) + 1;
115. }
116.
117. /**
118. * 功能描述:返回日份
119. *
120. * @param date
121. * Date 日期
122. * @return 返回日份
123. */
124. public static int getDay(Date date) {
125. calendar = Calendar.getInstance();
126. calendar.setTime(date);
127. return calendar.get(Calendar.DAY_OF_MONTH);
128. }
129.
130. /**
131. * 功能描述:返回小时
132. *
133. * @param date
134. * 日期
135. * @return 返回小时
136. */
137. public static int getHour(Date date) {
138. calendar = Calendar.getInstance();
139. calendar.setTime(date);
140. return calendar.get(Calendar.HOUR_OF_DAY);
141. }
142.
143. /**
144. * 功能描述:返回分钟
145. *
146. * @param date
147. * 日期
148. * @return 返回分钟
149. */
150. public static int getMinute(Date date) {
151. calendar = Calendar.getInstance();
152. calendar.setTime(date);
153. return calendar.get(Calendar.MINUTE);
154. }
155.
156. /**
157. * 返回秒钟
158. *
159. * @param date
160. * Date 日期
161. * @return 返回秒钟
162. */
163. public static int getSecond(Date date) {
164. calendar = Calendar.getInstance();
165. calendar.setTime(date);
166. return calendar.get(Calendar.SECOND);
167. }
168.
169. /**
170. * 功能描述:返回毫秒
171. *
172. * @param date
173. * 日期
174. * @return 返回毫秒
175. */
176. public static long getMillis(Date date) {
177. calendar = Calendar.getInstance();
178. calendar.setTime(date);
179. return calendar.getTimeInMillis();
180. }
181.
182. /**
183. * 功能描述:返回字符型日期
184. *
185. * @param date
186. * 日期
187. * @return 返回字符型日期 yyyy/MM/dd 格式
188. */
189. public static String getDate(Date date) {
190. return format(date, "yyyy/MM/dd");
191. }
192.
193. /**
194. * 功能描述:返回字符型时间
195. *
196. * @param date
197. * Date 日期
198. * @return 返回字符型时间 HH:mm:ss 格式
199. */
200. public static String getTime(Date date) {
201. return format(date, "HH:mm:ss");
202. }
203.
204. /**
205. * 功能描述:返回字符型日期时间
206. *
207. * @param date
208. * Date 日期
209. * @return 返回字符型日期时间 yyyy/MM/dd HH:mm:ss 格式
210. */
211. public static String getDateTime(Date date) {
212. return format(date, "yyyy/MM/dd HH:mm:ss");
213. }
214.
215. /**
216. * 功能描述:日期相加
217. *
218. * @param date
219. * Date 日期
220. * @param day
221. * int 天数
222. * @return 返回相加后的日期
223. */
224. public static Date addDate(Date date, int day) {
225. calendar = Calendar.getInstance();
226. long millis = getMillis(date) + ((long) day) * 24 * 3600 * 1000;
227. calendar.setTimeInMillis(millis);
228. return calendar.getTime();
229. }
230.
231. /**
232. * 功能描述:日期相减
233. *
234. * @param date
235. * Date 日期
236. * @param date1
237. * Date 日期
238. * @return 返回相减后的日期
239. */
240. public static int diffDate(Date date, Date date1) {
241. return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000));
242. }
243.
244. /**
245. * 功能描述:取得指定月份的第一天
246. *
247. * @param strdate
248. * String 字符型日期
249. * @return String yyyy-MM-dd 格式
250. */
251. public static String getMonthBegin(String strdate) {
252. date = parseDate(strdate);
253. return format(date, "yyyy-MM") + "-01";
254. }
255.
256. /**
257. * 功能描述:取得指定月份的最后一天
258. *
259. * @param strdate
260. * String 字符型日期
261. * @return String 日期字符串 yyyy-MM-dd格式
262. */
263. public static String getMonthEnd(String strdate) {
264. date = parseDate(getMonthBegin(strdate));
265. calendar = Calendar.getInstance();
266. calendar.setTime(date);
267. 1);
268. 1);
269. return formatDate(calendar.getTime());
270. }
271.
272. /**
273. * 功能描述:常用的格式化日期
274. *
275. * @param date
276. * Date 日期
277. * @return String 日期字符串 yyyy-MM-dd格式
278. */
279. public static String formatDate(Date date) {
280. return formatDateByFormat(date, "yyyy-MM-dd");
281. }
282.
283. /**
284. * 功能描述:以指定的格式来格式化日期
285. *
286. * @param date
287. * Date 日期
288. * @param format
289. * String 格式
290. * @return String 日期字符串
291. */
292. public static String formatDateByFormat(Date date, String format) {
293. "";
294. if (date != null) {
295. try {
296. new SimpleDateFormat(format);
297. result = sdf.format(date);
298. catch (Exception ex) {
299. ex.printStackTrace();
300. }
301. }
302. return result;
303. }
304.
305. public static void main(String[] args) {
306. new Date();
307. System.out.println(d.toString());
308. System.out.println(formatDate(d).toString());
309. System.out.println(getMonthBegin(formatDate(d).toString()));
310. "2008/07/19"));
311. "2008/07/19"));
312. 15).toString());
313. }
314.
315. }
Java代码
1. /*[java]一个压缩工具类 zip2008年05月28日 星期三 17:48/*
2. * Zip.java
3. *
4. * Created on 2008年4月2日, 下午2:20
5. *
6. * To change this template, choose Tools | Template Manager
7. * and open the template in the editor.
8. */
9.
10. package com.founder.mnp.utl;
11.
12. /**
13. *
14. * @author GuoJiale
15. */
16. import java.io.BufferedInputStream;
17. import java.io.BufferedOutputStream;
18. import java.io.File;
19. import java.io.FileInputStream;
20. import java.io.FileOutputStream;
21. import java.util.zip.ZipEntry;
22. import java.util.zip.ZipOutputStream;
23.
24.
25. public class ZipUtils {
26. static final int BUFFER = 2048;
27.
28. public static boolean zip( String[] filename ,String outname){
29.
30. boolean test = true;
31. try {
32. null;
33. new FileOutputStream(outname);
34. new ZipOutputStream(new BufferedOutputStream(
35. dest));
36. byte data[] = new byte[BUFFER];
37. // File f= new File("d:\\111\\");
38. // File files[] = f.listFiles();
39.
40. for (int i = 0; i < filename.length; i++) {
41. new File(filename[i]);
42. new FileInputStream(file);
43. new BufferedInputStream(fi, BUFFER);
44. new ZipEntry(file.getName());
45. out.putNextEntry(entry);
46. int count;
47. while ((count = origin.read(data, 0, BUFFER)) != -1) {
48. 0, count);
49. }
50. origin.close();
51. }
52. out.close();
53. catch (Exception e) {
54. false;
55. e.printStackTrace();
56. }
57. return test;
58. }
59.
60.
61.
62. public static void main(String argv[]) {
63. // File f= new File("d:\\111\\");
64. new String[]{"F:\\12.jpg"};
65. "F:/12.zip");
66. }
67. }
68.
Java代码
1. package com.test.test;
2.
3. /************************************************
4. MD5 算法的Java Bean
5. Last Modified:10,Mar,2001
6. *************************************************/
7. import java.io.File;
8. import java.io.FileInputStream;
9. import java.io.FileNotFoundException;
10. import java.io.IOException;
11. import java.math.BigInteger;
12. import java.security.MessageDigest;
13. import java.util.HashMap;
14. import java.util.Map;
15. import java.util.zip.CRC32;
16. import java.util.zip.CheckedInputStream;
17.
18. /*************************************************
19. * md5 类实现了RSA Data Security, Inc.在提交给IETF 的RFC1321中的MD5 message-digest 算法。
20. *************************************************/
21. public class MD5 {
22. /*
23. * 下面这些S11-S44实际上是一个44的矩阵,在原始的C实现中是用#define 实现的, 这里把它们实现成为static
24. * final是表示了只读,切能在同一个进程空间内的多个 Instance间共享
25. */
26. private static final int S11 = 7;
27. private static final int S12 = 12;
28. private static final int S13 = 17;
29. private static final int S14 = 22;
30. private static final int S21 = 5;
31. private static final int S22 = 9;
32. private static final int S23 = 14;
33. private static final int S24 = 20;
34. private static final int S31 = 4;
35. private static final int S32 = 11;
36. private static final int S33 = 16;
37. private static final int S34 = 23;
38. private static final int S41 = 6;
39. private static final int S42 = 10;
40. private static final int S43 = 15;
41. private static final int S44 = 21;
42. private static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45. 0, 0, 0, 0, 0, 0, 0 };
46. /*
47. * 下面的三个成员是MD5计算过程中用到的3个核心数据,在原始的C实现中 被定义到MD5_CTX结构中
48. */
49. private long[] state = new long[4]; // state (ABCD)
50. private long[] count = new long[2]; // number of bits, modulo 2^64 (lsb
51. // first)
52. private byte[] buffer = new byte[64]; // input buffer
53. /*
54. * digestHexStr是MD5的唯一一个公共成员,是最新一次计算结果的 16进制ASCII表示.
55. */
56. private String digestHexStr;
57. /*
58. * digest,是最新一次计算结果的2进制内部表示,表示128bit的MD5值.
59. */
60. private byte[] digest = new byte[16];
61.
62. /**
63. * getMD5ofStr是类MD5最主要的公共方法,入口参数是你想要进行MD5变换的字符串
64. * 返回的是变换完的结果,这个结果是从公共成员digestHexStr取得的.
65. */
66. public String getMD5ofStr(String inbuf) {
67. md5Init();
68. md5Update(inbuf.getBytes(), inbuf.length());
69. md5Final();
70. "";
71. for (int i = 0; i < 16; i++) {
72. digestHexStr += byteHEX(digest[i]);
73. }
74. return digestHexStr;
75. }
76.
77. // 这是MD5这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数
78. public MD5() {
79. md5Init();
80. return;
81. }
82.
83. /* md5Init是一个初始化函数,初始化核心变量,装入标准的幻数 */
84. private void md5Init() {
85. 0] = 0L;
86. 1] = 0L;
87. // /* Load magic initialization constants.
88. 0] = 0x67452301L;
89. 1] = 0xefcdab89L;
90. 2] = 0x98badcfeL;
91. 3] = 0x10325476L;
92. return;
93. }
94.
95. /*
96. 4个基本的MD5函数,在原始的MD5的C实现中,由于它们是
97. private方法,名字保持了原来C中的。
98. */
99. private long F(long x, long y, long z) {
100. return (x & y) | ((~x) & z);
101. }
102.
103. private long G(long x, long y, long z) {
104. return (x & z) | (y & (~z));
105. }
106.
107. private long H(long x, long y, long z) {
108. return x ^ y ^ z;
109. }
110.
111. private long I(long x, long y, long z) {
112. return y ^ (x | (~z));
113. }
114.
115. /*
116. * Rotation is separate from addition to prevent recomputation.
117. */
118. private long FF(long a, long b, long c, long d, long x, long s, long ac) {
119. a += F(b, c, d) + x + ac;
120. int) a << s) | ((int) a >>> (32 - s));
121. a += b;
122. return a;
123. }
124.
125. private long GG(long a, long b, long c, long d, long x, long s, long ac) {
126. a += G(b, c, d) + x + ac;
127. int) a << s) | ((int) a >>> (32 - s));
128. a += b;
129. return a;
130. }
131.
132. private long HH(long a, long b, long c, long d, long x, long s, long ac) {
133. a += H(b, c, d) + x + ac;
134. int) a << s) | ((int) a >>> (32 - s));
135. a += b;
136. return a;
137. }
138.
139. private long II(long a, long b, long c, long d, long x, long s, long ac) {
140. a += I(b, c, d) + x + ac;
141. int) a << s) | ((int) a >>> (32 - s));
142. a += b;
143. return a;
144. }
145.
146. /*
147. * md5Update是MD5的主计算过程,inbuf是要变换的字节串,inputlen是长度,这个
148. * 函数由getMD5ofStr调用,调用之前需要调用md5init,因此把它设计成private的
149. */
150. private void md5Update(byte[] inbuf, int inputLen) {
151. int i, index, partLen;
152. byte[] block = new byte[64];
153. int) (count[0] >>> 3) & 0x3F;
154. // /* Update number of bits */
155. if ((count[0] += (inputLen << 3)) < (inputLen << 3))
156. 1]++;
157. 1] += (inputLen >>> 29);
158. 64 - index;
159. // Transform as many times as possible.
160. if (inputLen >= partLen) {
161. 0, partLen);
162. md5Transform(buffer);
163. for (i = partLen; i + 63 < inputLen; i += 64) {
164. 0, i, 64);
165. md5Transform(block);
166. }
167. 0;
168. else
169. 0;
170. // /* Buffer remaining input */
171. md5Memcpy(buffer, inbuf, index, i, inputLen - i);
172. }
173.
174. /*
175. * md5Final整理和填写输出结果
176. */
177. private void md5Final() {
178. byte[] bits = new byte[8];
179. int index, padLen;
180. // /* Save number of bits */
181. 8);
182. // /* Pad out to 56 mod 64.
183. int) (count[0] >>> 3) & 0x3f;
184. 56) ? (56 - index) : (120 - index);
185. md5Update(PADDING, padLen);
186. // /* Append length (before padding) */
187. 8);
188. // /* Store state in digest */
189. 16);
190. }
191.
192. /*
193. * md5Memcpy是一个内部使用的byte数组的块拷贝函数,从input的inpos开始把len长度的
194. * 字节拷贝到output的outpos位置开始
195. */
196. private void md5Memcpy(byte[] output, byte[] input, int outpos, int inpos,
197. int len) {
198. int i;
199. for (i = 0; i < len; i++)
200. output[outpos + i] = input[inpos + i];
201. }
202.
203. /*
204. * md5Transform是MD5核心变换程序,有md5Update调用,block是分块的原始字节
205. */
206. private void md5Transform(byte block[]) {
207. long a = state[0], b = state[1], c = state[2], d = state[3];
208. long[] x = new long[16];
209. 64);
210.
211. /* Round 1 */
212. 0], S11, 0xd76aa478L); /* 1 */
213. 1], S12, 0xe8c7b756L); /* 2 */
214. 2], S13, 0x242070dbL); /* 3 */
215. 3], S14, 0xc1bdceeeL); /* 4 */
216. 4], S11, 0xf57c0fafL); /* 5 */
217. 5], S12, 0x4787c62aL); /* 6 */
218. 6], S13, 0xa8304613L); /* 7 */
219. 7], S14, 0xfd469501L); /* 8 */
220. 8], S11, 0x698098d8L); /* 9 */
221. 9], S12, 0x8b44f7afL); /* 10 */
222. 10], S13, 0xffff5bb1L); /* 11 */
223. 11], S14, 0x895cd7beL); /* 12 */
224. 12], S11, 0x6b901122L); /* 13 */
225. 13], S12, 0xfd987193L); /* 14 */
226. 14], S13, 0xa679438eL); /* 15 */
227. 15], S14, 0x49b40821L); /* 16 */
228. /* Round 2 */
229. 1], S21, 0xf61e2562L); /* 17 */
230. 6], S22, 0xc040b340L); /* 18 */
231. 11], S23, 0x265e5a51L); /* 19 */
232. 0], S24, 0xe9b6c7aaL); /* 20 */
233. 5], S21, 0xd62f105dL); /* 21 */
234. 10], S22, 0x2441453L); /* 22 */
235. 15], S23, 0xd8a1e681L); /* 23 */
236. 4], S24, 0xe7d3fbc8L); /* 24 */
237. 9], S21, 0x21e1cde6L); /* 25 */
238. 14], S22, 0xc33707d6L); /* 26 */
239. 3], S23, 0xf4d50d87L); /* 27 */
240. 8], S24, 0x455a14edL); /* 28 */
241. 13], S21, 0xa9e3e905L); /* 29 */
242. 2], S22, 0xfcefa3f8L); /* 30 */
243. 7], S23, 0x676f02d9L); /* 31 */
244. 12], S24, 0x8d2a4c8aL); /* 32 */
245. /* Round 3 */
246. 5], S31, 0xfffa3942L); /* 33 */
247. 8], S32, 0x8771f681L); /* 34 */
248. 11], S33, 0x6d9d6122L); /* 35 */
249. 14], S34, 0xfde5380cL); /* 36 */
250. 1], S31, 0xa4beea44L); /* 37 */
251. 4], S32, 0x4bdecfa9L); /* 38 */
252. 7], S33, 0xf6bb4b60L); /* 39 */
253. 10], S34, 0xbebfbc70L); /* 40 */
254. 13], S31, 0x289b7ec6L); /* 41 */
255. 0], S32, 0xeaa127faL); /* 42 */
256. 3], S33, 0xd4ef3085L); /* 43 */
257. 6], S34, 0x4881d05L); /* 44 */
258. 9], S31, 0xd9d4d039L); /* 45 */
259. 12], S32, 0xe6db99e5L); /* 46 */
260. 15], S33, 0x1fa27cf8L); /* 47 */
261. 2], S34, 0xc4ac5665L); /* 48 */
262. /* Round 4 */
263. 0], S41, 0xf4292244L); /* 49 */
264. 7], S42, 0x432aff97L); /* 50 */
265. 14], S43, 0xab9423a7L); /* 51 */
266. 5], S44, 0xfc93a039L); /* 52 */
267. 12], S41, 0x655b59c3L); /* 53 */
268. 3], S42, 0x8f0ccc92L); /* 54 */
269. 10], S43, 0xffeff47dL); /* 55 */
270. 1], S44, 0x85845dd1L); /* 56 */
271. 8], S41, 0x6fa87e4fL); /* 57 */
272. 15], S42, 0xfe2ce6e0L); /* 58 */
273. 6], S43, 0xa3014314L); /* 59 */
274. 13], S44, 0x4e0811a1L); /* 60 */
275. 4], S41, 0xf7537e82L); /* 61 */
276. 11], S42, 0xbd3af235L); /* 62 */
277. 2], S43, 0x2ad7d2bbL); /* 63 */
278. 9], S44, 0xeb86d391L); /* 64 */
279. 0] += a;
280. 1] += b;
281. 2] += c;
282. 3] += d;
283. }
284.
285. /*
286. * Encode把long数组按顺序拆成byte数组,因为java的long类型是64bit的, 只拆低32bit,以适应原始C实现的用途
287. */
288. private void Encode(byte[] output, long[] input, int len) {
289. int i, j;
290. for (i = 0, j = 0; j < len; i++, j += 4) {
291. byte) (input[i] & 0xffL);
292. 1] = (byte) ((input[i] >>> 8) & 0xffL);
293. 2] = (byte) ((input[i] >>> 16) & 0xffL);
294. 3] = (byte) ((input[i] >>> 24) & 0xffL);
295. }
296. }
297.
298. /*
299. * Decode把byte数组按顺序合成成long数组,因为java的long类型是64bit的,
300. * 只合成低32bit,高32bit清零,以适应原始C实现的用途
301. */
302. private void Decode(long[] output, byte[] input, int len) {
303. int i, j;
304. for (i = 0, j = 0; j < len; i++, j += 4)
305. 1]) << 8)
306. 2]) << 16) | (b2iu(input[j + 3]) << 24);
307. return;
308. }
309.
310. /**
311. * b2iu是我写的一个把byte按照不考虑正负号的原则的"升位"程序,因为java没有unsigned运算
312. */
313. public static long b2iu(byte b) {
314. return b < 0 ? b & 0x7F + 128 : b;
315. }
316.
317. /**
318. * byteHEX(),用来把一个byte类型的数转换成十六进制的ASCII表示,
319. * 因为java中的byte的toString无法实现这一点,我们又没有C语言中的 sprintf(outbuf,"%02X",ib)
320. */
321. public static String byteHEX(byte ib) {
322. char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
323. 'B', 'C', 'D', 'E', 'F' };
324. char[] ob = new char[2];
325. 0] = Digit[(ib >>> 4) & 0X0F];
326. 1] = Digit[ib & 0X0F];
327. new String(ob);
328. return s;
329. }
330.
331.
332. //-----------------------------------------------------------
333.
334. /**
335. * 获取单个文件的MD5值!
336. * @param file
337. * @return
338. */
339. public static String getFileMD5(File file) {
340. if (!file.isFile()) {
341. return null;
342. }
343. null;
344. null;
345. byte buffer[] = new byte[1024];
346. int len;
347. try {
348. "MD5");
349. new FileInputStream(file);
350. while ((len = in.read(buffer, 0, 1024)) != -1) {
351. 0, len);
352. }
353. in.close();
354. catch (Exception e) {
355. e.printStackTrace();
356. return null;
357. }
358. new BigInteger(1, digest.digest());
359. return bigInt.toString(16).toUpperCase();
360. }
361.
362. /**
363. * 获取单个文件的SHA1值!
364. *
365. * @param file
366. * @return
367. */
368. public static String getFileSHA1(File file) {
369. if (!file.isFile()) {
370. return null;
371. }
372. null;
373. null;
374. byte buffer[] = new byte[1024];
375. int len;
376. try {
377. "SHA1");
378. new FileInputStream(file);
379. while ((len = in.read(buffer, 0, 1024)) != -1) {
380. 0, len);
381. }
382. in.close();
383. catch (Exception e) {
384. e.printStackTrace();
385. return null;
386. }
387. new BigInteger(1, digest.digest());
388. return bigInt.toString(16).toUpperCase();
389. }
390.
391. /**
392. * 获取单个文件的CRC32值!
393. * @param file
394. * @return
395. * @throws Exception
396. */
397. public static String getFileCRC32(File file) {
398. FileInputStream fileinputstream;
399. try {
400. new FileInputStream(file);
401. new CRC32();
402. for (CheckedInputStream checkedinputstream = new CheckedInputStream(
403. 1;) {
404. }
405. return Long.toHexString(crc32.getValue());
406. catch (FileNotFoundException e) {
407. e.printStackTrace();
408. catch (IOException e) {
409. e.printStackTrace();
410. }
411. return null;
412. }
413.
414.
415. /**
416. * 获取文件夹中文件的MD5值
417. *
418. * @param file
419. * @param listChild
420. * ;true递归子目录中的文件
421. * @return
422. */
423. public static Map<String, String> getDirMD5(File file, boolean listChild) {
424. if (!file.isDirectory()) {
425. return null;
426. }
427. // <filepath,md5>
428. new HashMap<String, String>();
429. String md5;
430. File files[] = file.listFiles();
431. for (int i = 0; i < files.length; i++) {
432. File f = files[i];
433. if (f.isDirectory() && listChild) {
434. map.putAll(getDirMD5(f, listChild));
435. else {
436. md5 = getFileMD5(f);
437. if (md5 != null) {
438. map.put(f.getPath(), md5);
439. }
440. }
441. }
442. return map;
443. }
444.
445. /**
446. * 获取文件夹中文件的SHA1值
447. *
448. * @param file
449. * @param listChild
450. * ;true递归子目录中的文件
451. * @return
452. */
453. public static Map<String, String> getDirSHA1(File file, boolean listChild) {
454. if (!file.isDirectory()) {
455. return null;
456. }
457. // <filepath,SHA1>
458. new HashMap<String, String>();
459. String sha1;
460. File files[] = file.listFiles();
461. for (int i = 0; i < files.length; i++) {
462. File f = files[i];
463. if (f.isDirectory() && listChild) {
464. map.putAll(getDirSHA1(f, listChild));
465. else {
466. sha1 = getFileSHA1(f);
467. if (sha1 != null) {
468. map.put(f.getPath(), sha1);
469. }
470. }
471. }
472. return map;
473. }
474.
475. /**
476. * 获取文件夹中文件的CRC32值
477. *
478. * @param file
479. * @param listChild
480. * ;true递归子目录中的文件
481. * @return
482. */
483. public static Map<String, String> getDirCRC32(File file, boolean listChild) {
484. if (!file.isDirectory()) {
485. return null;
486. }
487. // <filepath,CRC32>
488. new HashMap<String, String>();
489. String crc32;
490. File files[] = file.listFiles();
491. for (int i = 0; i < files.length; i++) {
492. File f = files[i];
493. if (f.isDirectory() && listChild) {
494. map.putAll(getDirCRC32(f, listChild));
495. else {
496. crc32 = getFileCRC32(f);
497. if (crc32 != null) {
498. map.put(f.getPath(), crc32);
499. }
500. }
501. }
502. return map;
503. }
504. }