可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

I put a zip file in the android assets. How do i extract the file in the android internal storage? I know how to get the file, but i don't know how to extract it. This is my code..

Util zip ;

zip = new Util();

zip.copyFileFromAsset(this, "myfile.zip", getExternalStorage()+

"/android/data/edu.binus.profile/");

Thanks for helping :D

回答1:

This piece of code will help you....Just pass the zipfile location and the location where you want the extracted files to be saved to this class while making an object...and call unzip method...

public class Decompress {

private String zip;

private String loc;

public Decompress(String zipFile, String location) {

zip = zipFile;

loc = location;

dirChecker("");

}

public void unzip() {

try {

FileInputStream fin = new FileInputStream(zip);

ZipInputStream zin = new ZipInputStream(fin);

ZipEntry ze = null;

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

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

if(ze.isDirectory()) {

dirChecker(ze.getName());

} else {

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

for (int c = zin.read(); c != -1; c = zin.read()) {

fout.write(c);

}

zin.closeEntry();

fout.close();

}

}

zin.close();

} catch(Exception e) {

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

}

}

private void dirChecker(String dir) {

File f = new File(_location + dir);

if(!f.isDirectory()) {

f.mkdirs();

}

}

}

回答2:

Based on Sreedev R solution,

I added the option to read the file from assets and use buffer:

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()) {

boolean success = f.createNewFile();

if (!success) {

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

continue;

}

FileOutputStream fout = new FileOutputStream(f);

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());

}

}

}

}

回答3:

Maybe you should try using a FileOutputStream in combination with an inputstream from the zip file. With a package file, this should work.

To quote @wordy from this question:

PackageManager pm = context.getPackageManager();

String apkFile = pm.getApplicationInfo(context.getPackageName(), 0).sourceDir;

ZipFile zipFile = new ZipFile(apkFile);

ZipEntry entry = zipFile.getEntry("assets/FILENAME");

myInput = zipFile.getInputStream(entry);

myOutput = new FileOutputStream(file);

byte[] buffer = new byte[1024*4];

int length;

int total = 0;

int counter = 1;

while ((length = myInput.read(buffer)) > 0) {

total += length;

counter++;

if (counter % 32 == 0) {

publishProgress(total);

}

myOutput.write(buffer, 0, length);

}

Looks like there may be problems with ProGuard but hopefully the code sample works for you.

回答4:

I haven't tested yet,but while doing a project on OCR I came across this library,where there is method of unzipping a downloaded file from the net. The exact method for unzipping file is installZipFromAssets(String sourceFilename,File destinationDir,File destinationFile) found under this class.Hope this is what you are looking for

回答5:

You can also make use of the zip4j external library that provides additional features like encryption. Also, it has functions to extract files to a particular location provided the path.

android unzip file,Unzip File in Android Assets相关推荐

  1. Android运行时候报错:android.view.InflateException: Binary XML file line #19: Binary XML file lin

    Android运行时候报错:android.view.InflateException: Binary XML file line #19: Binary XML file lin 这个问题自己大致在 ...

  2. android之数据存储,Android数据存储之File

    Android使用与其他平台类似的基于磁盘文件系统(disk-based file systems),上篇文章通过SharedPerference来进行数据存储,这次可以使用File.File 对象非 ...

  3. android studio创建文件,如何在Android Studio中创建File Templates

    标签: File Template Android Studio 我发现一个可以让写程序变得简单的方法,那就是自定义文件模板(Custom File Templates).那么什么是File Temp ...

  4. Android webview Input type=file 文件上传 解决方法

    默认的情况下在HTML中 写 <input type=file /> 的时候回弹出选择文件的窗口,但实际上在webview中默认的是不弹出窗口的 解决方法 在WebChromeClient ...

  5. 解决Failed to open .ini file C:\Users\xxx\.android\emu-update-last-check.ini for writing.

    文章目录 问题背景 解决措施 原因思考 问题背景 本人使用的是比较新的版本的 sdk,集成在 idea 中运行,本人的环境变量是通过配置 ANDROID_AVD_HOME 把 avd 设备专门放到一个 ...

  6. FileUriExposedException: file:///storage/emulated/0/Android/data/com.skyrin.bingo/cache/app/app.apk

    安卓在app更新的时候file parseuri 报错FileuriExposedException:.................exposed beyond app through Inten ...

  7. 全志H6 lichee编译lichee/tools/pack/pctools/linux/android/mkbootimg: No such file or directory

    全志H6 lichee编译lichee/tools/pack/pctools/linux/android/mkbootimg: No such file or directory https://bl ...

  8. make[1]: *** /home/kernel/msm: No such file or directory. Stop android 编译 make: *** [Makefile:24: __

    1:报错: make[1]: *** /home/kernel/msm: No such file or directory. Stop android 编译 make: *** [Makefile: ...

  9. android中将网络图片转换为file,android-将BufferedInputStream转换为文件

    我正在将图像从网络加载到本地android手机.我要写入文件的代码如下 BufferedInputStream bisMBImage=null; InputStream isImage = null; ...

最新文章

  1. 数据结构Java实现03----单向链表的插入和删除b
  2. Django model层 mysql_Django模型层(models.py)之模型创建
  3. 树莓派学习笔记(7):利用bypy实现树莓派NAS同步百度云
  4. 常问面试题总结(JAVA基础篇)
  5. 信息系统项目管理知识--信息安全
  6. S4 KNUMH的设计原理
  7. 问题 1044: [编程入门]三个字符串的排序
  8. Azure实践之如何批量为资源组虚拟机创建alert
  9. xilinx7中管脚mrcc和srcc_Xilinx 7系列FPGA收发器架构之硬件设计指导(一)
  10. python股票接口_Python 从 sina 股票数据接口读取数据,并保存到 MySQL 数据库
  11. 【vue】--利用vue-cli--搭建项目------1912--(另一个种)
  12. adb ps shell 查看进程_Appium学废系列(三) adb调试桥命令
  13. stm32——使用串口下载程序
  14. 南方cass计算表面积_CASS在工程中的应用“计算表面积”的方法
  15. IE-LAB网络实验室:HCNP培训机构 HCIE培训中心 HCIE认证培训 HCNA培训 华为面试考试时需要注意什么
  16. NAS网络文件服务器搭建流程
  17. 常见视频文件格式详解
  18. 数字经济下,银行线上场景化建设的服务颗粒度、用户忠诚度和生态融合度
  19. C语言基础级——标准输入和输出
  20. epub文件是什么文件?如何在windows系统上打开?

热门文章

  1. JQuery.autocomplete扩展功能:实现多列自动提示
  2. linux apache中文名称图片,Apache、NGINX支持中文URL图片、文件名的终极解决方案
  3. MySQL赋权navicat_mysql 的root 用户无法授权及解决navicat 远程授权提示1044问题
  4. 【离散数学中的数据结构与算法】六 排列与组合二
  5. 线程安全-常用的模式
  6. cursor.execute(sql) 执行结果集是有记录的 但是num=cursor.rownumber 返回值为0
  7. [Linux] linux下安装配置 zookeeper/redis/solr/tomcat/IK分词器 详细实例.
  8. Python小数据池
  9. [SimplePlayer] 2. 在屏幕上显示视频图像
  10. 文档基本结构标签的作用