Android - 解压缩文件夹?

发布于 2024-10-17 20:16:45 字数 49 浏览 1 评论 0原文

我的 SD 卡上有一个 zip 文件夹,如何解压缩该文件夹(在我的应用程序代码中)?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小嗲 2024-10-24 20:16:45

我正在使用 Beginner 方法的修改版本,该方法扩展了 AsyncTask 并可以更新主线程上的观察者。逐字节压缩非常慢,应该避免。相反,更有效的方法是将大块数据复制到输出流。

package com.blarg.webviewscroller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Observable;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.commons.io.IOUtils;

import android.os.AsyncTask;
import android.util.Log;

public class UnZipper extends Observable {

    private static final String TAG = "UnZip";
    private String mFileName, mFilePath, mDestinationPath;

    public UnZipper (String fileName, String filePath, String destinationPath) {
        mFileName = fileName;
        mFilePath = filePath;
        mDestinationPath = destinationPath;
    }

    public String getFileName () {
        return mFileName;
    }

    public String getFilePath() {
        return mFilePath;
    }

    public String getDestinationPath () {
        return mDestinationPath;
    }

    public void unzip () {
        String fullPath = mFilePath + "/" + mFileName + ".zip";
        Log.d(TAG, "unzipping " + mFileName + " to " + mDestinationPath);
        new UnZipTask().execute(fullPath, mDestinationPath);
    }

    private class UnZipTask extends AsyncTask<String, Void, Boolean> {

        @SuppressWarnings("rawtypes")
        @Override
        protected Boolean doInBackground(String... params) {
            String filePath = params[0];
            String destinationPath = params[1];

            File archive = new File(filePath);
            try {
                ZipFile zipfile = new ZipFile(archive);
                for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
                    ZipEntry entry = (ZipEntry) e.nextElement();
                    unzipEntry(zipfile, entry, destinationPath);
                }
            } catch (Exception e) {
                Log.e(TAG, "Error while extracting file " + archive, e);
                return false;
            }

            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            setChanged();
            notifyObservers();
        }

        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.v(TAG, "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) {
            if (dir.exists()) {
                return;
            }
            Log.v(TAG, "Creating dir " + dir.getName());
            if (!dir.mkdirs()) {
                throw new RuntimeException("Can not create dir " + dir);
            }
        }
    }
} 

它由实现 Observer 的类使用,例如:

private void unzipWebFile(String filename) {
    String unzipLocation = getExternalFilesDir(null) + "/unzipped";
    String filePath = Environment.getExternalStorageDirectory().toString();

    UnZipper unzipper = new UnZipper(filename, filePath, unzipLocation);
    unzipper.addObserver(this);
    unzipper.unzip();
}

当解压完成时,您的观察者将获得一个 update(Observable observable, Object data) 回调。

I am using a modified version of Beginner's method that extends AsyncTask and can update Observers on the main thread. Byte by byte compression is extremely slow and should be avoided. Instead a more efficient approach is to copy large chunks of data to the output stream.

package com.blarg.webviewscroller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Observable;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.commons.io.IOUtils;

import android.os.AsyncTask;
import android.util.Log;

public class UnZipper extends Observable {

    private static final String TAG = "UnZip";
    private String mFileName, mFilePath, mDestinationPath;

    public UnZipper (String fileName, String filePath, String destinationPath) {
        mFileName = fileName;
        mFilePath = filePath;
        mDestinationPath = destinationPath;
    }

    public String getFileName () {
        return mFileName;
    }

    public String getFilePath() {
        return mFilePath;
    }

    public String getDestinationPath () {
        return mDestinationPath;
    }

    public void unzip () {
        String fullPath = mFilePath + "/" + mFileName + ".zip";
        Log.d(TAG, "unzipping " + mFileName + " to " + mDestinationPath);
        new UnZipTask().execute(fullPath, mDestinationPath);
    }

    private class UnZipTask extends AsyncTask<String, Void, Boolean> {

        @SuppressWarnings("rawtypes")
        @Override
        protected Boolean doInBackground(String... params) {
            String filePath = params[0];
            String destinationPath = params[1];

            File archive = new File(filePath);
            try {
                ZipFile zipfile = new ZipFile(archive);
                for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
                    ZipEntry entry = (ZipEntry) e.nextElement();
                    unzipEntry(zipfile, entry, destinationPath);
                }
            } catch (Exception e) {
                Log.e(TAG, "Error while extracting file " + archive, e);
                return false;
            }

            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            setChanged();
            notifyObservers();
        }

        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.v(TAG, "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) {
            if (dir.exists()) {
                return;
            }
            Log.v(TAG, "Creating dir " + dir.getName());
            if (!dir.mkdirs()) {
                throw new RuntimeException("Can not create dir " + dir);
            }
        }
    }
} 

It is used by a class that implements Observer, such as:

private void unzipWebFile(String filename) {
    String unzipLocation = getExternalFilesDir(null) + "/unzipped";
    String filePath = Environment.getExternalStorageDirectory().toString();

    UnZipper unzipper = new UnZipper(filename, filePath, unzipLocation);
    unzipper.addObserver(this);
    unzipper.unzip();
}

Your observer will get an update(Observable observable, Object data) callback when the unzip finishes.

長街聽風 2024-10-24 20:16:45
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);
        }
}

这对我有用谢谢大家

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

人间☆小暴躁 2024-10-24 20:16:45

只是 @rich.e 答案的“addon”:

doInBackground() 中,迭代 ZipEtries 后,您应该关闭文件,因为有时您希望在解压缩文件后删除该文件,并且如果文件存在,它会抛出异常没有关闭:

try {
        ZipFile zipfile = new ZipFile(archive);
        int entries = zipfile.size();
        int total = 0;
        if(onZipListener != null)
            onZipListener.onUncompressStart(archive);

        for (Enumeration<?> e = zipfile.entries(); e.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            if(onZipListener != null)
                onZipListener.onUncompressProgress(archive, (int) (total++ * 100 / entries));
            unzipEntry(zipfile, entry, path);
        }
        zipfile.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

just "addon" for @rich.e answer:

in doInBackground() after iterating through ZipEtries you should close the file, because sometimes you want do delete the file after unzipping it and it throws an exception if file was not closed:

try {
        ZipFile zipfile = new ZipFile(archive);
        int entries = zipfile.size();
        int total = 0;
        if(onZipListener != null)
            onZipListener.onUncompressStart(archive);

        for (Enumeration<?> e = zipfile.entries(); e.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            if(onZipListener != null)
                onZipListener.onUncompressProgress(archive, (int) (total++ * 100 / entries));
            unzipEntry(zipfile, entry, path);
        }
        zipfile.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文