Android 安裝包優化】Android 中使用 7zr 可執行程序 壓縮文件

      網友投稿 1018 2022-05-30

      文章目錄

      一、Android 中使用 7zr 可執行程序壓縮文件

      二、完整代碼示例

      三、參考資料

      一、Android 中使用 7zr 可執行程序壓縮文件

      在上一篇博客 【Android 安裝包優化】Android 應用中 7zr 可執行程序準備 ( Android Studio 導入可執行 7zr 程序 | 從 Assets 資源文件拷貝 7zr 到內置存儲 ) 中 , 將 7zr 可執行文件拷貝到了應用內置目錄 " /data/user/0/kim.hsl.a7_zip/files/ " 中 , 只有放在該目錄下 , 才能執行該 7zr 可執行程序 ;

      判定命令是否執行完畢 : 調用 Process 的 exitValue 方法 , 獲取退出碼 , 如果返回 0 0 0 說明執行成功 ; 如果捕獲到 IllegalThreadStateException 異常 , 說明命令還在執行中 ;

      /** * 判定命令是否執行完畢 * 調用 process.exitValue 方法 , 如果沒有執行完畢 , 會拋異常, * 如果執行完畢會返回一個確定的值 */ fun isComplete(process: Process): Boolean { try { // 已經執行完畢 process.exitValue() return true } catch (e: IllegalThreadStateException) { // 未執行完畢 return false } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      拼裝 7zr 壓縮命令 :

      var cmd = "${exeFile.absolutePath} a ${filesDir.absolutePath}/files.7z ${filesDir.absolutePath} -mx=9 -t7z"

      1

      執行命令行 :

      var process: Process = Runtime.getRuntime().exec(cmd)

      1

      使用 7zr 命令壓縮文件 :

      /** * 使用 7zr 進行壓縮 */ fun compress7z() { // /data/user/0/kim.hsl.a7_zip/files/7zr var exeFile = File(filesDir, "7zr") // 執行前賦予可執行權限 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) // 讀取命令執行過程數據 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, "執行完畢 , 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

      二、完整代碼示例

      完整代碼 :

      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() } /** * 將 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}") // 查看該文件是否存在, 如果存在設置該文件可執行 // 如果不存在 , 拷貝文件 if (exeFile.exists()) { exeFile.setExecutable(true) Log.i(TAG, "內置存儲空間存在該 /data/user/0/kim.hsl.a7_zip/files/7zr 文件") return } else { Log.i(TAG, "內置存儲空間不存在 7zr 可執行文件 , 開始拷貝文件") } // 如果不存在 , 拷貝文件 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 架構拷貝不同的可執行程序 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") // 執行前賦予可執行權限 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) // 讀取命令執行過程數據 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, "執行完畢 , exitValue = $exitValue") } /** * 判定命令是否執行完畢 * 調用 process.exitValue 方法 , 如果沒有執行完畢 , 會拋異常, * 如果執行完畢會返回一個確定的值 */ fun isComplete(process: Process): Boolean { try { // 已經執行完畢 process.exitValue() return true } catch (e: IllegalThreadStateException) { // 未執行完畢 return false } } }

      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

      【Android 安裝包優化】Android 中使用 7zr 可執行程序 壓縮文件

      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

      執行結果 :

      2021-04-29 22:07:06.867 8965-8965/kim.hsl.a7_zip I/MainActivity: 開始拷貝 7zr 文件 2021-04-29 22:07:06.869 8965-8965/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:07:06.869 8965-8965/kim.hsl.a7_zip I/MainActivity: 內置存儲空間不存在 7zr 可執行文件 , 開始拷貝文件 2021-04-29 22:07:06.869 8965-8965/kim.hsl.a7_zip I/MainActivity: Build.CPU_ABI = arm64-v8a 2021-04-29 22:07:06.897 8965-8965/kim.hsl.a7_zip I/MainActivity: 拷貝 7zr 文件結束 2021-04-29 22:07:06.897 8965-8965/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:07:07.262 8965-8965/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:07:07.262 8965-8965/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:07:07.262 8965-8965/kim.hsl.a7_zip I/MainActivity: Scanning the drive: 2021-04-29 22:07:07.262 8965-8965/kim.hsl.a7_zip I/MainActivity: 1 folder, 1 file, 994304 bytes (971 KiB) 2021-04-29 22:07:07.262 8965-8965/kim.hsl.a7_zip I/MainActivity: Creating archive: /data/user/0/kim.hsl.a7_zip/files/files.7z 2021-04-29 22:07:07.262 8965-8965/kim.hsl.a7_zip I/MainActivity: Items to compress: 2 2021-04-29 22:07:07.262 8965-8965/kim.hsl.a7_zip I/MainActivity: Files read from disk: 1 2021-04-29 22:07:07.262 8965-8965/kim.hsl.a7_zip I/MainActivity: Archive size: 308165 bytes (301 KiB) 2021-04-29 22:07:07.262 8965-8965/kim.hsl.a7_zip I/MainActivity: Everything is Ok 2021-04-29 22:07:07.263 8965-8965/kim.hsl.a7_zip I/MainActivity: 執行完畢 , exitValue = 0

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      三、參考資料

      參考資料 :

      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

      博客資源 : 源碼 , 編譯后的可執行文件, 在 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

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:excel表格里折疊展開的方法
      下一篇:【愚公系列】2021年12月 攻防世界-進階題-MISC-071(4-1)
      相關文章
      亚洲精品国产电影午夜| 成人亚洲国产va天堂| 亚洲夂夂婷婷色拍WW47| 亚洲精品国产免费| 久久久久亚洲AV成人无码网站 | 国产午夜亚洲精品理论片不卡| 国产成人亚洲午夜电影| 成人婷婷网色偷偷亚洲男人的天堂| 亚洲日本天堂在线| 亚洲hairy多毛pics大全| 亚洲老熟女五十路老熟女bbw| 亚洲一区二区三区在线观看网站| 亚洲粉嫩美白在线| 亚洲精品第一综合99久久| 亚洲不卡影院午夜在线观看| 中文字幕在线观看亚洲视频| 亚洲大成色www永久网址| 一本天堂ⅴ无码亚洲道久久| 亚洲精品色播一区二区| 亚洲aⅴ无码专区在线观看| 精品国产亚洲AV麻豆| 亚洲国产精品嫩草影院久久 | 亚洲AV无码一区二三区 | 亚洲综合无码一区二区三区| 亚洲成a人片在线观看播放| 亚洲一区二区三区91| 中文字幕亚洲精品无码| 亚洲国产美女精品久久久| 国产精品亚洲五月天高清| 亚洲精品无码永久在线观看| 337p日本欧洲亚洲大胆裸体艺术 | 国产精品亚洲片在线| 亚洲伦另类中文字幕| 亚洲美女视频网站| 亚洲AV成人噜噜无码网站| 亚洲欧美日韩中文字幕在线一区| 校园亚洲春色另类小说合集 | 亚洲精品国产精品国自产观看| 亚洲午夜福利717| 亚洲男人第一av网站| 亚洲一区二区三区免费视频|