package com.trust.ywx.utils import android.content.Context import android.util.Log import com.trust.ywx.AppManager import java.io.File import java.io.FileInputStream import java.io.FileOutputStream import java.io.IOException import java.util.zip.ZipEntry import java.util.zip.ZipInputStream class FileHelper { companion object { fun getDirPath(context: Context?, type: String?): String { return (context ?: AppManager.lastActivity())?.getExternalFilesDir(type)?.absolutePath!! } fun getFilePath(name: String, context: Context?, type: String?): String { val path = getDirPath(context, type) + File.separator + name return path } fun copyAssetFileToInternalStorage( context: Context, assetFileName: String, destFile: File ) { val assetManager = context.assets try { assetManager.open(assetFileName).use { `in` -> FileOutputStream(destFile).use { out -> val buffer = ByteArray(1024) var read: Int while ((`in`.read(buffer).also { read = it }) != -1) { out.write(buffer, 0, read) } Log.d( ">>>>>>>>>>>>>>CopyAssets", "Copied: " + assetFileName + " to " + destFile.getAbsolutePath() ) } } } catch (e: IOException) { Log.e(">>>>>>>>>>>>>>CopyAssets", "Failed to copy asset: $assetFileName", e) } } @Throws(IOException::class) fun unzip(zipFile: File, targetDir: File) { if (!targetDir.exists()) targetDir.mkdirs() ZipInputStream(FileInputStream(zipFile)).use { zipIn -> var entry: ZipEntry? while ((zipIn.getNextEntry().also { entry = it }) != null) { val file = File(targetDir, entry!!.name) if (entry.isDirectory) { file.mkdirs() } else { // 确保父目录存在 val parent = file.getParentFile() if (!parent.exists()) parent.mkdirs() FileOutputStream(file).use { fos -> val buffer = ByteArray(1024) var len: Int while ((zipIn.read(buffer).also { len = it }) > 0) { fos.write(buffer, 0, len) } } } zipIn.closeEntry() } } } } }