SoFunction
Updated on 2025-03-09

Sample code for compression, decompression, encryption and decryption of Android zip4j

jdk has a native zip package. Because it does not achieve the desired effect, this time I use a third-party zip4j open source

Official website download link

Direct code:

package .compress_operate;

import ;

import .;
import .;
import .;
import ..Zip4jConstants;

import ;

/**
  * Created by WangChaowei on 2017/12/27.
  *
  * This type uses third-party open source zip4j operation files (directories) to compress, decompress, and encrypt and decrypt.
  */

public class CompressOperate_zip4j {
  private ZipFile zipFile;
  private ZipParameters zipParameters;
  private int result = 0; //Status return value
  private static final String TAG = "CompressOperate_zip4j";

  /**
    * zip4j compression
    * @param filePath File path to compress (file, directory)
    * @param zipFilePath file path generated by zip
    * @param password
    * @return status return value
    */
  public int compressZip4j(String filePath, String zipFilePath, String password) {
    File sourceFile = new File(filePath);
    File zipFile_ = new File(zipFilePath);
    try {
      zipFile = new ZipFile(zipFile_);
      ("GBK"); //Set the encoding format (supports Chinese)
      zipParameters = new ZipParameters();
      (Zip4jConstants.COMP_DEFLATE); //Compression method      (Zip4jConstants.DEFLATE_LEVEL_NORMAL); // Compression level      if (password != null && password != "") {  //Whether to encrypt (encryption will affect compression speed)        (true);
        (Zip4jConstants.ENC_METHOD_STANDARD); // Encryption method        (());
      }

      if (zipFile_.isDirectory()) {
        String sourceFileName = checkString(()); //File verification        zipFilePath = zipFilePath + "/" + sourceFileName + ".zip";
        (TAG, "The path to save the compressed file(zipFilePath):" + zipFilePath);
        compressZip4j(filePath,zipFilePath,password);
      }
      if (()) {
        // File[] files = ();
        // ArrayList<File> arrayList = new ArrayList<File>();
        // (arrayList, files);
        (sourceFile, zipParameters);
      } else {
        (sourceFile, zipParameters);
      }
      (TAG, "compressZip4j: Compressed successfully");
    } catch (ZipException e) {
      (TAG, "compressZip4j: Exception:" + e);
      result = -1;
      return result;
    }
    return result;
  }

  /**
    * Verify whether the extracted original file name is in format
    * @param sourceFileName file name to compress
    * @return
    */
  private String checkString(String sourceFileName){
    if ((".") > 0){
      sourceFileName = (0,() - 4);
      (TAG, "checkString: The checked sourceFileName is:" + sourceFileName);
    }
    return sourceFileName;
  }

  /**
    * zip4j decompression
    * @param zipFilePath The path of the zip file (directory) to be decompressed
    * @param filePath Decompressed save path
    * @param password
    * @return status return value
    */
  public int uncompressZip4j(String zipFilePath, String filePath, String password) {
    File zipFile_ = new File(zipFilePath);
    File sourceFile = new File(filePath);

    try {
      zipFile = new ZipFile(zipFile_);
      ("GBK"); //Set the encoding format (supports Chinese)      if (!()){   //Check whether the entered zip file is a valid zip file        throw new ZipException("The compressed file is illegal and may be corrupted.");
      }
      if (() && !()) {
        ();
      }
      if (()) {
        (());
      }
      (filePath); //Decompression      (TAG, "uncompressZip4j: Decompress successfully");

    } catch (ZipException e) {
      (TAG, "uncompressZip4j: Exception:"+ e);
      result = -1;
      return result;
    }
    return result;
  }

}

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.