Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading files from a ZIP file in your Android assets folder

I'm reading files from a ZIP file that's located in my Android assets folder using ZipInputStream: it works, but it's really slow, as it has to read it sequentially using getNextEntry(), and there are quite a lot of files.

If I copy the ZIP file onto the SD card, reading is really fast when using ZipFile.getEntry, but I didn't find a way to use ZipFile with the asset file!

Is there any way to access the ZIP in the asset folder in a speedy way? Or do I really have to copy the ZIP to the SD card?

(BTW, in case anybody wonders why I'm doing this: the app is larger than 50 MB, so in order to get it in the Play Store I have to use Expansion APKs; however, as this app should also be put into the Amazon App Store, I have to use another version for this, as Amazon doesn't support Expansion APKs, naturally... I thought that accessing a ZIP file at two different locations would be an easy way to handle this, but alas...)

like image 313
hdort Avatar asked Jul 23 '12 14:07

hdort


People also ask

How do I access assets files on Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 – Right click app >> New >> Folder >> Assets folder.

Can you view ZIP files on Android?

Files by Google allows you to extract and view contents of compressed files. Note: Only . zip files are supported.

How do I extract data from a zip file?

To unzip filesOpen File Explorer and find the zipped folder. To unzip the entire folder, right-click to select Extract All, and then follow the instructions. To unzip a single file or folder, double-click the zipped folder to open it. Then, drag or copy the item from the zipped folder to a new location.

Where is the Android Assets folder?

In Android Studio, click on the app folder, then the src folder, and then the main folder. Inside the main folder you can add the assets folder.


2 Answers

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();
like image 151
Slickelito Avatar answered Oct 13 '22 00:10

Slickelito


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.

like image 37
Mark Avatar answered Oct 13 '22 01:10

Mark