<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>