Android中如何将内部存储多文件夹压缩为单个Zip文件并实现解压?
Got it, let's walk through how to zip those internal storage folders into a single Zip on external storage, plus how to extract them later. I'll cover both user-friendly methods for regular folks and command-line/development approaches for those who prefer that.
压缩内部存储文件夹到外部存储Zip文件
方法1:用手机端文件管理器(适合普通用户)
这是最直观的方式,大部分主流文件管理器都支持Zip压缩:
- 打开你的文件管理器(比如ES文件浏览器、Solid Explorer、MiXplorer),导航到内部存储根目录
- 长按选中
databases、app_images、app_audios这几个目标文件夹(可以多选) - 点击底部工具栏的「压缩」/「归档」按钮,选择Zip作为压缩格式
- 在保存路径弹窗里,切换到外部存储(比如SD卡的根目录或你指定的文件夹),设置好Zip文件名(比如
app_backup.zip),确认后等待压缩完成
方法2:通过ADB命令(适合开发者/命令行爱好者)
如果你习惯用电脑操作,ADB是个高效的选择,前提是设备开启了USB调试,电脑安装了ADB工具:
- 用USB线连接设备到电脑,打开终端/命令提示符,输入
adb devices确认设备已成功连接 - 执行以下命令打包文件夹(替换路径为你的实际外部存储路径):
adb shell "cd /sdcard && zip -r /storage/your_external_path/app_backup.zip databases app_images app_audios"/sdcard是内部存储的默认根目录(部分设备可能是/storage/emulated/0)/storage/your_external_path替换成你的外部存储实际路径,比如SD卡路径通常是/storage/XXXX-XXXX(可以用adb shell ls /storage查看所有存储路径)-r参数表示递归压缩文件夹内的所有子文件和子文件夹
解压Zip文件还原到内部存储
方法1:用手机端文件管理器
和压缩操作一样简单:
- 打开文件管理器,导航到外部存储找到你生成的
app_backup.zip - 长按文件,选择「解压」/「提取」选项
- 在解压路径设置中,选择内部存储的根目录(或者你想要还原的具体位置),确认后等待解压完成,原来的
databases等文件夹会自动恢复到对应位置
方法2:通过ADB命令
同样用ADB快速操作:
- 确保设备连接电脑且ADB可用
- 执行解压命令(替换Zip文件路径为你的实际路径):
adb shell "cd /sdcard && unzip /storage/your_external_path/app_backup.zip"/sdcard是内部存储根目录,执行后Zip内的文件夹会直接还原到这里
额外:Android应用开发中实现该功能(如果是开发需求)
如果你要在自己的App里集成备份/还原功能,可以用Java/Kotlin的Zip API来实现,核心逻辑如下(Kotlin示例):
压缩核心代码
import java.io.File import java.io.FileOutputStream import java.util.zip.ZipEntry import java.util.zip.ZipOutputStream fun zipTargetFolders(sourceFolderPaths: List<String>, zipOutputPath: String) { ZipOutputStream(FileOutputStream(zipOutputPath)).use { zipOut -> sourceFolderPaths.forEach { folderPath -> val sourceFolder = File(folderPath) // 遍历文件夹下的所有文件和子文件夹 sourceFolder.walkTopDown().forEach { file -> // 计算Zip条目相对路径,避免写入完整绝对路径 val entryRelativePath = file.absolutePath.substring(sourceFolder.parentFile.absolutePath.length + 1) val zipEntry = if (file.isDirectory) { ZipEntry("$entryRelativePath/") // 文件夹条目要加斜杠 } else { ZipEntry(entryRelativePath) } zipOut.putNextEntry(zipEntry) if (file.isFile) { // 写入文件内容到Zip file.inputStream().use { it.copyTo(zipOut) } } zipOut.closeEntry() } } } }
调用时传入内部存储的文件夹路径列表和外部存储的Zip输出路径即可,注意要申请对应的存储权限(Android 10+需适配分区存储或申请MANAGE_EXTERNAL_STORAGE权限)。
解压核心代码
import java.io.File import java.io.FileInputStream import java.io.FileOutputStream import java.util.zip.ZipEntry import java.util.zip.ZipInputStream fun unzipFile(zipFilePath: String, targetDirPath: String) { val targetDir = File(targetDirPath) if (!targetDir.exists()) targetDir.mkdirs() ZipInputStream(FileInputStream(zipFilePath)).use { zipIn -> var entry: ZipEntry? = zipIn.nextEntry while (entry != null) { val entryFile = File(targetDir, entry.name) if (entry.isDirectory) { entryFile.mkdirs() } else { // 确保父文件夹存在 entryFile.parentFile?.mkdirs() // 写入解压后的文件 FileOutputStream(entryFile).use { zipIn.copyTo(it) } } zipIn.closeEntry() entry = zipIn.nextEntry } } }
内容的提问来源于stack exchange,提问作者user6931342




