【Android 安裝包優(yōu)化】Android 中使用 7zr 可執(zhí)行程序 解壓縮文件
文章目錄
一、Android 中使用 7zr 可執(zhí)行程序 解壓縮文件
二、完整代碼示例
三、參考資料
一、Android 中使用 7zr 可執(zhí)行程序 解壓縮文件
在上一篇博客 【Android 安裝包優(yōu)化】Android 中使用 7zr 可執(zhí)行程序壓縮文件 中 , 將 /data/user/0/kim.hsl.a7_zip/files 目錄壓縮存放到 /data/user/0/kim.hsl.a7_zip/files/files.7z 文件中 ;
拼裝 7zr 解壓縮命令 :
var cmd = "${exeFile.absolutePath} x ${filesDir.absolutePath}/files.7z -o${filesDir.absolutePath}/unzip_file"
1
實際命令 :
/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
1
執(zhí)行命令行 :
var process: Process = Runtime.getRuntime().exec(cmd)
1
使用 7zr 命令壓縮文件 :
/** * 使用 7zr 進行解壓縮 */ fun uncompress7z() { // /data/user/0/kim.hsl.a7_zip/files/7zr var exeFile = File(filesDir, "7zr") // 執(zhí)行前賦予可執(zhí)行權限 exeFile.setExecutable(true) 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") }
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
執(zhí)行結果 :
解壓縮命令 : /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 7-Zip (a) [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21 p7zip Version 16.02 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,8 CPUs LE) Scanning the drive for archives: 1 file, 308166 bytes (301 KiB) Extracting archive: /data/user/0/kim.hsl.a7_zip/files/files.7z -- Path = /data/user/0/kim.hsl.a7_zip/files/files.7z Type = 7z Physical Size = 308166 Headers Size = 168 Method = LZMA2:20 Solid = - Blocks = 1 Everything is Ok Folders: 1 Files: 1 Size: 994304 Compressed: 308166 解壓縮文件 , 執(zhí)行完畢 , exitValue = 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
二、完整代碼示例
完整代碼 :
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" } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) copy7zr() compress7z() uncompress7z() } /** * 將 7zr 文件拷貝到應用私有目錄 */ 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}") // 查看該文件是否存在, 如果存在設置該文件可執(zhí)行 // 如果不存在 , 拷貝文件 if (exeFile.exists()) { exeFile.setExecutable(true) Log.i(TAG, "內置存儲空間存在該 /data/user/0/kim.hsl.a7_zip/files/7zr 文件") return } else { Log.i(TAG, "內置存儲空間不存在 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 架構拷貝不同的可執(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 文件結束") } /** * 使用 7zr 進行壓縮 */ fun compress7z() { // /data/user/0/kim.hsl.a7_zip/files/7zr var exeFile = File(filesDir, "7zr") // 執(zhí)行前賦予可執(zhí)行權限 exeFile.setExecutable(true) 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í)行完畢 * 調用 process.exitValue 方法 , 如果沒有執(zhí)行完畢 , 會拋異常, * 如果執(zhí)行完畢會返回一個確定的值 */ fun isComplete(process: Process): Boolean { try { // 已經執(zhí)行完畢 process.exitValue() return true } catch (e: IllegalThreadStateException) { // 未執(zhí)行完畢 return false } } /** * 使用 7zr 進行解壓縮 */ fun uncompress7z() { // /data/user/0/kim.hsl.a7_zip/files/7zr var exeFile = File(filesDir, "7zr") // 執(zhí)行前賦予可執(zhí)行權限 exeFile.setExecutable(true) 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") } }
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
執(zhí)行結果 :
2021-04-29 22:16:33.842 10262-10262/kim.hsl.a7_zip I/MainActivity: 開始拷貝 7zr 文件 2021-04-29 22:16:33.844 10262-10262/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-04-29 22:16:33.844 10262-10262/kim.hsl.a7_zip I/MainActivity: 內置存儲空間不存在 7zr 可執(zhí)行文件 , 開始拷貝文件 2021-04-29 22:16:33.844 10262-10262/kim.hsl.a7_zip I/MainActivity: Build.CPU_ABI = arm64-v8a 2021-04-29 22:16:33.873 10262-10262/kim.hsl.a7_zip I/MainActivity: 拷貝 7zr 文件結束 2021-04-29 22:16:33.873 10262-10262/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-04-29 22:16:34.240 10262-10262/kim.hsl.a7_zip I/MainActivity: 7-Zip (a) [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21 2021-04-29 22:16:34.240 10262-10262/kim.hsl.a7_zip I/MainActivity: p7zip Version 16.02 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,8 CPUs LE) 2021-04-29 22:16:34.240 10262-10262/kim.hsl.a7_zip I/MainActivity: Scanning the drive: 2021-04-29 22:16:34.240 10262-10262/kim.hsl.a7_zip I/MainActivity: 1 folder, 1 file, 994304 bytes (971 KiB) 2021-04-29 22:16:34.240 10262-10262/kim.hsl.a7_zip I/MainActivity: Creating archive: /data/user/0/kim.hsl.a7_zip/files/files.7z 2021-04-29 22:16:34.240 10262-10262/kim.hsl.a7_zip I/MainActivity: Items to compress: 2 2021-04-29 22:16:34.240 10262-10262/kim.hsl.a7_zip I/MainActivity: Files read from disk: 1 2021-04-29 22:16:34.240 10262-10262/kim.hsl.a7_zip I/MainActivity: Archive size: 308166 bytes (301 KiB) 2021-04-29 22:16:34.240 10262-10262/kim.hsl.a7_zip I/MainActivity: Everything is Ok 2021-04-29 22:16:34.240 10262-10262/kim.hsl.a7_zip I/MainActivity: 壓縮文件 , 執(zhí)行完畢 , exitValue = 0 2021-04-29 22:16:34.241 10262-10262/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-04-29 22:16:34.246 10262-10262/kim.hsl.a7_zip I/MainActivity: 7-Zip (a) [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21 2021-04-29 22:16:34.246 10262-10262/kim.hsl.a7_zip I/MainActivity: p7zip Version 16.02 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,8 CPUs LE) 2021-04-29 22:16:34.246 10262-10262/kim.hsl.a7_zip I/MainActivity: Scanning the drive for archives: 2021-04-29 22:16:34.246 10262-10262/kim.hsl.a7_zip I/MainActivity: 1 file, 308166 bytes (301 KiB) 2021-04-29 22:16:34.246 10262-10262/kim.hsl.a7_zip I/MainActivity: Extracting archive: /data/user/0/kim.hsl.a7_zip/files/files.7z 2021-04-29 22:16:34.275 10262-10262/kim.hsl.a7_zip I/MainActivity: -- 2021-04-29 22:16:34.275 10262-10262/kim.hsl.a7_zip I/MainActivity: Path = /data/user/0/kim.hsl.a7_zip/files/files.7z 2021-04-29 22:16:34.275 10262-10262/kim.hsl.a7_zip I/MainActivity: Type = 7z 2021-04-29 22:16:34.275 10262-10262/kim.hsl.a7_zip I/MainActivity: Physical Size = 308166 2021-04-29 22:16:34.275 10262-10262/kim.hsl.a7_zip I/MainActivity: Headers Size = 168 2021-04-29 22:16:34.275 10262-10262/kim.hsl.a7_zip I/MainActivity: Method = LZMA2:20 2021-04-29 22:16:34.275 10262-10262/kim.hsl.a7_zip I/MainActivity: Solid = - 2021-04-29 22:16:34.275 10262-10262/kim.hsl.a7_zip I/MainActivity: Blocks = 1 2021-04-29 22:16:34.275 10262-10262/kim.hsl.a7_zip I/MainActivity: Everything is Ok 2021-04-29 22:16:34.275 10262-10262/kim.hsl.a7_zip I/MainActivity: Folders: 1 2021-04-29 22:16:34.275 10262-10262/kim.hsl.a7_zip I/MainActivity: Files: 1 2021-04-29 22:16:34.275 10262-10262/kim.hsl.a7_zip I/MainActivity: Size: 994304 2021-04-29 22:16:34.275 10262-10262/kim.hsl.a7_zip I/MainActivity: Compressed: 308166 2021-04-29 22:16:34.275 10262-10262/kim.hsl.a7_zip I/MainActivity: 解壓縮文件 , 執(zhí)行完畢 , exitValue = 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
36
三、參考資料
參考資料 :
7-Zip 官網 : https://www.7-zip.org/
Android NDK 編譯構建腳本參考文檔 :
ndk-build 腳本 : https://developer.android.google.cn/ndk/guides/ndk-build
Android.mk 構建腳本 : https://developer.android.google.cn/ndk/guides/android_mk
Application.mk 構建腳本 : 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 項目源碼 : https://github.com/han1202012/7-Zip
博客源碼快照 : https://download.csdn.net/download/han1202012/18254613
Android
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。