超强文件压缩解压插件 支持安卓/iOS/鸿蒙(cz-unzip)UTS插件

介绍
  1. 支持多种压缩/解压格式:ZIP、7z、RAR、TAR、GZIP、BZIP2、XZ、CPIO。
  2. 支持加密压缩/解压(ZIP AES-256、7z AES-256、RAR 密码),鸿蒙暂不支持密码。
  3. 支持压缩级别设置、分卷压缩(仅 Android)、进度回调等高级功能。
  4. 支持列出压缩包内文件列表(部分格式仅 Android)。
插件详情

去插件市场体验

API说明
方法名称 参数 返回参数 说明
requestPermission 回调 请求文件系统权限,Android/iOS/鸿蒙自动适配
resolveFilePath string string 将 URI 或 file:// 路径转为本地可用的绝对路径
zip ZipParams 将文件/目录压缩为 .zip 格式
unzip UnzipParams 解压 .zip 文件
listZipFiles ListFilesParams 列出 .zip 文件内容(仅 Android)
sevenZip SevenZParams 将文件/目录压缩为 .7z 格式
unsevenZip UnSevenZParams 解压 .7z 文件
listSevenZFiles ListSevenZFilesParams 列出 .7z 文件内容(仅 Android)
unrar UnRarParams 解压 .rar 文件
listRarFiles ListRarFilesParams 列出 .rar 文件内容(仅 Android)
tar TarParams 将文件/目录打包为 .tar 归档
untar UnTarParams 解包 .tar 文件
listTarFiles ListTarFilesParams 列出 .tar 文件内容(仅 Android)
gzip GzipParams 将单文件压缩为 .gz 格式(不支持目录)
ungzip UnGzipParams 解压 .gz 文件
bzip2 Bzip2Params 将单文件压缩为 .bz2 格式(仅 Android,不支持目录)
unbzip2 UnBzip2Params 解压 .bz2 文件(仅 Android)
xz XzParams 将单文件压缩为 .xz 格式(不支持目录)
unxz UnXzParams 解压 .xz 文件
cpio CpioParams 将文件/目录归档为 .cpio 格式(仅 Android)
uncpio UnCpioParams 解包 .cpio 文件(仅 Android)
listCpioFiles ListCpioFilesParams 列出 .cpio 文件内容(仅 Android)
公共参数类型说明
ZipParams(ZIP 压缩参数)
参数 类型 必填 默认值 说明
srcPaths string[] 或 null null 要压缩的文件/目录路径数组(优先于 srcPath)
srcPath string - 单个文件/目录路径(srcPaths 为空时使用)
destPath string - 输出的 .zip 文件完整路径
password string 或 null null 压缩密码,iOS 固定 AES-256
level number 或 null 5 压缩级别 0-9,0=仅存储,9=最高压缩比
method string 或 null “DEFLATE” 压缩方法:“DEFLATE” 或 “STORE”(仅 Android)
encryptionMethod string 或 null “AES” 加密方式(仅 Android,有密码时生效)
aesKeyStrength string 或 null “KEY_STRENGTH_256” AES 密钥强度(仅 Android)
includeRootFolder boolean 或 null true 压缩目录时是否包含根目录名
readHiddenFiles boolean 或 null - 是否包含隐藏文件(仅 Android)
readHiddenFolders boolean 或 null - 是否包含隐藏目录(仅 Android)
defaultFolderPath string 或 null null zip 内的默认父路径(仅 Android)
fileNameInZip string 或 null null 单文件在 zip 中的文件名(仅 Android)
charset string 或 null null 文件名/注释编码如 “UTF-8”、“GBK”(仅 Android)
zipComment string 或 null null zip 文件注释(仅 Android)
splitArchive boolean 或 null false 是否分卷(仅 Android)
splitLength number 或 null 0 分卷大小(字节),最小 65536(仅 Android)
progressListener (progress: number)=>void - 进度回调,返回 0-100
completeListener (res: ResponseEntity)=>void - 完成回调,data 为 ZipResult 类型
UnzipParams(ZIP 解压参数)
参数 类型 必填 默认值 说明
srcPath string - .zip 文件路径
destPath string - 解压目标目录
password string 或 null null 解压密码
fileName string 或 null null 仅解压 zip 内指定文件(仅 Android)
charset string 或 null null 文件名/注释编码(仅 Android)
progressListener (progress: number)=>void - 进度回调,返回 0-100
completeListener (res: ResponseEntity)=>void - 完成回调,data 为 UnzipResult 类型
SevenZParams(7z 压缩参数)
参数 类型 必填 默认值 说明
srcPaths string[] 或 null null 要压缩的文件/目录路径数组
srcPath string - 单个文件/目录路径
destPath string - 输出的 .7z 文件完整路径
password string 或 null null AES-256 加密密码
method string 或 null “LZMA2” 压缩方法:“COPY” / “LZMA2” / “DEFLATE” / “BZIP2”,iOS 支持 “LZMA”/“LZMA2”/“PPMd”
level number 或 null 7 压缩级别 1-9
progressListener (progress: number)=>void - 进度回调,返回 0-100
completeListener (res: ResponseEntity)=>void - 完成回调,data 为 SevenZResult 类型
UnRarParams(RAR 解压参数)
参数 类型 必填 默认值 说明
srcPath string - .rar 文件路径
destPath string - 解压目标目录
password string 或 null null 解压密码
progressListener (progress: number)=>void - 进度回调,返回 0-100
completeListener (res: ResponseEntity)=>void - 完成回调,data 为 UnRarResult 类型
TarParams(TAR 打包参数)
参数 类型 必填 默认值 说明
srcPaths string[] 或 null null 要打包的文件/目录路径数组
srcPath string - 单个文件/目录路径
destPath string - 输出的 .tar 文件完整路径
progressListener (progress: number)=>void - 进度回调,返回 0-100
completeListener (res: ResponseEntity)=>void - 完成回调,data 为 TarResult 类型
GzipParams(GZIP 压缩参数,仅支持单文件)
参数 类型 必填 默认值 说明
srcPath string - 单文件路径(不能是目录)
destPath string - 输出的 .gz 文件完整路径
level number 或 null - 压缩级别 0-9(仅 Android)
progressListener (progress: number)=>void - 进度回调,返回 0-100
completeListener (res: ResponseEntity)=>void - 完成回调,data 为 GzipResult 类型
Bzip2Params(BZ2 压缩参数,仅支持单文件,仅 Android)
参数 类型 必填 默认值 说明
srcPath string - 单文件路径(不能是目录)
destPath string - 输出的 .bz2 文件完整路径
level number 或 null 5 压缩级别 0-9
progressListener (progress: number)=>void - 进度回调,返回 0-100
completeListener (res: ResponseEntity)=>void - 完成回调,data 为 Bzip2Result 类型
XzParams(XZ 压缩参数,仅支持单文件)
参数 类型 必填 默认值 说明
srcPath string - 单文件路径(不能是目录)
destPath string - 输出的 .xz 文件完整路径
level number 或 null 6 LZMA2 预设级别 0-9
progressListener (progress: number)=>void - 进度回调,返回 0-100
completeListener (res: ResponseEntity)=>void - 完成回调,data 为 XzResult 类型
CpioParams(CPIO 归档参数,仅 Android)
参数 类型 必填 默认值 说明
srcPaths string[] 或 null null 要归档的文件/目录路径数组
srcPath string - 单个文件/目录路径
destPath string - 输出的 .cpio 文件完整路径
progressListener (progress: number)=>void - 进度回调,返回 0-100
completeListener (res: ResponseEntity)=>void - 完成回调,data 为 CpioResult 类型
返回值类型说明
ResponseEntity(统一响应类型)
{
  success: boolean,       // true=成功, false=失败
  data?: any | null,      // 成功时包含具体结果
  message?: string | null // 状态消息或错误描述
}
ZipResult
{
  srcPaths: string[],     // 源路径
  destPath: string,       // 输出 .zip 路径
  splitArchive: boolean,  // 是否分卷
  splitLength: number | null
}
UnzipResult
{
  srcPath: string,          // 源 .zip 路径
  destPath: string,         // 目标目录
  extractedFiles: string[]  // 解压出的文件路径列表
}
SevenZResult
{
  srcPaths: string[],    // 源路径
  destPath: string,      // 输出 .7z 路径
  fileCount: number,     // 压缩的文件数量
  totalSize: number      // 压缩前总大小(字节)
}
GzipResult
{
  srcPath: string,    // 源路径
  destPath: string,   // 输出 .gz 路径
  totalSize: number   // 压缩前总大小(字节)
}
平台支持
方法 Android iOS 鸿蒙
zip ✅ 完整 ✅ 部分 ✅ 无密码
unzip ✅ 完整 ✅ 部分 ✅ 无密码
listZipFiles
sevenZip ✅ 完整 ✅ 部分 ✅ 无密码
unsevenZip ✅ 完整 ✅ 部分 ✅ 无密码
listSevenZFiles
unrar ✅ 仅RAR4
listRarFiles
tar
untar
listTarFiles
gzip
ungzip
bzip2
unbzip2
xz
unxz
cpio
uncpio
listCpioFiles

注意:

  • 鸿蒙:ZIP/7z/RAR 操作不支持密码加密;7z/GZIP/XZ 不支持自定义压缩级别。
  • iOS:gzip/ungzip/bzip2/unbzip2/xz/unxz/cpio/uncpio 暂不支持。
  • RAR:鸿蒙仅支持 RAR4 格式解压,不支持 RAR5 和加密 RAR。
uvue代码调用示例
<template>
	<scroll-view style="flex: 1;" scroll-y="true">
		<view class="page-root">
			<view class="panel-card">
				<text class="panel-title">当前平台</text>
				<text class="panel-line platform-tag">{{ platform }}</text>
			</view>
			<view class="uni-padding-wrap uni-common-mt">
				<button @tap="testZip">zip压缩</button>
			</view>
			<view class="uni-padding-wrap uni-common-mt" style="margin-top: 10px;">
				<button @tap="testUnzip">unzip解压缩</button>
			</view>
			<view class="uni-padding-wrap uni-common-mt" style="margin-top: 10px;">
				<button @tap="testSevenZip">7z压缩</button>
			</view>
			<view class="uni-padding-wrap uni-common-mt" style="margin-top: 10px;">
				<button @tap="testUnSevenZip">7z解压</button>
			</view>
			<view class="uni-padding-wrap uni-common-mt" style="margin-top: 10px;">
				<button @tap="testUnRar">RAR解压</button>
			</view>
			<view class="uni-padding-wrap uni-common-mt" style="margin-top: 10px;">
				<button @tap="testTar">TAR压缩</button>
			</view>
			<view class="uni-padding-wrap uni-common-mt" style="margin-top: 10px;">
				<button @tap="testUnTar">TAR解压</button>
			</view>
			<view class="uni-padding-wrap uni-common-mt" style="margin-top: 10px;">
				<button @tap="testGzip">GZIP压缩</button>
			</view>
			<view class="uni-padding-wrap uni-common-mt" style="margin-top: 10px;">
				<button @tap="testUnGzip">GZIP解压</button>
			</view>
			<view class="uni-padding-wrap uni-common-mt" style="margin-top: 10px;">
				<button @tap="testBzip2">BZ2压缩</button>
			</view>
			<view class="uni-padding-wrap uni-common-mt" style="margin-top: 10px;">
				<button @tap="testUnBzip2">BZ2解压</button>
			</view>
			<view class="uni-padding-wrap uni-common-mt" style="margin-top: 10px;">
				<button @tap="testXz">XZ压缩</button>
			</view>
			<view class="uni-padding-wrap uni-common-mt" style="margin-top: 10px;">
				<button @tap="testUnXz">XZ解压</button>
			</view>
			<view class="uni-padding-wrap uni-common-mt" style="margin-top: 10px;">
				<button @tap="testCpio">CPIO归档</button>
			</view>
			<view class="uni-padding-wrap uni-common-mt" style="margin-top: 10px;">
				<button @tap="testUnCpio">CPIO解包</button>
			</view>
		</view>
	</scroll-view>
</template>

<script>
	import * as module from "@/uni_modules/cz-unzip"
	import { ZipResult, GzipResult } from "@/uni_modules/cz-unzip";

	export default {
		data() {
			return {
				platform: ''
			}
		},
		onReady() {
			let device = uni.getDeviceInfo()
			console.log(`读取系统信息: ${JSON.stringify(device)}`)
			this.platform = `手机信息: ${device.deviceBrand}-${device.osName}-${device.osVersion}-${device.romVersion}`;
		},
		methods: {
			// ==================== ZIP 压缩 ====================
			testZip(){
				let self = this;
				// 1. 先请求文件权限
				module.requestPermission((res)=>{
					// 2. 选择文件
					uni.chooseFile({
						count: 2,
						type: 'all',
						success: (res) => {
							if (res.tempFiles.length > 0) {
								// 3. 解析文件路径(处理 content:// 等 URI)
								const srcPaths = res.tempFiles.map((v) => module.resolveFilePath(v.path))
								// 4. 调用压缩
								module.zip({
									srcPaths: srcPaths,
									destPath: self.toZipPath(srcPaths[0]),
									// password: "123456",        // 可选:加密密码
									method: "DEFLATE",             // 压缩方法
									level: 5,                      // 压缩级别 0-9
									progressListener: (progress : number) => {
										console.log('压缩进度:' + progress + '%')
									},
									completeListener: (res) => {
										console.log('压缩完成:' + JSON.stringify(res))
										if (res.success) {
											self.showToast('压缩成功:' + (res.data as ZipResult).destPath)
										} else {
											self.showToast('压缩失败:' + res.message)
										}
									}
								})
							}
						},
						fail: (err) => {
							self.showToast('选择文件失败:' + JSON.stringify(err))
						}
					})
				})
			},
			// ==================== ZIP 解压 ====================
			testUnzip(){
				let self = this;
				module.requestPermission((res)=>{
					uni.chooseFile({
						count: 1,
						type: 'all',
						success: (res) => {
							if (res.tempFiles.length > 0) {
								let srcPath = res.tempFiles[0].path
								let path = module.resolveFilePath(srcPath);
								module.unzip({
									srcPath: path,
									destPath: self.toUnzipPath(path),
									// password: "123456",        // 可选:解压密码
									charset: "utf-8",              // 文件名编码
									progressListener: (progress) => {
										console.log('解压进度:' + progress + '%')
									},
									completeListener: (res) => {
										console.log('解压完成:' + JSON.stringify(res))
										if (res.success) {
											self.showToast('解压成功,输出文件:' + JSON.stringify(res.data))
										} else {
											self.showToast('解压失败:' + res.message)
										}
									}
								})
							}
						},
						fail: (err) => {
							self.showToast('选择文件失败:' + JSON.stringify(err))
						}
					})
				})
			},
			// ==================== 7z 压缩 ====================
			testSevenZip(){
				let self = this;
				module.requestPermission((res)=>{
					uni.chooseFile({
						count: 99,
						type: 'all',
						success: (res) => {
							if (res.tempFiles.length > 0) {
								const srcPaths = res.tempFiles.map((v) => module.resolveFilePath(v.path))
								module.sevenZip({
									srcPaths: srcPaths,
									destPath: self.toSevenZPath(srcPaths[0]),
									// password: "123456",        // 可选:AES-256 加密密码
									method: "LZMA2",               // 压缩方法
									// method 不传默认 LZMA2,可选 "COPY"|"LZMA2"|"DEFLATE"|"BZIP2"
									progressListener: (progress : number) => {
										console.log('7z压缩进度:' + progress + '%')
									},
									completeListener: (res) => {
										console.log('7z压缩完成:' + JSON.stringify(res))
										if (res.success) {
											self.showToast('7z压缩成功:' + JSON.stringify(res.data))
										} else {
											self.showToast('7z压缩失败:' + res.message)
										}
									}
								})
							}
						},
						fail: (err) => {
							self.showToast('选择文件失败:' + JSON.stringify(err))
						}
					})
				})
			},
			// ==================== 7z 解压 ====================
			testUnSevenZip(){
				let self = this;
				module.requestPermission((res)=>{
					uni.chooseFile({
						count: 1,
						type: 'all',
						success: (res) => {
							if (res.tempFiles.length > 0) {
								let srcPath = res.tempFiles[0].path
								let path = module.resolveFilePath(srcPath);
								module.unsevenZip({
									srcPath: path,
									destPath: self.toUnSevenZPath(path),
									// password: "123456",        // 可选
									progressListener: (progress : number) => {
										console.log('7z解压进度:' + progress + '%')
									},
									completeListener: (res) => {
										if (res.success) {
											self.showToast('7z解压成功:' + JSON.stringify(res.data))
										} else {
											self.showToast('7z解压失败:' + res.message)
										}
									}
								})
							}
						},
						fail: (err) => {
							self.showToast('选择文件失败:' + JSON.stringify(err))
						}
					})
				})
			},
			// ==================== RAR 解压 ====================
			testUnRar(){
				let self = this;
				module.requestPermission((res)=>{
					uni.chooseFile({
						count: 1,
						type: 'all',
						success: (res) => {
							if (res.tempFiles.length > 0) {
								let srcPath = res.tempFiles[0].path
								let path = module.resolveFilePath(srcPath);
								module.unrar({
									srcPath: path,
									destPath: self.toUnRarPath(path),
									// password: "123456",        // 可选
									progressListener: (progress : number) => {
										console.log('RAR解压进度:' + progress + '%')
									},
									completeListener: (res) => {
										console.log('RAR解压完成:' + JSON.stringify(res))
										if (res.success) {
											self.showToast('RAR解压成功:' + JSON.stringify(res.data))
										} else {
											self.showToast('RAR解压失败:' + res.message)
										}
									}
								})
							}
						},
						fail: (err) => {
							self.showToast('选择文件失败:' + JSON.stringify(err))
						}
					})
				})
			},
			// ==================== TAR 打包 ====================
			testTar(){
				let self = this;
				module.requestPermission((res)=>{
					uni.chooseFile({
						count: 99,
						type: 'all',
						success: (res) => {
							if (res.tempFiles.length > 0) {
								const srcPaths = res.tempFiles.map((v) => module.resolveFilePath(v.path))
								module.tar({
									srcPaths: srcPaths,
									destPath: self.toTarPath(srcPaths[0]),
									progressListener: (progress : number) => {
										console.log('TAR压缩进度:' + progress + '%')
									},
									completeListener: (res) => {
										if (res.success) {
											self.showToast('TAR压缩成功:' + JSON.stringify(res.data))
										} else {
											self.showToast('TAR压缩失败:' + res.message)
										}
									}
								})
							}
						},
						fail: (err) => {
							self.showToast('选择文件失败:' + JSON.stringify(err))
						}
					})
				})
			},
			// ==================== TAR 解包 ====================
			testUnTar(){
				let self = this;
				module.requestPermission((res)=>{
					uni.chooseFile({
						count: 1,
						type: 'all',
						success: (res) => {
							if (res.tempFiles.length > 0) {
								let srcPath = res.tempFiles[0].path
								let path = module.resolveFilePath(srcPath);
								module.untar({
									srcPath: path,
									destPath: self.toUnTarPath(path),
									progressListener: (progress : number) => {
										console.log('TAR解压进度:' + progress + '%')
									},
									completeListener: (res) => {
										if (res.success) {
											self.showToast('TAR解压成功:' + JSON.stringify(res.data))
										} else {
											self.showToast('TAR解压失败:' + res.message)
										}
									}
								})
							}
						},
						fail: (err) => {
							self.showToast('选择文件失败:' + JSON.stringify(err))
						}
					})
				})
			},
			// ==================== GZIP 压缩(仅单文件) ====================
			testGzip(){
				let self = this;
				module.requestPermission((res)=>{
					uni.chooseFile({
						count: 1,
						type: 'all',
						success: (res) => {
							if (res.tempFiles.length > 0) {
								let srcPath = res.tempFiles[0].path
								let path = module.resolveFilePath(srcPath);
								module.gzip({
									srcPath: path,
									destPath: self.toGzipPath(path),
									level: 5,
									progressListener: (progress : number) => {
										console.log('GZIP压缩进度:' + progress + '%')
									},
									completeListener: (res) => {
										if (res.success) {
											self.showToast('GZIP压缩成功:' + JSON.stringify(res.data))
										} else {
											self.showToast('GZIP压缩失败:' + res.message)
										}
									}
								})
							}
						},
						fail: (err) => {
							self.showToast('选择文件失败:' + JSON.stringify(err))
						}
					})
				})
			},
			// ==================== GZIP 解压(仅单文件) ====================
			testUnGzip(){
				let self = this;
				module.requestPermission((res)=>{
					uni.chooseFile({
						count: 1,
						type: 'all',
						success: (res) => {
							if (res.tempFiles.length > 0) {
								let srcPath = res.tempFiles[0].path
								let path = module.resolveFilePath(srcPath);
								module.ungzip({
									srcPath: path,
									destPath: self.toUnGzipPath(path),
									progressListener: (progress : number) => {
										console.log('GZIP解压进度:' + progress + '%')
									},
									completeListener: (res) => {
										if (res.success) {
											self.showToast('GZIP解压成功:' + JSON.stringify(res.data))
										} else {
											self.showToast('GZIP解压失败:' + res.message)
										}
									}
								})
							}
						},
						fail: (err) => {
							self.showToast('选择文件失败:' + JSON.stringify(err))
						}
					})
				})
			},
			// ==================== BZIP2 压缩(仅单文件,仅 Android) ====================
			testBzip2(){
				let self = this;
				module.requestPermission((res)=>{
					uni.chooseFile({
						count: 1,
						type: 'all',
						success: (res) => {
							if (res.tempFiles.length > 0) {
								let srcPath = res.tempFiles[0].path
								let path = module.resolveFilePath(srcPath);
								module.bzip2({
									srcPath: path,
									destPath: self.toBzip2Path(path),
									level: 5,
									progressListener: (progress : number) => {
										console.log('BZ2压缩进度:' + progress + '%')
									},
									completeListener: (res) => {
										if (res.success) {
											self.showToast('BZ2压缩成功:' + JSON.stringify(res.data))
										} else {
											self.showToast('BZ2压缩失败:' + res.message)
										}
									}
								})
							}
						},
						fail: (err) => {
							self.showToast('选择文件失败:' + JSON.stringify(err))
						}
					})
				})
			},
			// ==================== BZIP2 解压(仅单文件,仅 Android) ====================
			testUnBzip2(){
				let self = this;
				module.requestPermission((res)=>{
					uni.chooseFile({
						count: 1,
						type: 'all',
						success: (res) => {
							if (res.tempFiles.length > 0) {
								let srcPath = res.tempFiles[0].path
								let path = module.resolveFilePath(srcPath);
								module.unbzip2({
									srcPath: path,
									destPath: self.toUnBzip2Path(path),
									progressListener: (progress : number) => {
										console.log('BZ2解压进度:' + progress + '%')
									},
									completeListener: (res) => {
										if (res.success) {
											self.showToast('BZ2解压成功:' + JSON.stringify(res.data))
										} else {
											self.showToast('BZ2解压失败:' + res.message)
										}
									}
								})
							}
						},
						fail: (err) => {
							self.showToast('选择文件失败:' + JSON.stringify(err))
						}
					})
				})
			},
			// ==================== XZ 压缩(仅单文件) ====================
			testXz(){
				let self = this;
				module.requestPermission((res)=>{
					uni.chooseFile({
						count: 1,
						type: 'all',
						success: (res) => {
							if (res.tempFiles.length > 0) {
								let srcPath = res.tempFiles[0].path
								let path = module.resolveFilePath(srcPath);
								module.xz({
									srcPath: path,
									destPath: self.toXzPath(path),
									level: 6,
									progressListener: (progress : number) => {
										console.log('XZ压缩进度:' + progress + '%')
									},
									completeListener: (res) => {
										if (res.success) {
											self.showToast('XZ压缩成功:' + JSON.stringify(res.data))
										} else {
											self.showToast('XZ压缩失败:' + res.message)
										}
									}
								})
							}
						},
						fail: (err) => {
							self.showToast('选择文件失败:' + JSON.stringify(err))
						}
					})
				})
			},
			// ==================== XZ 解压(仅单文件) ====================
			testUnXz(){
				let self = this;
				module.requestPermission((res)=>{
					uni.chooseFile({
						count: 1,
						type: 'all',
						success: (res) => {
							if (res.tempFiles.length > 0) {
								let srcPath = res.tempFiles[0].path
								let path = module.resolveFilePath(srcPath);
								module.unxz({
									srcPath: path,
									destPath: self.toUnXzPath(path),
									progressListener: (progress : number) => {
										console.log('XZ解压进度:' + progress + '%')
									},
									completeListener: (res) => {
										if (res.success) {
											self.showToast('XZ解压成功:' + JSON.stringify(res.data))
										} else {
											self.showToast('XZ解压失败:' + res.message)
										}
									}
								})
							}
						},
						fail: (err) => {
							self.showToast('选择文件失败:' + JSON.stringify(err))
						}
					})
				})
			},
			// ==================== CPIO 归档(仅 Android) ====================
			testCpio(){
				let self = this;
				module.requestPermission((res)=>{
					uni.chooseFile({
						count: 99,
						type: 'all',
						success: (res) => {
							if (res.tempFiles.length > 0) {
								const srcPaths = res.tempFiles.map((v) => module.resolveFilePath(v.path))
								module.cpio({
									srcPaths: srcPaths,
									destPath: self.toCpioPath(srcPaths[0]),
									progressListener: (progress : number) => {
										console.log('CPIO归档进度:' + progress + '%')
									},
									completeListener: (res) => {
										if (res.success) {
											self.showToast('CPIO归档成功:' + JSON.stringify(res.data))
										} else {
											self.showToast('CPIO归档失败:' + res.message)
										}
									}
								})
							}
						},
						fail: (err) => {
							self.showToast('选择文件失败:' + JSON.stringify(err))
						}
					})
				})
			},
			// ==================== CPIO 解包(仅 Android) ====================
			testUnCpio(){
				let self = this;
				module.requestPermission((res)=>{
					uni.chooseFile({
						count: 1,
						type: 'all',
						success: (res) => {
							if (res.tempFiles.length > 0) {
								let srcPath = res.tempFiles[0].path
								let path = module.resolveFilePath(srcPath);
								module.uncpio({
									srcPath: path,
									destPath: self.toUnCpioPath(path),
									progressListener: (progress : number) => {
										console.log('CPIO解包进度:' + progress + '%')
									},
									completeListener: (res) => {
										if (res.success) {
											self.showToast('CPIO解包成功:' + JSON.stringify(res.data))
										} else {
											self.showToast('CPIO解包失败:' + res.message)
										}
									}
								})
							}
						},
						fail: (err) => {
							self.showToast('选择文件失败:' + JSON.stringify(err))
						}
					})
				})
			},
			// ==================== 路径工具方法 ====================
			/**
			 * 将任意文件路径转为对应的 .zip 路径
			 * 例如:/cache/111.pdf → /cache/111.zip
			 */
			toZipPath(srcPath : string) : string {
				const lastDot = srcPath.lastIndexOf('.')
				const lastSep = Math.max(srcPath.lastIndexOf('/'), srcPath.lastIndexOf('\\'))
				if (lastDot > lastSep) {
					return srcPath.substring(0, lastDot) + '.zip'
				}
				return srcPath + '.zip'
			},
			/**
			 * 将 .zip 路径转为解压目标目录
			 * 例如:/cache/111.zip → /cache/111
			 */
			toUnzipPath(zipPath : string) : string {
				const lower = zipPath.toLowerCase()
				if (lower.endsWith('.zip')) {
					return zipPath.substring(0, zipPath.length - 4)
				}
				return zipPath + '_unzip'
			},
			toSevenZPath(srcPath : string) : string {
				const lastDot = srcPath.lastIndexOf('.')
				const lastSep = Math.max(srcPath.lastIndexOf('/'), srcPath.lastIndexOf('\\'))
				if (lastDot > lastSep) {
					return srcPath.substring(0, lastDot) + '.7z'
				}
				return srcPath + '.7z'
			},
			toUnSevenZPath(path : string) : string {
				const lower = path.toLowerCase()
				if (lower.endsWith('.7z')) {
					return path.substring(0, path.length - 3)
				}
				return path + '_un7z'
			},
			toUnRarPath(path : string) : string {
				const lower = path.toLowerCase()
				if (lower.endsWith('.rar')) {
					return path.substring(0, path.length - 4)
				}
				return path + '_unrar'
			},
			toTarPath(srcPath : string) : string {
				const lastDot = srcPath.lastIndexOf('.')
				const lastSep = Math.max(srcPath.lastIndexOf('/'), srcPath.lastIndexOf('\\'))
				if (lastDot > lastSep) {
					return srcPath.substring(0, lastDot) + '.tar'
				}
				return srcPath + '.tar'
			},
			toUnTarPath(path : string) : string {
				const lower = path.toLowerCase()
				if (lower.endsWith('.tar')) {
					return path.substring(0, path.length - 4)
				}
				return path + '_untar'
			},
			toGzipPath(srcPath : string) : string {
				return srcPath + '.gz'
			},
			toUnGzipPath(path : string) : string {
				const lower = path.toLowerCase()
				if (lower.endsWith('.gz')) {
					return path.substring(0, path.length - 3)
				}
				return path + '_ungzip'
			},
			toBzip2Path(srcPath : string) : string {
				return srcPath + '.bz2'
			},
			toUnBzip2Path(path : string) : string {
				const lower = path.toLowerCase()
				if (lower.endsWith('.bz2')) {
					return path.substring(0, path.length - 4)
				}
				return path + '_unbz2'
			},
			toXzPath(srcPath : string) : string {
				return srcPath + '.xz'
			},
			toUnXzPath(path : string) : string {
				const lower = path.toLowerCase()
				if (lower.endsWith('.xz')) {
					return path.substring(0, path.length - 3)
				}
				return path + '_unxz'
			},
			toCpioPath(srcPath : string) : string {
				const lastDot = srcPath.lastIndexOf('.')
				const lastSep = Math.max(srcPath.lastIndexOf('/'), srcPath.lastIndexOf('\\'))
				if (lastDot > lastSep) {
					return srcPath.substring(0, lastDot) + '.cpio'
				}
				return srcPath + '.cpio'
			},
			toUnCpioPath(path : string) : string {
				const lower = path.toLowerCase()
				if (lower.endsWith('.cpio')) {
					return path.substring(0, path.length - 5)
				}
				return path + '_uncpio'
			},
			showToast(msg:string){
				if(msg == ''){
					uni.showToast({
						icon:'none',
						title:'未获取到相关信息'
					})
				}else {
					uni.showToast({
						icon:'none',
						title: msg
					})
				}
			}
		}
	}
</script>

<style>
	.page-root {
		padding: 10px 15px 20px;
	}
	.panel-card {
		margin-bottom: 16px;
	}
	.panel-title {
		font-size: 15px;
		font-weight: bold;
		color: #333333;
		margin-bottom: 8px;
	}
	.panel-line {
		font-size: 13px;
		color: #666666;
		margin-bottom: 4px;
	}
	.platform-tag {
		color: #007aff;
	}
</style>

用到的权限

安卓
<!-- 读取存储 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<!-- 写入存储 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- Android 11+ 所有文件管理权限 -->
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
鸿蒙
"requestPermissions": [
  {
    "name": "ohos.permission.READ_MEDIA",
    "reason": "$string:module_desc",
    "usedScene": {
      "abilities": ["EntryAbility"],
      "when": "always"
    }
  },
  {
    "name": "ohos.permission.WRITE_MEDIA",
    "reason": "$string:module_desc",
    "usedScene": {
      "abilities": ["EntryAbility"],
      "when": "always"
    }
  }
]
Logo

作为“人工智能6S店”的官方数字引擎,为AI开发者与企业提供一个覆盖软硬件全栈、一站式门户。

更多推荐