Reading files from a ZIP file in your Android assets folder Reading files from a ZIP file in your Android assets folder android android

Reading files from a ZIP file in your Android assets folder


This works for me:

private void loadzip(String folder, InputStream inputStream) throws IOException{    ZipInputStream zipIs = new ZipInputStream(inputStream);     ZipEntry ze = null;            while ((ze = zipIs.getNextEntry()) != null) {                FileOutputStream fout = new FileOutputStream(folder +"/"+ ze.getName());                byte[] buffer = new byte[1024];                int length = 0;                while ((length = zipIs.read(buffer))>0) {                fout.write(buffer, 0, length);                }                zipIs.closeEntry();                fout.close();            }            zipIs.close();}


You can store the uncompressed files directly in assets (i.e. unpack the zip into assets/ folder). This way, you can access the files directly and they will be compressed anyway when you build the APK.


You can create a ZipInputStream in the following way :

ZipInputStream zipIs = new ZipInputStream(context.getResources().openRawResource(your.package.com.R.raw.filename)); ZipEntry ze = null;        while ((ze = zipIs.getNextEntry()) != null) {            FileOutputStream fout = new FileOutputStream(FOLDER_NAME +"/"+ ze.getName());            byte[] buffer = new byte[1024];            int length = 0;            while ((length = zipIs.read(buffer))>0) {            fout.write(buffer, 0, length);            }            zipIs .closeEntry();            fout.close();        }        zipIs .close();