Android 安装包优化】Android 中使用 7zr 可执行程序 解压缩文件

网友投稿 767 2022-05-30

文章目录

一、Android 中使用 7zr 可执行程序 解压缩文件

二、完整代码示例

三、参考资料

一、Android 中使用 7zr 可执行程序 解压缩文件

在上一篇博客 【Android 安装包优化】Android 中使用 7zr 可执行程序压缩文件 中 , 将 /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

执行命令行 :

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") // 执行前赋予可执行权限 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) // 读取命令执行过程数据 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

执行结果 :

解压缩命令 : /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 解压缩文件 , 执行完毕 , 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}") // 查看该文件是否存在, 如果存在设置该文件可执行 // 如果不存在 , 拷贝文件 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 } } /** * 使用 7zr 进行解压缩 */ fun uncompress7z() { // /data/user/0/kim.hsl.a7_zip/files/7zr var exeFile = File(filesDir, "7zr") // 执行前赋予可执行权限 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) // 读取命令执行过程数据 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

29

30

31

32

33

34

35

36

37

38

39

40

41

42

【Android 安装包优化】Android 中使用 7zr 可执行程序 解压缩文件

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

执行结果 :

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 可执行文件 , 开始拷贝文件 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: 压缩文件 , 执行完毕 , 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: 解压缩文件 , 执行完毕 , 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

博客资源 : 源码 , 编译后的可执行文件, 在 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小时内删除侵权内容。

上一篇:mock接口数据
下一篇:GaussDB(DWS)《通过JDBC,实现远程COPY导入数据到DWS》
相关文章