安卓 解压缩文件

2024-08-29 15:48
文章标签 安卓 压缩文件

本文主要是介绍安卓 解压缩文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

解压缩文件


public void showUnzipDialog() {new AlertDialog.Builder(this).setTitle("确认").setMessage("是否解压?").setPositiveButton("是", new DialogInterface.OnClickListener() {@Override
                public void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub
                    doZipExtractorWork();
                }}).setNegativeButton("否", new DialogInterface.OnClickListener() {@Override
                public void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub
                }}).show();
}
public void doZipExtractorWork() {ZipExtractorTask task = new ZipExtractorTask("/mnt/sdcard/test.zip", "/mnt/sdcard/test", this, true);
    task.execute();
}




import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.util.Log;

public class ZipExtractorTask extends AsyncTask<Void, Integer, Long> {private final String TAG = "ZipExtractorTask";
    private final File mInput;
    private final File mOutput;
    private final ProgressDialog mDialog;
    private int mProgress = 0;
    private final Context mContext;
    private boolean mReplaceAll;

    public ZipExtractorTask(String in, String out, Context context, boolean replaceAll) {super();
        mInput = new File(in);
        mOutput = new File(out);
        if (!mOutput.exists()) {if (!mOutput.mkdirs()) {Log.e(TAG, "Failed to make directories:" + mOutput.getAbsolutePath());
            }}if (context != null) {mDialog = new ProgressDialog(context);
        } else {mDialog = null;
        }mContext = context;
        mReplaceAll = replaceAll;
    }@Override
    protected Long doInBackground(Void... params) {// TODO Auto-generated method stub
        return unzip();
    }@Override
    protected void onPostExecute(Long result) {// TODO Auto-generated method stub
        //super.onPostExecute(result);
        if (mDialog != null && mDialog.isShowing()) {mDialog.dismiss();
        }if (isCancelled())return;
    }@Override
    protected void onPreExecute() {// TODO Auto-generated method stub
        //super.onPreExecute();
        if (mDialog != null) {mDialog.setTitle("Extracting");
            mDialog.setMessage(mInput.getName());
            mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mDialog.setOnCancelListener(new OnCancelListener() {@Override
                public void onCancel(DialogInterface dialog) {// TODO Auto-generated method stub
                    cancel(true);
                }});
            mDialog.show();
        }}@Override
    protected void onProgressUpdate(Integer... values) {// TODO Auto-generated method stub
        //super.onProgressUpdate(values);
        if (mDialog == null)return;
        if (values.length > 1) {int max = values[1];
            mDialog.setMax(max);
        } else
            mDialog.setProgress(values[0].intValue());
    }private long unzip() {long extractedSize = 0L;
        Enumeration<ZipEntry> entries;
        ZipFile zip = null;
        try {zip = new ZipFile(mInput);//zip 文件路径
            long uncompressedSize = getOriginalSize(zip);
            publishProgress(0, (int) uncompressedSize);//刷新进度条

            entries = (Enumeration<ZipEntry>) zip.entries();
            while (entries.hasMoreElements()) {ZipEntry entry = entries.nextElement();
                if (entry.isDirectory()) {continue;
                }File destination = new File(mOutput, entry.getName());
                if (!destination.getParentFile().exists()) {Log.e(TAG, "make=" + destination.getParentFile().getAbsolutePath());
                    destination.getParentFile().mkdirs();
                }if (destination.exists() && mContext != null && !mReplaceAll) {}ProgressReportingOutputStream outStream = new ProgressReportingOutputStream(destination);
                extractedSize += copy(zip.getInputStream(entry), outStream);
                outStream.close();
            }} catch (ZipException e) {// TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {// TODO Auto-generated catch block
            e.printStackTrace();
        } finally {try {zip.close();
            } catch (IOException e) {// TODO Auto-generated catch block
                e.printStackTrace();
            }}return extractedSize;
    }/**
     * 获取   大小
     *
     * @param file
     * @return
     */
    private long getOriginalSize(ZipFile file) {Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) file.entries();
        long originalSize = 0l;
        while (entries.hasMoreElements()) {ZipEntry entry = entries.nextElement();
            if (entry.getSize() >= 0) {originalSize += entry.getSize();
            }}return originalSize;
    }private int copy(InputStream input, OutputStream output) {byte[] buffer = new byte[1024 * 8];
        BufferedInputStream in = new BufferedInputStream(input, 1024 * 8);
        BufferedOutputStream out = new BufferedOutputStream(output, 1024 * 8);
        int count = 0, n = 0;
        try {while ((n = in.read(buffer, 0, 1024 * 8)) != -1) {out.write(buffer, 0, n);
                count += n;
            }out.flush();
        } catch (IOException e) {// TODO Auto-generated catch block
            e.printStackTrace();
        } finally {try {out.close();
            } catch (IOException e) {// TODO Auto-generated catch block
                e.printStackTrace();
            }try {in.close();
            } catch (IOException e) {// TODO Auto-generated catch block
                e.printStackTrace();
            }}return count;
    }private final class ProgressReportingOutputStream extends FileOutputStream {public ProgressReportingOutputStream(File file)throws FileNotFoundException {super(file);
            // TODO Auto-generated constructor stub
        }@Override
        public void write(byte[] buffer, int byteOffset, int byteCount)throws IOException {// TODO Auto-generated method stub
            super.write(buffer, byteOffset, byteCount);
            mProgress += byteCount;
            publishProgress(mProgress);
        }}
}

这篇关于安卓 解压缩文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1118289

相关文章

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法

消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法   消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法 [转载]原地址:http://blog.csdn.net/x605940745/article/details/17911115 消除SDK更新时的“

安卓玩机工具------小米工具箱扩展工具 小米机型功能拓展

小米工具箱扩展版                     小米工具箱扩展版 iO_Box_Mi_Ext是由@晨钟酱开发的一款适用于小米(MIUI)、多亲(2、2Pro)、多看(多看电纸书)的多功能工具箱。该工具所有功能均可以免root实现,使用前,请打开开发者选项中的“USB调试”  功能特点 【小米工具箱】 1:冻结MIUI全家桶,隐藏状态栏图标,修改下拉通知栏图块数量;冻结

安卓开发板_联发科MTK开发评估套件串口调试

串口调试 如果正在进行lk(little kernel ) 或内核开发,USB 串口适配器( USB 转串口 TTL 适配器的简称)对于检查系统启动日志非常有用,特别是在没有图形桌面显示的情况下。 1.选购适配器 常用的许多 USB 转串口的适配器,按芯片来分,有以下几种: CH340PL2303CP2104FT232 一般来说,采用 CH340 芯片的适配器,性能比较稳定,价

安卓实现弹出软键盘屏幕自适应调整

今天,我通过尝试诸多方法,最终实现了软键盘弹出屏幕的自适应。      其实,一开始我想通过EditText的事件来实现,后来发现,安卓自带的函数十分强大,只需几行代码,便可实现。实现如下:     在Manifest中设置activity的属性:android:windowSoftInputMode="adjustUnspecified|stateHidden|adjustResi

linux下zip加密压缩文件

zip -q -r  -P password zipfile.zip sourcefiles.txt                            #password 是加密密码 zipfile.zip 是生成的压缩文件 sourcefiles.txt 是被压缩的文件   zip [参数] <压缩包> <源文件>   使用zip格式打包文件     -r 递归,将指定目

安卓错误经验分析之 R cannot be resolved to a variable

当出现 R cannot be resolved to a variable  错误的时候,不能采用编译器建议的修改方法,试着clean一下,然后查找gen文件夹下R.java是否丢失,如果不存在R.java,程序没有报错且采用其它方法均无效,八成是res文件夹下的layout或者manifest出现错误没有显示出来,需要自己查一遍,否则无法根本解决问题,盲目修改代码是没用的。

uni-app 扫码优化:谈谈我是如何提升安卓 App 扫码准确率的

一. 前言 之前的一个项目遭到用户吐槽:“你们这个 App 扫码的正确率太低了,尤其是安卓的设备。经常性的扫码扫不出来,就算是扫出来了,也是错误的结果!” 由于之前是扫描二维码的需求,所以没有对扫描条形码做严格的测试,客户提示说是条形码扫描效率低下。随即,我用自己的手机测试了一下,在安卓手机上确实有这样的问题,扫码准确率确实是低,尤其是条形码,扫码效率慢且不准确。扫描二维码的的效率还算可以

安卓金币字符串转换成三位一个逗号的格式

DecimalFormat df1 = new DecimalFormat("###,###,###,##0.##");         return df1.format(Double.parseDouble(“ 17352208.25 ”)); 输出   17,352,208,25

安卓开发的无线adb

无线adb调试程序,和 电脑手机之间传输文件 使用无线 adb 从 远程手机 上 下载 文件 adb pull sdcard/mapgis/data G: 将 data 文件夹里的 所有 文件 下载 到 电脑 的 G: 盘目录下 现在我有一个小方法说不定可以帮助某些人哦(多为不是android开发人员) 1. 条件如下: android 手机  wifi无线连