app.archive - 归档 API
压缩和解压文件。
支持格式
zip, ipa, apk, tar, tar.gz, tar.bz2, 7z
方法
app.archive.extract(archivePath, destPath, password?)
解压归档文件。
参数:
archivePath(string) - 归档文件路径destPath(string) - 解压目标目录password(string, 可选) - 密码
返回值: boolean, error - 是否成功,失败时返回 false, error
-- 解压 zip
local ok, err = app.archive.extract("/path/to/file.zip", "/path/to/output")
-- 解压加密的 zip
app.archive.extract("/path/to/encrypted.zip", "/path/to/output", "password123")
app.archive.compress(sourcePath, archivePath, password?)
压缩文件或目录。
参数:
sourcePath(string) - 要压缩的文件或目录路径archivePath(string) - 输出归档文件路径password(string, 可选) - 密码
返回值: boolean, error - 是否成功,失败时返回 false, error
-- 压缩目录
local ok, err = app.archive.compress("/path/to/folder", "/path/to/output.zip")
-- 压缩并加密
app.archive.compress("/path/to/folder", "/path/to/output.zip", "password123")
app.archive.list(archivePath, password?)
列出归档内容。
参数:
archivePath(string) - 归档文件路径password(string, 可选) - 密码
返回值: table, error - 文件路径数组,失败时返回 nil, error
local files = app.archive.list("/path/to/file.zip")
if files then
for _, f in ipairs(files) do
app.log.info(f)
end
end
示例
解压选中的文件
function MyPlugin:handleExtract(context)
for _, file in ipairs(context.selectedFiles) do
local ext = app.path.extension(file)
if ext == "zip" or ext == "tar" or ext == "gz" then
local destDir = app.path.removeExtension(file)
if app.archive.extract(file, destDir) then
app.notification.show("完成", "已解压: " .. app.path.basename(file))
else
app.notification.show("失败", "解压失败: " .. app.path.basename(file))
end
end
end
end
压缩选中的文件夹
function MyPlugin:handleCompress(context)
for _, file in ipairs(context.selectedFiles) do
if app.file.isDirectory(file) then
local zipPath = file .. ".zip"
if app.archive.compress(file, zipPath) then
app.notification.show("完成", "已压缩: " .. app.path.basename(zipPath))
end
end
end
end