Android: read the gzip file in the ASSETS folder

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

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

GZIPInputStream fIn = new GZIPInputStream(mContext.getResources().openRawResource(R.raw.myfilegz)); int size = fIn.available(); 

for some reason, the size is always 1. But if Idon't gzip a file, it works fine.

Note: Using Android 1.5

+4
source share
6 answers
 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 unpack all the files in this zip file to your local folder. I use this for NDK.

you can access files from ndk through: / Data / data // files /

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

+3
source

I met the same problem when reading a gz file from a data folder.

This is caused by the gz file name. Just rename yourfile.gz to another name, for example yourfile.bin. It looks like the Android build system automatically decompresses the file if it considers it gz.

+3
source

this is the documented behavior of InflaterInputStream.available:

http://java.sun.com/javase/6/docs/api/java/util/zip/InflaterInputStream.html#available ()

 Returns 0 after EOF has been reached, otherwise always return 1. 

abuse is a common mistake - in no case can you assume that it tells you the length of the file (although sometimes this happens, as you noticed). you want to continue calling read (byte [], int, int) until it returns 0. If you want the length to highlight the byte [] in front, you probably want to create a ByteArrayOutputStream and write this every time you will read and then get byte [] from this when exiting the loop. this works for all InputStreams in all cases.

+2
source

What happens if you use AssetManager instead of Resources ? Example:

 InputStream is = mContext.getAssets().open("myfilegz"); GZIPInputStream fIn = new GZIPINputStream(is); 

Internally, Resources simply calls the AssetManager ; Interestingly, somewhere along the way it all gets in the way.

0
source

Try finding a source for translations from open source apps-for-android and see if that helps at all.

They use the GZIPInputStream for the raw file in their selectRandomWord () function [line 326] (source inserted below)

 public void selectRandomWord() { BufferedReader fr = null; try { GZIPInputStream is = new GZIPInputStream(getResources().openRawResource(R.raw.dictionary)); 
0
source

The build system seems to treat .gz files as a special case, even if it is included as a source resource. Rename the .gz file to have a different extension, such as .raw or .bin.

Valid at least for Android Studio 2.2. I cannot find any documents to confirm this expected behavior or, better, how to prevent it, but changing the extension, at least, helps to solve the problem.

0
source

Source: https://habr.com/ru/post/1302879/


All Articles