Android的 - 解压缩文件夹? [英] Android - Unzip a folder?

查看:155
本文介绍了Android的 - 解压缩文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的SD卡中的zip文件夹,我怎么解压的文件夹(在我的应用程序code)?

解决方案

 静态处理器将myHandler;
ProgressDialog myProgress;

公共无效unzipFile(文件压缩文件){
        myProgress = ProgressDialog.show(的getContext(),解压缩zip,
                        解压文件...,真,假);
        文件的压缩文件=压​​缩文件;
        字符串目录= NULL;
        目录= zipFile.getParent();
        目录=目录+/;
        将myHandler =新的处理程序(){

                @覆盖
                公共无效的handleMessage(信息MSG){
                        //处理传入这里的消息
                        开关(msg.what){
                        情况下0:
                                //更新进度条
                                myProgress.setMessage(+(字符串)msg.obj);
                                打破;
                        情况1:
                                myProgress.cancel();
                                吐司面包= Toast.makeText(的getContext()
                                                邮编提取成功,
Toast.LENGTH_SHORT);
                                toast.show();
                                provider.refresh();
                                打破;
                        案例2:
                                myProgress.cancel();
                                打破;
                        }
                        super.handleMessage(MSG);
                }

        };
        螺纹workthread =新主题(新解压缩(压缩文件,目录));
        workthread.start();
}

公共类解压实现Runnable {

        文件归档;
        字符串outputDir;

        公共解压(文件ziparchive,字符串目录){
                存档= ziparchive;
                outputDir =目录;
        }

        公共无效日志(字符串日志){
                Log.v(解压,日志);
        }

        @燮pressWarnings(未登记)
        公共无效的run(){
                信息MSG;
                尝试 {
                        ZipFile的zip文件=新的ZipFile(存档);
                        对于(枚举E = zipfile.entries();
e.hasMoreElements()){
                                ZipEntry的入口=(ZipEntry的)e.nextElement();
                                味精=新的Message();
                                msg.what = 0;
                                msg.obj =提取+ entry.getName();
                                myHandler.sendMessage(MSG);
                                unzipEntry(zip文件,录入,outputDir);
                        }
                }赶上(例外五){
                        登录(+归档,而解压文件出错);
                }
                味精=新的Message();
                msg.what = 1;
                myHandler.sendMessage(MSG);
        }

        @燮pressWarnings(未登记)
        公共无效unzipArchive(文件存档,串outputDir){
                尝试 {
                        ZipFile的zip文件=新的ZipFile(存档);
                        对于(枚举E = zipfile.entries();
e.hasMoreElements()){
                                ZipEntry的入口=(ZipEntry的)e.nextElement();
                                unzipEntry(zip文件,录入,outputDir);
                        }
                }赶上(例外五){
                        登录(+归档,而解压文件出错);
                }
        }

        私人无效unzipEntry(ZipFile中的压缩文件,ZipEntry的进入,
                        字符串outputDir)抛出IOException异常{

                如果(entry.isDirectory()){
                        createDir(新文件(outputDir,entry.getName()));
                        返回;
                }

                文件OUTPUTFILE =新的文件(outputDir,entry.getName());
                如果(!outputFile.getParentFile()。存在()){
                        createDir(outputFile.getParentFile());
                }

                日志(提取+项);
                的BufferedInputStream的InputStream =新
的BufferedInputStream(zip文件
                                .getInputStream(条目));
                的BufferedOutputStream的OutputStream =新的BufferedOutputStream(
                                新的FileOutputStream(OUTPUTFILE));

                尝试 {
                        IOUtils.copy(的InputStream,OutputStream的);
                } 最后 {
                        outputStream.close();
                        inputStream.close();
                }
        }

        私人无效createDir(文件目录){
                日志(创建目录+ dir.getName());
                如果(!dir.mkdirs())
                        抛出新的RuntimeException(无法创建目录+方向);
        }
}
 

这是为我工作的人的感谢。

I have a zip folder on my SD card, how do i unzip the folder (within my application code) ?

解决方案

static Handler myHandler;
ProgressDialog myProgress;

public void unzipFile(File zipfile) {
        myProgress = ProgressDialog.show(getContext(), "Extract Zip",
                        "Extracting Files...", true, false);
        File zipFile = zipfile;
        String directory = null;
        directory = zipFile.getParent();
        directory = directory + "/";
        myHandler = new Handler() {

                @Override
                public void handleMessage(Message msg) {
                        // process incoming messages here
                        switch (msg.what) {
                        case 0:
                                // update progress bar
                                myProgress.setMessage("" + (String) msg.obj);
                                break;
                        case 1:
                                myProgress.cancel();
                                Toast toast = Toast.makeText(getContext(),
                                                "Zip extracted successfully", 
Toast.LENGTH_SHORT);
                                toast.show();
                                provider.refresh();
                                break;
                        case 2:
                                myProgress.cancel();
                                break;
                        }
                        super.handleMessage(msg);
                }

        };
        Thread workthread = new Thread(new UnZip(zipFile, directory));
        workthread.start();
}

public class UnZip implements Runnable {

        File archive;
        String outputDir;

        public UnZip(File ziparchive, String directory) {
                archive = ziparchive;
                outputDir = directory;
        }

        public void log(String log) {
                Log.v("unzip", log);
        }

        @SuppressWarnings("unchecked")
        public void run() {
                Message msg;
                try {
                        ZipFile zipfile = new ZipFile(archive);
                        for (Enumeration e = zipfile.entries(); 
e.hasMoreElements();) {
                                ZipEntry entry = (ZipEntry) e.nextElement();
                                msg = new Message();
                                msg.what = 0;
                                msg.obj = "Extracting " + entry.getName();
                                myHandler.sendMessage(msg);
                                unzipEntry(zipfile, entry, outputDir);
                        }
                } catch (Exception e) {
                        log("Error while extracting file " + archive);
                }
                msg = new Message();
                msg.what = 1;
                myHandler.sendMessage(msg);
        }

        @SuppressWarnings("unchecked")
        public void unzipArchive(File archive, String outputDir) {
                try {
                        ZipFile zipfile = new ZipFile(archive);
                        for (Enumeration e = zipfile.entries(); 
e.hasMoreElements();) {
                                ZipEntry entry = (ZipEntry) e.nextElement();
                                unzipEntry(zipfile, entry, outputDir);
                        }
                } catch (Exception e) {
                        log("Error while extracting file " + archive);
                }
        }

        private void unzipEntry(ZipFile zipfile, ZipEntry entry,
                        String outputDir) throws IOException {

                if (entry.isDirectory()) {
                        createDir(new File(outputDir, entry.getName()));
                        return;
                }

                File outputFile = new File(outputDir, entry.getName());
                if (!outputFile.getParentFile().exists()) {
                        createDir(outputFile.getParentFile());
                }

                log("Extracting: " + entry);
                BufferedInputStream inputStream = new 
BufferedInputStream(zipfile
                                .getInputStream(entry));
                BufferedOutputStream outputStream = new BufferedOutputStream(
                                new FileOutputStream(outputFile));

                try {
                        IOUtils.copy(inputStream, outputStream);
                } finally {
                        outputStream.close();
                        inputStream.close();
                }
        }

        private void createDir(File dir) {
                log("Creating dir " + dir.getName());
                if (!dir.mkdirs())
                        throw new RuntimeException("Can not create dir " + dir);
        }
}

This is what worked for me thanks people

这篇关于Android的 - 解压缩文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆