How to read zip package directly without decompression by Android
This article mainly explains the "Android how to read the zip package directly without decompression", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Android how to read the zip package without decompression directly"!
Text:
Zip package directory structure: res/txt/data.json
File SD card path: android.os.Environment.getExternalStorageDirectory () + "/ res.zip"
File name of public static String readDataFile (String file) throws Exception {/ / intercept path res String fileName = file.substring (file.length ()-9, 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 "";}
The above method is relatively simple and there is nothing to say, as long as you understand it. One thing you need to pay attention to is that when judging whether you want to read the file, the path here is to compare the compressed directory of zip as the root directory. That is, the current value of fileName in the sentence if (ze.getName (). Equals (fileName + "/ txt/data.json")) is res. Finally, the content read is returned and the String is done.
The reading of the picture and the xml file are similar, and the code is pasted directly below.
Picture:
Zip package directory structure: res/pic/.png
File SD card path: android.os.Environment.getExternalStorageDirectory () + "/ res.zip"
Public static Bitmap readGuidePic (String file, String ResId) throws Exception {String fileName = file.substring (file.length ()-9, 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/.png")) {InputStream is = zf.getInputStream (ze); Bitmap bitmap = BitmapFactory.decodeStream (is) Return bitmap;}} zin.closeEntry (); return null;}
Xml file:
Zip package directory structure: res/xml/app.xml
File SD card path: android.os.Environment.getExternalStorageDirectory () + "/ res.zip"
Public static InputStream readAppFile (String file) throws IOException {String fileName = file.substring (file.length ()-9, 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 } Thank you for your reading. The above is the content of "how to read the zip package directly without decompressing Android". After the study of this article, I believe you have a deeper understanding of how Android can read the zip package without decompression, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!