android cc文件,解压Android文件中的文件

该博客内容展示了如何在Android环境中使用Java代码从Assets或文件系统中读取ZIP文件,并将其解压缩到指定目录。主要涉及`Decompress`类中的静态方法,包括从Assets解压和从文件路径解压,利用`ZipInputStream`遍历并解压每个条目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于Sreedev R染料溶液, 我加入到阅读的资产和使用缓冲区中的文件的选项:

package com.pixoneye.api.utils;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipInputStream;

import android.content.Context;

import android.util.Log;

public class Decompress {

private static final int BUFFER_SIZE = 1024 * 10;

private static final String TAG = "Decompress";

public static void unzipFromAssets(Context context, String zipFile, String destination) {

try {

if (destination == null || destination.length() == 0)

destination = context.getFilesDir().getAbsolutePath();

InputStream stream = context.getAssets().open(zipFile);

unzip(stream, destination);

} catch (IOException e) {

e.printStackTrace();

}

}

public static void unzip(String zipFile, String location) {

try {

FileInputStream fin = new FileInputStream(zipFile);

unzip(fin, location);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

public static void unzip(InputStream stream, String destination) {

dirChecker(destination, "");

byte[] buffer = new byte[BUFFER_SIZE];

try {

ZipInputStream zin = new ZipInputStream(stream);

ZipEntry ze = null;

while ((ze = zin.getNextEntry()) != null) {

Log.v(TAG, "Unzipping " + ze.getName());

if (ze.isDirectory()) {

dirChecker(destination, ze.getName());

} else {

File f = new File(destination + ze.getName());

if (!f.exists()) {

FileOutputStream fout = new FileOutputStream(destination + ze.getName());

int count;

while ((count = zin.read(buffer)) != -1) {

fout.write(buffer, 0, count);

}

zin.closeEntry();

fout.close();

}

}

}

zin.close();

} catch (Exception e) {

Log.e(TAG, "unzip", e);

}

}

private static void dirChecker(String destination, String dir) {

File f = new File(destination + dir);

if (!f.isDirectory()) {

boolean success = f.mkdirs();

if (!success) {

Log.w(TAG, "Failed to create folder " + f.getName());

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值