Android/Java compress / decompress / encrypt file / zip

background

In the process of redevelopment, no matter Android client or desktop program, it may involve file upload and download. In case of multiple files, zip compression is usually used to compress multiple files into a zip package, which is convenient for transmission. In addition, download of some resources may also be involved in the client, which are usually stored in the service in the form of zip On the server, the client can download the zip file when appropriate, and then extract the resources. Let's take a look at using the Zip4j framework to compress and decompress files.

 

Zip4j

Official website: http://www.lingala.net/zip4j/index.php

You can download the latest jar (as if it is empty),

Here is my jar with the latest source code. You can also download this jar file, https://download.csdn.net/download/lindroid/10768400

 

Use

The following is a encapsulated compressed tool class. You can set the password for the zip package. Of course, you can also pass null without setting the password;

public class ZipUtil {


    /**
     * Compress and encrypt file list
     * @param srcfile
     * @param destZipFile
     * @param password
     * @return
     */
    public static File doZipFilesWithPassword(File[] srcfile, String destZipFile, String password) {
        if (srcfile == null || srcfile.length == 0) {
            return null;
        }
        ZipParameters parameters = new ZipParameters();
        // Compression mode
        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
        // Compression level
        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
        // Encryption mode
        if (!TextUtils.isEmpty(password)) {
            parameters.setEncryptFiles(true);
            parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
            parameters.setPassword(password);
        }
        ArrayList<File> existFileList = new ArrayList<File>();
        for (int i = 0; i < srcfile.length; i++) {
            if (srcfile[i] != null) {
                existFileList.add(srcfile[i]);
            }
        }
        try {
            ZipFile zipFile = new ZipFile(destZipFile);
            zipFile.addFiles(existFileList, parameters);
            return zipFile.getFile();
        } catch (ZipException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * Encrypt folder
     * @param folder
     * @param destZipFile
     * @param password
     * @return
     */
    public static File doZipFilesWithPassword(File folder, String destZipFile, String password) {
        if (!folder.exists()) {
            return null;
        }
        ZipParameters parameters = new ZipParameters();
        // Compression mode
        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
        // Compression level
        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
        // Encryption mode
        if (!TextUtils.isEmpty(password)) {
            parameters.setEncryptFiles(true);//
            parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
            parameters.setPassword(password);
        }
        try {
            ZipFile zipFile = new ZipFile(destZipFile);
            zipFile.addFolder(folder, parameters);
            return zipFile.getFile();
        } catch (ZipException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * Single file compression and encryption
     * @param file zip file to compress
     * @param destZipFile zip Save path
     * @param password Password can be null
     * @return
     */
    public static File doZipSingleFileWithPassword(File file, String destZipFile, String password) {
        if (!file.exists()) {
            return null;
        }
        ZipParameters parameters = new ZipParameters();
        // Compression mode
        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
        // Compression level
        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
        // Encryption mode
        if (!TextUtils.isEmpty(password)) {
            parameters.setEncryptFiles(true);//
            parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
            parameters.setPassword(password);
        }
        try {
            ZipFile zipFile = new ZipFile(destZipFile);
            zipFile.addFile(file,parameters);
            return zipFile.getFile();
        } catch (ZipException e) {
            e.printStackTrace();
            return null;
        }
    }


/**
*   Decompression file
    File: Target zip file
    password: Password, if no null can be passed
    path: Directory path to extract to
*/
    public static boolean unZip(File file,String password,String path) {
    	boolean res = false;
		try {
			ZipFile zipFile = new ZipFile(file);
			if (zipFile.isEncrypted()) { 
	    		if(password != null && !password.isEmpty()) {
	    			zipFile.setPassword(password);  
	    		}
	    	}  
	    	zipFile.extractAll(path);
	    	res = true;
		} catch (ZipException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		return res;
    }

}

 

Several methods in the tool class are basically simple encapsulation of the api provided by zip4j, mainly the ZipFile class and ZipParameters class. You can refer to continue to encapsulate some simpler methods.

 

Tags: Mobile Android PHP

Posted on Mon, 09 Dec 2019 13:10:01 -0800 by misty