SoFunction
Updated on 2025-03-10

Android method to read zip packages directly without decompression

I encountered a requirement in the project before. The director asked us to read the resources downloaded from the server without decompressing them. This way, the steps to verify whether the resources are correct are saved. It seems to make sense. . . Just upload the code without saying much nonsense.

The resources I have tried to read include text, pictures, and xml files.

text:

zip package directory structure: res/txt/

File sd card path: () + “/”

public static String readDataFile(String file) throws Exception {
  //The file name of the intercept path is res  String fileName = (() - 9, () - 4);
  ZipFile zf = new ZipFile(file);
  InputStream in = new BufferedInputStream(new FileInputStream(file));
  ZipInputStream zin = new ZipInputStream(in);
  ZipEntry ze;
  while ((ze = ()) != null) {
   if (()) {
    //Do nothing
   } else {
    if (().equals(fileName + "/txt/")) {
     BufferedReader br = new BufferedReader(
       new InputStreamReader((ze)));
     String line;
     while ((line = ()) != null) {
      return line;
     }
     ();
    }
   }
  }
  ();
  return "";
 }

The above method is relatively simple and has nothing to say, just understand it. One thing you need to pay attention to 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, if (().equals(fileName + "/txt/")) The current value of fileName in this sentence is res. Finally, return the read content String and it's done.

The reading of the image and xml file is similar, so the code is posted below.

picture:

zip package directory structure: res/pic/

File sd card path: () + “/”

public static Bitmap readGuidePic(String file, String ResId) throws Exception {
  String fileName = (() - 9, () - 4);
  ZipFile zf = new ZipFile(file);
  InputStream in = new BufferedInputStream(new FileInputStream(file));
  ZipInputStream zin = new ZipInputStream(in);
  ZipEntry ze;
  while ((ze = ()) != null) {
   if (()) {
    //Do nothing
   } else {
    ("tag", "file - " + () + " : " + () + " bytes");
    if (().equals(fileName + "/pic/")) {
     InputStream is = (ze);
     Bitmap bitmap = (is);
     return bitmap;
    }
   }
  }
  ();
  return null;
 }

xml file:

zip package directory structure: res/xml/

File sd card path: () + “/”

public static InputStream readAppFile(String file) throws IOException {
  String fileName = (() - 9, () - 4);
  ZipFile zf = new ZipFile(file);
  InputStream in = new BufferedInputStream(new FileInputStream(file));
  ZipInputStream zin = new ZipInputStream(in);
  ZipEntry ze;
  while ((ze = ()) != null) {
   if (()) {
    //Do nothing
   } else {
    if (().equals(fileName + "/xml/")) {
     InputStream inputStream = (ze);
     return inputStream;
    }
   }
  }
  ();
  return null;
 }

The above method of reading zip packages directly without decompression is all the content I share with you. I hope you can give you a reference and I hope you can support me more.