Android中如何实现zip4j压缩、解压、加解密功能

发布时间:2021-07-21 09:19:02 作者:小新
来源:亿速云 阅读:989

这篇文章主要介绍了Android中如何实现zip4j压缩、解压、加解密功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

jdk有原生的zip包,因为用起来没有达到想要的效果,所以此次用的是第三方zip4j开源

直接代码:

package com.dfxh.wang.compress_operate;

import android.util.Log;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

import java.io.File;

/**
 * Created by WangChaowei on 2017/12/27.
 *
 * 此类是用第三方开源的zip4j操作文件(目录)的压缩、解压、加解密
 */

public class CompressOperate_zip4j {
  private ZipFile zipFile;
  private ZipParameters zipParameters;
  private int result = 0; //状态返回值

  private static final String TAG = "CompressOperate_zip4j";

  /**
   * zip4j压缩
   * @param filePath 要压缩的文件路径(可文件,可目录)
   * @param zipFilePath zip生成的文件路径
   * @param password 密码
   * @return 状态返回值
   */
  public int compressZip4j(String filePath, String zipFilePath, String password) {
    File sourceFile = new File(filePath);
    File zipFile_ = new File(zipFilePath);
    try {
      zipFile = new ZipFile(zipFile_);
      zipFile.setFileNameCharset("GBK"); //设置编码格式(支持中文)

      zipParameters = new ZipParameters();
      zipParameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); //压缩方式
      zipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 压缩级别
      if (password != null && password != "") {  //是否要加密(加密会影响压缩速度)
        zipParameters.setEncryptFiles(true);
        zipParameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密方式
        zipParameters.setPassword(password.toCharArray());
      }

      if (zipFile_.isDirectory()) {
        String sourceFileName = checkString(sourceFile.getName()); //文件校验
        zipFilePath = zipFilePath + "/" + sourceFileName + ".zip";
        Log.i(TAG, "保存压缩文件的路径(zipFilePath):" + zipFilePath);
        compressZip4j(filePath,zipFilePath,password);
      }
      if (sourceFile.isDirectory()) {
        // File[] files = sourceFile.listFiles();
        // ArrayList<File> arrayList = new ArrayList<File>();
        // Collections.addAll(arrayList, files);
        zipFile.addFolder(sourceFile, zipParameters);
      } else {
        zipFile.addFile(sourceFile, zipParameters);
      }
      Log.i(TAG, "compressZip4j: 压缩成功");
    } catch (ZipException e) {
      Log.e(TAG, "compressZip4j: 异常:" + e);
      result = -1;
      return result;
    }
    return result;
  }

  /**
   * 校验提取出的原文件名字是否带格式
   * @param sourceFileName 要压缩的文件名
   * @return
   */
  private String checkString(String sourceFileName){
    if (sourceFileName.indexOf(".") > 0){
      sourceFileName = sourceFileName.substring(0,sourceFileName.length() - 4);
      Log.i(TAG, "checkString: 校验过的sourceFileName是:" + sourceFileName);
    }
    return sourceFileName;
  }

  /**
   * zip4j解压
   * @param zipFilePath 待解压的zip文件(目录)路径
   * @param filePath 解压到的保存路径
   * @param password 密码
   * @return 状态返回值
   */
  public int uncompressZip4j(String zipFilePath, String filePath, String password) {
    File zipFile_ = new File(zipFilePath);
    File sourceFile = new File(filePath);

    try {
      zipFile = new ZipFile(zipFile_);
      zipFile.setFileNameCharset("GBK"); //设置编码格式(支持中文)
      if (!zipFile.isValidZipFile()){   //检查输入的zip文件是否是有效的zip文件
        throw new ZipException("压缩文件不合法,可能被损坏.");
      }
      if (sourceFile.isDirectory() && !sourceFile.exists()) {
        sourceFile.mkdir();
      }
      if (zipFile.isEncrypted()) {
        zipFile.setPassword(password.toCharArray());
      }
      zipFile.extractAll(filePath); //解压
      Log.i(TAG, "uncompressZip4j: 解压成功");

    } catch (ZipException e) {
      Log.e(TAG, "uncompressZip4j: 异常:"+ e);
      result = -1;
      return result;
    }
    return result;
  }

}

感谢你能够认真阅读完这篇文章,希望小编分享的“Android中如何实现zip4j压缩、解压、加解密功能”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. Centos压缩&解压
  2. node.js实现简单的压缩/解压缩功能示例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android zip4j

上一篇:C/C++文件的操作函数介绍

下一篇:node.js中怎么实现一个Fabric链码

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》