Android: read a GZIP file in the ASSETS folder

后端 未结 6 1361
不思量自难忘°
不思量自难忘° 2021-01-06 08:07

How can you read GZIP file in Android located in the \"ASSETS\" (or resources/raw) folder?

I have tried the following code, but my stream size is always 1.



        
6条回答
  •  臣服心动
    2021-01-06 08:34

    public class ResLoader {
    
        /**
         * @param res
         * @throws IOException
         * @throws FileNotFoundException
         * @throws IOException
         */
    
        static void unpackResources() throws FileNotFoundException, IOException {
            final int BUFFER = 8192;
    
            android.content.res.Resources t = TestingE3d.mContext.getResources();
    
            InputStream fis = t.openRawResource(R.raw.resources);
            if (fis == null)
                return;
    
            ZipInputStream zin = new ZipInputStream(new BufferedInputStream(fis,
                    BUFFER));
            ZipEntry entry;
            while ((entry = zin.getNextEntry()) != null) {
                int count;
    
                FileOutputStream fos = TestingE3d.mContext.openFileOutput(entry
                        .getName(), 0);
                BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
    
                byte data[] = new byte[BUFFER];
    
                while ((count = zin.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, count);
                    // Log.v("NOTAG", "writing "+count + " to "+entry.getName());
                }
                dest.flush();
                dest.close();
            }
            zin.close();
    
        }
    
    }
    

    R.raw.resources is a zip file - this class will decompress all files in that zip to your local folder. I use this for NDK.

    you can access your fils from ndk through: /data/data//files/

    package = package where ResLoader resides filename = one of files that is in raw/resources.zip

提交回复
热议问题