在 Android 中解压缩文件夹 [英] Unzip Folder in Android

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

问题描述

我尝试制作一个显示 100 张图片的应用.我想压缩照片文件夹,然后将其放在我的项目中.现在我需要了解如何将 zip 文件夹复制到内部存储,然后在 android 中解压缩?

I try to make an app that show 100 pictures. I want to zip folder of photos and then put it on my project. now i need to understand How can copy a zip folder to internal storage,And then unzip it in android?

推荐答案

您可以将 .zip 文件包含在 apk 的 Assets 文件夹中,并将它们包含在代码中,将 .zip 复制到内部存储并使用 ZipInputStream.

You can include your .zip file on Assets folder of apk, and them on code, copy the .zip to internal storage and unzipping using a ZipInputStream.

首先将 de .zip 文件复制到内部存储,然后解压缩文件:

First copy de .zip file to internal storage, and after unzip a file:

    protected void copyFromAssetsToInternalStorage(String filename){
        AssetManager assetManager = getAssets();

        try {
            InputStream input = assetManager.open(filename);
            OutputStream output = openFileOutput(filename, Context.MODE_PRIVATE);

             copyFile(input, output);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void unZipFile(String filename){
        try {
            ZipInputStream zipInputStream = new ZipInputStream(openFileInput(filename));
            ZipEntry zipEntry;

            while((zipEntry = zipInputStream.getNextEntry()) != null){
                FileOutputStream zipOutputStream = openFileOutput(zipEntry.getName(), MODE_PRIVATE);

                int length;
                byte[] buffer = new byte[1024];

                while((length = zipInputStream.read(buffer)) > 0){
                    zipOutputStream.write(buffer, 0, length);
                }

                zipOutputStream.close();
                zipInputStream.closeEntry();
            }
            zipInputStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1){
            out.write(buffer, 0, read);
        }
    }

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

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