1+ package com .xwintop .xJavaFxTool .services .codeTools ;
2+
3+ import com .xwintop .xJavaFxTool .controller .codeTools .FileUnicodeTransformationToolController ;
4+ import com .xwintop .xJavaFxTool .utils .DirectoryTreeUtil ;
5+ import com .xwintop .xcore .util .javafx .TooltipUtil ;
6+ import lombok .Getter ;
7+ import lombok .Setter ;
8+ import lombok .extern .slf4j .Slf4j ;
9+ import org .apache .commons .io .FileUtils ;
10+ import org .apache .commons .lang3 .StringUtils ;
11+
12+ import java .io .File ;
13+ import java .nio .file .Files ;
14+ import java .nio .file .Path ;
15+ import java .util .Iterator ;
16+ import java .util .regex .Pattern ;
17+
18+ /**
19+ * @ClassName: FileUnicodeTransformationToolService
20+ * @Description: 文件编码转换工具
21+ * @author: xufeng
22+ * @date: 2019/10/9 15:00
23+ */
24+
25+ @ Getter
26+ @ Setter
27+ @ Slf4j
28+ public class FileUnicodeTransformationToolService {
29+ private FileUnicodeTransformationToolController fileUnicodeTransformationToolController ;
30+
31+ public FileUnicodeTransformationToolService (FileUnicodeTransformationToolController fileUnicodeTransformationToolController ) {
32+ this .fileUnicodeTransformationToolController = fileUnicodeTransformationToolController ;
33+ }
34+
35+ public void transformationAction () throws Exception {
36+ String watchPath = fileUnicodeTransformationToolController .getDetectPathTextField ().getText ();
37+ if (StringUtils .isEmpty (watchPath )) {
38+ TooltipUtil .showToast ("检测目录不能为空!" );
39+ return ;
40+ }
41+ File file = new File (watchPath );
42+ if (file .isDirectory ()) {
43+ Path path = file .toPath ();
44+ Iterator <Path > iterator = null ;
45+ if (fileUnicodeTransformationToolController .getIncludeSubdirectoryCheckBox ().isSelected ()) {
46+ iterator = Files .walk (path ).iterator ();
47+ } else {
48+ iterator = Files .newDirectoryStream (path ).iterator ();
49+ }
50+ boolean sRegex = fileUnicodeTransformationToolController .getFileNameSupportRegexCheckBox ().isSelected ();
51+ String fileNameContains = fileUnicodeTransformationToolController .getFileNameContainsTextField ().getText ();
52+ String fileNameNotContains = fileUnicodeTransformationToolController .getFileNameNotContainsTextField ().getText ();
53+ Pattern fileNameCsPattern = null ;
54+ Pattern fileNameNCsPattern = null ;
55+ if (sRegex ) {
56+ fileNameCsPattern = Pattern .compile (fileNameContains , Pattern .CASE_INSENSITIVE );
57+ fileNameNCsPattern = Pattern .compile (fileNameNotContains , Pattern .CASE_INSENSITIVE );
58+ }
59+ while (iterator .hasNext ()) {
60+ Path nextPath = iterator .next ();
61+ if (Files .isRegularFile (nextPath ) && DirectoryTreeUtil .ifMatchText (nextPath .getFileName ().toString (), fileNameContains , fileNameNotContains , sRegex , fileNameCsPattern , fileNameNCsPattern )) {
62+ File newFile = nextPath .toFile ();
63+ String showHideFile = fileUnicodeTransformationToolController .getShowHideFileChoice ().getValue ();
64+ if ("非隐藏" .equals (showHideFile ) && file .isHidden ()) {
65+ return ;
66+ } else if ("隐藏文件" .equals (showHideFile ) && !file .isHidden ()) {
67+ return ;
68+ }
69+ fileUnicodeTransformation (newFile );
70+ }
71+ }
72+ } else if (file .isFile ()) {
73+ fileUnicodeTransformation (file );
74+ }
75+ }
76+
77+ private void fileUnicodeTransformation (File file ) throws Exception {
78+ String oldFileUnicode = fileUnicodeTransformationToolController .getOldFileUnicodeComboBox ().getValue ();
79+ if ("自动检测" .equals (oldFileUnicode )) {
80+ oldFileUnicode = CharsetDetectToolService .detectFileCharset (file , 51200 );
81+ }
82+ String newFileUnicode = fileUnicodeTransformationToolController .getNewFileUnicodeComboBox ().getValue ();
83+ String newFilePath = fileUnicodeTransformationToolController .getNewFilePathTextField ().getText ();
84+ String fileConcel = FileUtils .readFileToString (file , oldFileUnicode );
85+ if (StringUtils .isNotEmpty (newFilePath )) {
86+ newFilePath = StringUtils .removeEnd (newFilePath , "/" );
87+ newFilePath = StringUtils .removeEnd (newFilePath , "\\ " );
88+ String watchPath = fileUnicodeTransformationToolController .getDetectPathTextField ().getText ();
89+ watchPath = StringUtils .removeEnd (watchPath , "/" );
90+ watchPath = StringUtils .removeEnd (watchPath , "\\ " );
91+ File watchPathFile = new File (watchPath );
92+ String subPath = "" ;
93+ if (watchPathFile .isDirectory ()) {
94+ subPath = file .getParent ().replace (watchPath , "" );
95+ }
96+ file = new File (newFilePath + "/" + subPath , file .getName ());
97+ }
98+ FileUtils .writeByteArrayToFile (file , fileConcel .getBytes (newFileUnicode ));
99+ fileUnicodeTransformationToolController .getResultTextArea ().appendText (file .getAbsolutePath () + " 转换完成\n " );
100+ }
101+ }
0 commit comments