1+ package com .xwintop .xJavaFxTool .services .assistTools ;
2+
3+ import com .xwintop .xJavaFxTool .controller .assistTools .DecompilerWxApkgToolController ;
4+ import com .xwintop .xcore .util .javafx .TooltipUtil ;
5+ import lombok .Data ;
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 .io .FilenameUtils ;
11+ import org .apache .commons .lang3 .StringUtils ;
12+
13+ import java .io .*;
14+
15+ /**
16+ * @ClassName: DecompilerWxApkgToolService
17+ * @Description: 微信小程序反编译工具
18+ * @author: xufeng
19+ * @date: 2018/7/4 14:44
20+ */
21+
22+ @ Getter
23+ @ Setter
24+ @ Slf4j
25+ public class DecompilerWxApkgToolService {
26+ private DecompilerWxApkgToolController decompilerWxApkgToolController ;
27+
28+ private DataInputStream in ;//in 流
29+ private WxFile wxFile ;//当前文件
30+ private WxFile currentWxFile ;//当前文件
31+ private int count ;//当前文件
32+
33+ public void decompileButtonAction () throws Exception {
34+ String filePath = decompilerWxApkgToolController .getPackageFileTextField ().getText ();
35+ if (StringUtils .isBlank (filePath )) {
36+ TooltipUtil .showToast ("原包路径未填写。" );
37+ return ;
38+ }
39+ String decompilePath = decompilerWxApkgToolController .getDecompilePathTextField ().getText ();
40+ if (StringUtils .isBlank (decompilePath )) {
41+ decompilePath = FilenameUtils .getFullPath (filePath ) + FilenameUtils .getBaseName (filePath );
42+ FileUtils .forceMkdir (new File (decompilePath + "\\ " ));
43+ }
44+ decompilePath = StringUtils .appendIfMissing (decompilePath , "/" , "/" , "\\ " );
45+ this .in = new DataInputStream (new FileInputStream (filePath ));
46+ decodeWxFile (this .in );
47+ while (currentWxFile != null ) {
48+ String name = currentWxFile .getName ();
49+ File file = new File (decompilePath , name );
50+ if (!file .getParentFile ().exists ()) {
51+ file .getParentFile ().mkdirs ();
52+ }
53+ OutputStream o = new FileOutputStream (file );
54+ this .readWxFile (o );
55+ o .close ();
56+ }
57+ if (in != null ) {
58+ in .close ();
59+ }
60+ TooltipUtil .showToast ("反编译成功,文件保存在:" + decompilePath + "目录下。" );
61+ }
62+
63+ public DecompilerWxApkgToolService (DecompilerWxApkgToolController decompilerWxApkgToolController ) {
64+ this .decompilerWxApkgToolController = decompilerWxApkgToolController ;
65+ }
66+
67+ /**
68+ * 解码文件名区域
69+ *
70+ * @param in in
71+ */
72+ private void decodeWxFile (DataInputStream in ) throws IOException {
73+ in .readByte (); //标识
74+ in .readInt (); //未知
75+ in .readInt (); //文件名域长度
76+ in .readInt (); //内容域长度
77+ in .readByte (); //未知
78+ count = in .readInt (); //文件数量
79+ if (count > 0 ) {
80+ WxFile nextWxFile = null ;
81+ for (int i = 0 ; i < count ; i ++) {
82+ byte [] name = new byte [in .readInt ()]; //文件名长度
83+ int j = in .read (name );
84+ int offset = in .readInt (); //偏移
85+ int length = in .readInt (); //内容长度
86+ WxFile currentWxFile = new WxFile ();
87+ currentWxFile .setName (new String (name , "UTF-8" ));
88+ currentWxFile .setOffset (offset );
89+ currentWxFile .setLength (length );
90+ if (nextWxFile == null ) {
91+ nextWxFile = currentWxFile ;
92+ wxFile = nextWxFile ;
93+ } else {
94+ nextWxFile .setNext (currentWxFile );
95+ nextWxFile = currentWxFile ;
96+ }
97+ }
98+ }
99+ currentWxFile = wxFile ; //当前处理的文件
100+ }
101+
102+ /**
103+ * @param out 输出流
104+ */
105+ public void readWxFile (OutputStream out ) throws IOException {
106+ if (currentWxFile == null ) {
107+ throw new IOException ("no more files" );
108+ }
109+ int length = currentWxFile .getLength ();
110+ byte [] buffer = new byte [length ];
111+ int readSize ;
112+ while (length > 0 && (readSize = in .read (buffer )) != -1 ) {
113+ length -= readSize ;
114+ out .write (buffer , 0 , readSize );
115+ }
116+ currentWxFile = currentWxFile .getNext ();
117+ }
118+
119+ @ Data
120+ public class WxFile {
121+ private String name ;//文件名称
122+ private int offset ;//偏移量
123+ private int length ;//文件长度
124+ private WxFile next ;//文件长度
125+ }
126+ }
0 commit comments