当前位置: 首页 > news >正文

Android 常用文件操作方法

Android 常用文件操作方法

文件压缩、md5计算

package io.github.okhttplearn.ui.screenimport android.content.Context
import android.os.Environment
import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import io.github.okhttplearn.ui.theme.OkhttpLearnTheme
import io.github.okhttplearn.utils.Utils
import okio.buffer
import okio.sink
import java.io.Fileprivate const val TAG: String = "WorldScreen"@Composable
internal fun WorldScreen(modifier: Modifier = Modifier,snackBarHostState: SnackbarHostState,
) {val context: Context = LocalContext.currentval srcFile: File = remember {File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "src.txt")}val destFile: File = remember {File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "dest.gz")}Column(modifier = modifier) {Text(text = "写文件", modifier = Modifier.fillMaxWidth().padding(all = 5.dp).background(color = Color(color = 0xFFF8BBD0),shape = RoundedCornerShape(size = 5.dp)).padding(all = 5.dp).clickable {Log.i(TAG, "WorldScreen -> srcFile: $srcFile")if (srcFile.parentFile?.exists() == false) {Log.i(TAG, "WorldScreen -> isSuccess: ${srcFile.parentFile?.mkdirs()}")}srcFile.sink().buffer().use { bufferedSink ->for (i in 0 until 1000) {bufferedSink.writeUtf8("Hello World: $i")}}})Text(text = "压缩zip", modifier = Modifier.fillMaxWidth().padding(all = 5.dp).background(color = Color(color = 0xFFF8BBD0),shape = RoundedCornerShape(size = 5.dp)).padding(all = 5.dp).clickable {Log.i(TAG, "WorldScreen -> srcFile: $srcFile, destFile: $destFile")Utils.compressFileToGzip2(srcFile, destFile)})Text(text = "压缩gzip", modifier = Modifier.fillMaxWidth().padding(all = 5.dp).background(color = Color(color = 0xFFF8BBD0),shape = RoundedCornerShape(size = 5.dp)).padding(all = 5.dp))Text(text = "计算md5", modifier = Modifier.fillMaxWidth().padding(all = 5.dp).background(color = Color(color = 0xFFF8BBD0),shape = RoundedCornerShape(size = 5.dp)).padding(all = 5.dp).clickable {val md5: String = Utils.getFileMd5(destFile)Log.i(TAG, "WorldScreen -> md5: $md5")})}
}@Preview(showBackground = true)
@Composable
private fun WorldScreenPreview() {OkhttpLearnTheme {val snackBarHostState: SnackbarHostState = remember { SnackbarHostState() }WorldScreen(modifier = Modifier, snackBarHostState = snackBarHostState)}
}

工具方法

package io.github.okhttplearn.utilsimport okio.BufferedSink
import okio.BufferedSource
import okio.buffer
import okio.gzip
import okio.sink
import okio.source
import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.io.File
import java.io.InputStream
import java.security.MessageDigest
import java.util.zip.GZIPOutputStreamprivate const val TAG: String = "Utils"internal object Utils {internal fun compressFileToGzip1(sourceFile: File, targetFile: File) {targetFile.outputStream().buffered().use { fileOutputStream: BufferedOutputStream ->GZIPOutputStream(fileOutputStream).buffered().use { gzipOutputStream: BufferedOutputStream ->sourceFile.inputStream().buffered().use { bufferedInputStream: BufferedInputStream ->bufferedInputStream.copyTo(out = gzipOutputStream)}}}}/*** ```shell* PS C:\Users\29051> (Get-FileHash -Path C:\Users\29051\Downloads\dest.gz -Algorithm MD5).Hash.ToLower()* 98cbb6b41e3a4aeec9e35dfec9351673* ```*/internal fun compressFileToGzip2(sourceFile: File, targetFile: File) {targetFile.sink().gzip().buffer().use { bufferedSink: BufferedSink ->sourceFile.source().buffer().use { bufferedSource: BufferedSource ->bufferedSource.readAll(sink = bufferedSink)}}}internal fun getFileMd5(file: File): String {val digest: MessageDigest = MessageDigest.getInstance("MD5")file.inputStream().buffered().use { inputStream: InputStream ->val bytes = ByteArray(1024 * 8)var length: Intwhile (inputStream.read(bytes).also { length = it } > 0) {digest.update(bytes, 0, length)}val md5Bytes: ByteArray = digest.digest()return md5Bytes.joinToString(separator = "") { "%02x".format(it) }}}}

zip文件压缩可以看我的这篇文章

zip文件压缩

还有的话会继续在这篇文章补充

http://www.rkmt.cn/news/57127.html

相关文章:

  • 穿透EDA
  • 2025年热门的MEE自动复位防火阀厂家最新权威推荐排行榜
  • 2025年靠谱的不锈钢防火阀行业内知名厂家排行榜
  • quick lauching of git-bash
  • 2025年口碑好的天花明装线槽厂家最新推荐权威榜
  • 2025年微波等离子清洗机源头厂家权威推荐榜单:射频等离子清洗机/连续式等离子清洗机/在线式微波等离子清洗机源头厂家精选
  • FontCreator的Unicode分类名称
  • 【JVM】低延迟垃圾收集器:Shenandoah收集器与ZGC收集器 - 实践
  • 2025年11月中国悬浮门品牌口碑推荐榜单前十强深度解析
  • 2025年手持贴标机生产厂家排名TOP10:陕西彩航包装技术领先
  • 深入解析:安卓MX播放器 软件APP MXPlayer v2.1.2、1.93.4 解锁付费PRO版
  • 这款在线番茄钟工具,帮你找回高效专注的工作节奏
  • 2025年11月中国悬浮门源头厂家推荐榜解读
  • 2025 最新升降机实力厂家排行榜:央企合作 + 年销 2800 台标杆,液压 / 电动 / 无障碍设备优质品牌推荐移动式升降机/轮椅升降机/货梯升降机/汽车升降机公司推荐
  • 2025年废气废液焚烧品牌前十强排名:徐州海德测控技术有限公司领跑行业
  • 2025年低氮燃烧器厂家排名:徐州海德测控技术有限公司领跑行业
  • 2025年11月四川带锯床品牌口碑推荐列表
  • iOS进阶1-combine
  • Dart 中并发、异步编程
  • 这款免费利润计算工具,帮你轻松理清收入与支出
  • 2025 最新人机交互显示模组厂家权威推荐榜:旋钮屏 / TFT 智能屏等核心产品优选高品质企业点阵智能液晶模块 / 智能液晶显示模块 / TFT 智能液晶显示模块人机交互显示模组公司推荐
  • vue3 随机生产音符扩散效果
  • ASM1042A3S车规级CANFD芯片在两轮车和平衡车控制器优秀的方案中的技术应用
  • 2025年别墅供暖厂家权威推荐榜单:别墅锅炉/联排别墅供暖/小别墅供暖源头厂家精选
  • 2025年11月央国企求职机构选择指南:权威榜单与避坑要点
  • 2025 年 11 月预制袋套袋机,袋中袋套袋机,食品套袋机,八边封套袋机最新推荐,产能、专利、环保三维数据透视!
  • Linux 中截取文本的最后几个字符
  • 2025年新疆初三复读班权威推荐榜单:中考复读/初三集训班/本地中考复读学校精选
  • 11.21题解
  • NHVR-20 型油气回收在线监测系统:工业级全场景油气泄漏防控方案 - 详解