2017移動(dòng)開發(fā)年終盤點(diǎn)
970
2025-03-31
文章目錄
一、拷貝 p7zip 源碼中的頭文件到 android studio 項(xiàng)目中
二、完整代碼示例
1、Java 層代碼
2、JNI 層代碼
3、日志頭文件
4、執(zhí)行結(jié)果
四、參考資料
前置博客 :
【Android 安裝包優(yōu)化】使用 lib7zr.so 動(dòng)態(tài)庫處理壓縮文件 ( 修改 7zr 交叉編譯腳本 Android.mk | 交叉編譯 lib7zr.so 動(dòng)態(tài)庫 )
【Android 安裝包優(yōu)化】使用 lib7zr.so 動(dòng)態(tài)庫處理壓縮文件 ( 拷貝 lib7zr.so 動(dòng)態(tài)庫到 android studio 工程 | 配置 build.gradle 構(gòu)建腳本 )
【Android 安裝包優(yōu)化】使用 lib7zr.so 動(dòng)態(tài)庫處理壓縮文件 ( 拷貝 lib7zr.so 動(dòng)態(tài)庫頭文件到 Android 工程中 | 配置 CMakeLists.txt 構(gòu)建腳本 )
一、拷貝 p7zip 源碼中的頭文件到 Android Studio 項(xiàng)目中
在上一篇博客 【Android 安裝包優(yōu)化】使用 lib7zr.so 動(dòng)態(tài)庫處理壓縮文件 ( 拷貝 lib7zr.so 動(dòng)態(tài)庫頭文件到 Android 工程中 | 配置 CMakeLists.txt 構(gòu)建腳本 ) 中 , 將 lib7zr.so 動(dòng)態(tài)庫需要的頭文件都拷貝到了 Android Studio 工程中 , 并配置了 CMakeLists.txt 構(gòu)建腳本 ;
本篇博客開發(fā) JNI 類 , 驗(yàn)證 lib7zr.so 動(dòng)態(tài)庫 ;
在 【Android 安裝包優(yōu)化】Android 中使用 7zr 可執(zhí)行程序 解壓縮文件 博客的 Android 項(xiàng)目的基礎(chǔ)上進(jìn)行開發(fā) ;
首先加載 libnative-lib.so 動(dòng)態(tài)庫 , 這是 CMakeLists.txt 編譯出來的動(dòng)態(tài)庫 , 聲明 native 方法 ;
class MainActivity : AppCompatActivity() { companion object { init { System.loadLibrary("native-lib") } } external fun executeCmd(cmd: String): Unit }
1
2
3
4
5
6
7
8
在 JNI 層的 C++ 文件中實(shí)現(xiàn)上述 native 方法 , MainActivity 類的包名是 kim.hsl.a7_zip , 因此 executeCmd 方法對應(yīng)的 JNI 層的方法是 Java_kim_hsl_a7_1zip_MainActivity_executeCmd ;
extern "C" JNIEXPORT void JNICALL Java_kim_hsl_a7_1zip_MainActivity_executeCmd(JNIEnv* env, jobject thiz, jstring cmd) { LOGI("7zTypes SZ_OK = %d", SZ_OK ); }
1
2
3
4
5
二、完整代碼示例
1、Java 層代碼
Java 層代碼 :
package kim.hsl.a7_zip import android.os.Build import android.os.Bundle import android.util.Log import androidx.appcompat.app.AppCompatActivity import java.io.* class MainActivity : AppCompatActivity() { companion object { val TAG = "MainActivity" init { System.loadLibrary("native-lib") } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) copy7zr() compress7z() uncompress7z() executeCmd("7z") } /** * 將 7zr 文件拷貝到應(yīng)用私有目錄 */ fun copy7zr() { Log.i(TAG, "開始拷貝 7zr 文件") // /data/user/0/kim.hsl.a7_zip/files/7zr var exeFile = File(filesDir, "7zr") Log.i(TAG, "filesDir = ${filesDir.absolutePath} , exeFile = ${exeFile.absolutePath}") // 查看該文件是否存在, 如果存在設(shè)置該文件可執(zhí)行 // 如果不存在 , 拷貝文件 if (exeFile.exists()) { exeFile.setExecutable(true) Log.i(TAG, "內(nèi)置存儲(chǔ)空間存在該 /data/user/0/kim.hsl.a7_zip/files/7zr 文件") return } else { Log.i(TAG, "內(nèi)置存儲(chǔ)空間不存在 7zr 可執(zhí)行文件 , 開始拷貝文件") } // 如果不存在 , 拷貝文件 var InputStream: InputStream = assets.open("libs/arm64-v8a/7zr") // /data/user/0/kim.hsl.a7_zip/files/7zr var fileOutputStream: FileOutputStream = FileOutputStream(exeFile) Log.i(TAG, "Build.CPU_ABI = ${Build.CPU_ABI}") // 不同 CPU 架構(gòu)拷貝不同的可執(zhí)行程序 if (Build.CPU_ABI.startsWith("armeabi-v7a")) { inputStream = assets.open("libs/armeabi-v7a/7zr") } else if (Build.CPU_ABI.startsWith("arm64-v8a")) { inputStream = assets.open("libs/arm64-v8a/7zr") } else if (Build.CPU_ABI.startsWith("x86")) { inputStream = assets.open("libs/x86/7zr") } else if (Build.CPU_ABI.startsWith("x86_64")) { inputStream = assets.open("libs/x86_64/7zr") } // 拷貝文件 var buffer: ByteArray = ByteArray(1024) var readCount = inputStream.read(buffer); while (readCount != -1) { fileOutputStream.write(buffer) readCount = inputStream.read(buffer); } fileOutputStream.flush() fileOutputStream.close() Log.i(TAG, "拷貝 7zr 文件結(jié)束") } /** * 使用 7zr 進(jìn)行壓縮 */ fun compress7z() { // /data/user/0/kim.hsl.a7_zip/files/7zr var exeFile = File(filesDir, "7zr") // 執(zhí)行前賦予可執(zhí)行權(quán)限 exeFile.setExecutable(true) var file_7z = File("${filesDir.absolutePath}/files.7z") if(file_7z.exists()){ file_7z.delete() } var cmd = "${exeFile.absolutePath} a ${filesDir.absolutePath}/files.7z ${filesDir.absolutePath} -mx=9 -t7z" Log.i(TAG, "壓縮命令 : $cmd") var process: Process = Runtime.getRuntime().exec(cmd) // 讀取命令執(zhí)行過程數(shù)據(jù) var reader = BufferedReader(InputStreamReader(process.inputStream)) while (true) { val line = reader.readLine() if (line != null) { Log.i(TAG, "$line") }else{ break } } val exitValue = process.exitValue() Log.i(TAG, "壓縮文件 , 執(zhí)行完畢 , exitValue = $exitValue") } /** * 判定命令是否執(zhí)行完畢 * 調(diào)用 process.exitValue 方法 , 如果沒有執(zhí)行完畢 , 會(huì)拋異常, * 如果執(zhí)行完畢會(huì)返回一個(gè)確定的值 */ fun isComplete(process: Process): Boolean { try { // 已經(jīng)執(zhí)行完畢 process.exitValue() return true } catch (e: IllegalThreadStateException) { // 未執(zhí)行完畢 return false } } /** * 使用 7zr 進(jìn)行解壓縮 */ fun uncompress7z() { // /data/user/0/kim.hsl.a7_zip/files/7zr var exeFile = File(filesDir, "7zr") // 執(zhí)行前賦予可執(zhí)行權(quán)限 exeFile.setExecutable(true) // 刪除解壓目錄 var unzip_file = File("${filesDir.absolutePath}/unzip_file") if(unzip_file.exists()){ recursionDeleteFile(unzip_file) } var cmd = "${exeFile.absolutePath} x ${filesDir.absolutePath}/files.7z -o${filesDir.absolutePath}/unzip_file" Log.i(TAG, "解壓縮命令 : $cmd") var process: Process = Runtime.getRuntime().exec(cmd) // 讀取命令執(zhí)行過程數(shù)據(jù) var reader = BufferedReader(InputStreamReader(process.inputStream)) while (true) { val line = reader.readLine() if (line != null) { Log.i(TAG, "$line") }else{ break } } val exitValue = process.exitValue() Log.i(TAG, "解壓縮文件 , 執(zhí)行完畢 , exitValue = $exitValue") } /** * 遞歸刪除文件 */ fun recursionDeleteFile(file: File) { if (file.isDirectory) { // 如果是目錄 , 則遞歸刪除 file.listFiles().forEach { // ForEach 循環(huán)刪除目錄 recursionDeleteFile(it) } } else { // 如果是文件直接刪除 file.delete() } } external fun executeCmd(cmd: String): Unit }
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
2、JNI 層代碼
#include
1
2
3
4
5
6
7
8
9
10
3、日志頭文件
日志打印頭文件 :
// // Created by octop on 2021/5/6. // #ifndef INC_7_ZIP_LOGGING_MACROS_H #define INC_7_ZIP_LOGGING_MACROS_H #include
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
4、執(zhí)行結(jié)果
執(zhí)行結(jié)果 :
2021-05-06 20:44:57.920 8966-8966/kim.hsl.a7_zip I/MainActivity: 開始拷貝 7zr 文件 2021-05-06 20:44:57.921 8966-8966/kim.hsl.a7_zip I/MainActivity: filesDir = /data/user/0/kim.hsl.a7_zip/files , exeFile = /data/user/0/kim.hsl.a7_zip/files/7zr 2021-05-06 20:44:57.924 8966-8966/kim.hsl.a7_zip I/MainActivity: 內(nèi)置存儲(chǔ)空間存在該 /data/user/0/kim.hsl.a7_zip/files/7zr 文件 2021-05-06 20:44:57.925 8966-8966/kim.hsl.a7_zip I/MainActivity: 壓縮命令 : /data/user/0/kim.hsl.a7_zip/files/7zr a /data/user/0/kim.hsl.a7_zip/files/files.7z /data/user/0/kim.hsl.a7_zip/files -mx=9 -t7z 2021-05-06 20:45:00.074 8966-8966/kim.hsl.a7_zip I/MainActivity: 7-Zip (a) [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21 2021-05-06 20:45:00.074 8966-8966/kim.hsl.a7_zip I/MainActivity: p7zip Version 16.02 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,8 CPUs LE) 2021-05-06 20:45:00.074 8966-8966/kim.hsl.a7_zip I/MainActivity: Scanning the drive: 2021-05-06 20:45:00.074 8966-8966/kim.hsl.a7_zip I/MainActivity: 13 folders, 6 files, 5965824 bytes (5826 KiB) 2021-05-06 20:45:00.074 8966-8966/kim.hsl.a7_zip I/MainActivity: Creating archive: /data/user/0/kim.hsl.a7_zip/files/files.7z 2021-05-06 20:45:00.075 8966-8966/kim.hsl.a7_zip I/MainActivity: Items to compress: 19 2021-05-06 20:45:00.075 8966-8966/kim.hsl.a7_zip I/MainActivity: Files read from disk: 6 2021-05-06 20:45:00.075 8966-8966/kim.hsl.a7_zip I/MainActivity: Archive size: 309075 bytes (302 KiB) 2021-05-06 20:45:00.075 8966-8966/kim.hsl.a7_zip I/MainActivity: Everything is Ok 2021-05-06 20:45:00.075 8966-8966/kim.hsl.a7_zip I/MainActivity: 壓縮文件 , 執(zhí)行完畢 , exitValue = 0 2021-05-06 20:45:00.078 8966-8966/kim.hsl.a7_zip I/MainActivity: 解壓縮命令 : /data/user/0/kim.hsl.a7_zip/files/7zr x /data/user/0/kim.hsl.a7_zip/files/files.7z -o/data/user/0/kim.hsl.a7_zip/files/unzip_file 2021-05-06 20:45:00.088 8966-8966/kim.hsl.a7_zip I/MainActivity: 7-Zip (a) [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21 2021-05-06 20:45:00.088 8966-8966/kim.hsl.a7_zip I/MainActivity: p7zip Version 16.02 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,8 CPUs LE) 2021-05-06 20:45:00.088 8966-8966/kim.hsl.a7_zip I/MainActivity: Scanning the drive for archives: 2021-05-06 20:45:00.088 8966-8966/kim.hsl.a7_zip I/MainActivity: 1 file, 309075 bytes (302 KiB) 2021-05-06 20:45:00.088 8966-8966/kim.hsl.a7_zip I/MainActivity: Extracting archive: /data/user/0/kim.hsl.a7_zip/files/files.7z 2021-05-06 20:45:00.135 8966-8966/kim.hsl.a7_zip I/MainActivity: -- 2021-05-06 20:45:00.135 8966-8966/kim.hsl.a7_zip I/MainActivity: Path = /data/user/0/kim.hsl.a7_zip/files/files.7z 2021-05-06 20:45:00.135 8966-8966/kim.hsl.a7_zip I/MainActivity: Type = 7z 2021-05-06 20:45:00.135 8966-8966/kim.hsl.a7_zip I/MainActivity: Physical Size = 309075 2021-05-06 20:45:00.135 8966-8966/kim.hsl.a7_zip I/MainActivity: Headers Size = 298 2021-05-06 20:45:00.135 8966-8966/kim.hsl.a7_zip I/MainActivity: Method = LZMA2:6m 2021-05-06 20:45:00.135 8966-8966/kim.hsl.a7_zip I/MainActivity: Solid = + 2021-05-06 20:45:00.135 8966-8966/kim.hsl.a7_zip I/MainActivity: Blocks = 1 2021-05-06 20:45:00.135 8966-8966/kim.hsl.a7_zip I/MainActivity: Everything is Ok 2021-05-06 20:45:00.135 8966-8966/kim.hsl.a7_zip I/MainActivity: Folders: 13 2021-05-06 20:45:00.135 8966-8966/kim.hsl.a7_zip I/MainActivity: Files: 6 2021-05-06 20:45:00.135 8966-8966/kim.hsl.a7_zip I/MainActivity: Size: 5965824 2021-05-06 20:45:00.135 8966-8966/kim.hsl.a7_zip I/MainActivity: Compressed: 309075 2021-05-06 20:45:00.135 8966-8966/kim.hsl.a7_zip I/MainActivity: 解壓縮文件 , 執(zhí)行完畢 , exitValue = 0 2021-05-06 20:45:00.135 8966-8966/kim.hsl.a7_zip I/octopus: 7zTypes SZ_OK = 0
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
四、參考資料
參考資料 :
7-Zip 官網(wǎng) : https://www.7-zip.org/
Android NDK 編譯構(gòu)建腳本參考文檔 :
ndk-build 腳本 : https://developer.android.google.cn/ndk/guides/ndk-build
Android.mk 構(gòu)建腳本 : https://developer.android.google.cn/ndk/guides/android_mk
Application.mk 構(gòu)建腳本 : https://developer.android.google.cn/ndk/guides/application_mk
博客資源 : 源碼 , 編譯后的可執(zhí)行文件, 在 7zip\p7zip_16.02\CPP\ANDROID\7zr\libs\ 目錄下 ;
- : https://download.csdn.net/download/han1202012/18215890
GitHub 項(xiàng)目源碼 : https://github.com/han1202012/7-Zip
Android
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。