Android 不解压直接读取zip包

之前项目中遇到个需求,总监让我们把从服务器下载下来的资源不解压直接读取里面的资源,这样的话就省去了一个个校验资源是否正确的步骤,听着貌似有点道理。。。废话不多说直接上代码。

目前我所试验过的可以读取的资源有文本、图片、xml文件。

文本:
zip包目录结构:res/txt/data.json
文件sd卡路径:android.os.Environment.getExternalStorageDirectory() + “/res.zip”

public static String readDataFile(String file) throws Exception {
        //截取路径的文件名 res
        String fileName = file.substring(file.length() - 7, file.length() - 4);
        ZipFile zf = new ZipFile(file);
        InputStream in = new BufferedInputStream(new FileInputStream(file));
        ZipInputStream zin = new ZipInputStream(in);
        ZipEntry ze;
        while ((ze = zin.getNextEntry()) != null) {
            if (ze.isDirectory()) {
                //Do nothing
            } else {
                if (ze.getName().equals(fileName + "/txt/data.json")) {
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(zf.getInputStream(ze)));
                    String line;
                    while ((line = br.readLine()) != null) {
                        return line;
                    }
                    br.close();
                }
            }
        }
        zin.closeEntry();
        return "";
    }

上面方法比较简单没什么好说的,大家理解就行,有点需要注意的就是在判断是否是想要读取的文件的时候,这里的路径是以zip的压缩目录为根目录做比较。也就是if (ze.getName().equals(fileName + "/txt/data.json")) 这句话中的fileName 当前值为res。最后返回读取的内容String就完事了。

图片和xml文件的读取都差不多,下面直接贴出代码了。

图片:

zip包目录结构:res/pic/haha.png
文件sd卡路径:android.os.Environment.getExternalStorageDirectory() + “/res.zip”

public static Bitmap readGuidePic(String file, String ResId) throws Exception {
        String fileName = file.substring(file.length() - 7, file.length() - 4);
        ZipFile zf = new ZipFile(file);
        InputStream in = new BufferedInputStream(new FileInputStream(file));
        ZipInputStream zin = new ZipInputStream(in);
        ZipEntry ze;
        while ((ze = zin.getNextEntry()) != null) {
            if (ze.isDirectory()) {
                //Do nothing
            } else {
                Log.i("tag", "file - " + ze.getName() + " : " + ze.getSize() + " bytes");
                if (ze.getName().equals(fileName + "/pic/haha.png")) {
                    InputStream is = zf.getInputStream(ze);
                    Bitmap bitmap = BitmapFactory.decodeStream(is);
                    return bitmap;
                }
            }
        }
        zin.closeEntry();
        return null;
    }

xml文件:
zip包目录结构:res/xml/app.xml
文件sd卡路径:android.os.Environment.getExternalStorageDirectory() + “/res.zip”

public static InputStream readAppFile(String file) throws IOException {
        String fileName = file.substring(file.length() - 7, file.length() - 4);
        ZipFile zf = new ZipFile(file);
        InputStream in = new BufferedInputStream(new FileInputStream(file));
        ZipInputStream zin = new ZipInputStream(in);
        ZipEntry ze;
        while ((ze = zin.getNextEntry()) != null) {
            if (ze.isDirectory()) {
                //Do nothing
            } else {
                if (ze.getName().equals(fileName + "/xml/app.xml")) {
                    InputStream inputStream = zf.getInputStream(ze);
                    return inputStream;
                }
            }
        }
        zin.closeEntry();
        return null;
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值