SoFunction
Updated on 2025-03-08

Android Zip decompression tool sharing

This article shares the specific code of Android Zip decompression tool for your reference. The specific content is as follows

package ;

import ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

/**
  * @author: lijuan
  * @description: Unzip ZIP file
  * @date: 2017-04-11
  * @time: 09:22
  */
public class ZipUtils {
 public static final String TAG="ZIP";
 public ZipUtils(){

 }

 /**
   * Unzip zip to the specified path
   * @param zipFileString The name of ZIP
   * @param outPathString To decompress the path
   * @throws Exception
   */
 public static void UnZipFolder(String zipFileString, String outPathString) throws Exception {
  ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString));
  ZipEntry zipEntry;
  String szName = "";
  while ((zipEntry = ()) != null) {
   szName = ();
   if (()) {
    //Get the folder name of the component    szName = (0, () - 1);
    File folder = new File(outPathString +  + szName);
    ();
   } else {
    (TAG,outPathString +  + szName);
    File file = new File(outPathString +  + szName);
    if (!()){
     (TAG, "Create the file:" + outPathString +  + szName);
     ().mkdirs();
     ();
    }
    // Get the output stream of the file    FileOutputStream out = new FileOutputStream(file);
    int len;
    byte[] buffer = new byte[1024];
    // Read (byte) bytes to the buffer    while ((len = (buffer)) != -1) {
     // Write (byte) bytes from buffer (0) position     (buffer, 0, len);
     ();
    }
    ();
   }
  }
  ();
 }

 public static void UnZipFolder(String zipFileString, String outPathString,String szName) throws Exception {
  ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString));
  ZipEntry zipEntry;
   while ((zipEntry = ()) != null) {
   //szName = ();
   if (()) {
    //Get the folder name of the component    szName = (0, () - 1);
    File folder = new File(outPathString +  + szName);
    ();
   } else {
    (TAG,outPathString +  + szName);
    File file = new File(outPathString +  + szName);
    if (!()){
     (TAG, "Create the file:" + outPathString +  + szName);
     ().mkdirs();
     ();
    }
    // Get the output stream of the file    FileOutputStream out = new FileOutputStream(file);
    int len;
    byte[] buffer = new byte[1024];
    // Read (byte) bytes to the buffer    while ((len = (buffer)) != -1) {
     // Write (byte) bytes from buffer (0) position     (buffer, 0, len);
     ();
    }
    ();
   }
  }
  ();
 }

 /**
   * Compressed files and folders
   * @param srcFileString file or folder to be compressed
   * @param zipFileString The decompressed Zip path
   * @throws Exception
   */
 public static void ZipFolder(String srcFileString, String zipFileString)throws Exception {
  //Create a ZIP  ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFileString));
  //Create a file  File file = new File(srcFileString);
  //compression  ZipFiles(()+, (), outZip);
  //Complete and close  ();
  ();
 }

 /**
   * Compressed file
   * @param folderString
   * @param fileString
   * @param zipOutputSteam
   * @throws Exception
   */
 private static void ZipFiles(String folderString, String fileString, ZipOutputStream zipOutputSteam)throws Exception{
  if(zipOutputSteam == null)
   return;
  File file = new File(folderString+fileString);
  if (()) {
   ZipEntry zipEntry = new ZipEntry(fileString);
   FileInputStream inputStream = new FileInputStream(file);
   (zipEntry);
   int len;
   byte[] buffer = new byte[4096];
   while((len=(buffer)) != -1)
   {
    (buffer, 0, len);
   }
   ();
  }
  else {
   //Folder   String fileList[] = ();
   //No subfiles and compression   if ( <= 0) {
    ZipEntry zipEntry = new ZipEntry(fileString+);
    (zipEntry);
    ();
   }
   //Subfiles and recursion   for (int i = 0; i < ; i++) {
    ZipFiles(folderString, fileString+ +fileList[i], zipOutputSteam);
   }
  }
 }

 /**
   * Returns the file input stream of zip
   * @param zipFileString The name of zip
   * @param fileString ZIP filename
   * @return InputStream
   * @throws Exception
   */
 public static InputStream UpZip(String zipFileString, String fileString)throws Exception {
  ZipFile zipFile = new ZipFile(zipFileString);
  ZipEntry zipEntry = (fileString);
  return (zipEntry);
 }

 /**
   * Returns the list of files (files and folders) in ZIP
   * @param zipFileString The name of ZIP
   * @param bContainFolder Does it contain folders
   * @param bContainFile does it contain a file
   * @return
   * @throws Exception
   */
 public static List<File> GetFileList(String zipFileString, boolean bContainFolder, boolean bContainFile)throws Exception {
  List<File> fileList = new ArrayList<File>();
  ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString));
  ZipEntry zipEntry;
  String szName = "";
  while ((zipEntry = ()) != null) {
   szName = ();
   if (()) {
    // Get the folder name of the component    szName = (0, () - 1);
    File folder = new File(szName);
    if (bContainFolder) {
     (folder);
    }
   } else {
    File file = new File(szName);
    if (bContainFile) {
     (file);
    }
   }
  }
  ();
  return fileList;
 }
}

Add permissions

<!-- Allows applications to write data to external storage devices(MainlySDCard) -->
  <uses-permission android:name=".WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name=".READ_EXTERNAL_STORAGE" />
  <!-- existsdcardCreated in/Permission to delete files -->
  <uses-permission android:name=".MOUNT_UNMOUNT_FILESYSTEMS" />

Test it in specific scenarios

(AppConfig.DOWNLOAD_PATH + (), AppConfig.UNZIP_PATH);

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.