Method for Android to read zip packets directly without decompressing

  • 2021-08-12 03:29:30
  • OfStack

Before the project encountered a demand, the director asked us to download the resources from the server without decompressing and directly read the resources inside, so as to save a step to check whether the resources are correct, which seems to make sense. . . Don't talk nonsense and go straight to the code.

At present, the readable resources I have tried are text, pictures and xml files.

Text:

zip package directory structure: res/txt/data. json

File sd card path: android. os. Environment. getExternalStorageDirectory () + "/res. zip"


public static String readDataFile(String file) throws Exception {
  // Filename of 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 nothing to say, just understand it. What needs attention is that when judging whether it is a file you want to read, 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, return the read content String and you are finished.

The reading of pictures and xml files is similar, and the code is posted directly below.

Picture:

zip package directory structure: res/pic/haha. png

File sd card path: android. os. Environment () + "/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/haha.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;
 }

Related articles: