栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android zip4j压缩、解压、加解密的示例代码

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android zip4j压缩、解压、加解密的示例代码

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

zip4j.jar官网下载链接

直接代码:

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;



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

  private static final String TAG = "CompressOperate_zip4j";

  
  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 arrayList = new ArrayList();
 // 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;
  }

  
  private String checkString(String sourceFileName){
    if (sourceFileName.indexOf(".") > 0){
      sourceFileName = sourceFileName.substring(0,sourceFileName.length() - 4);
      Log.i(TAG, "checkString: 校验过的sourceFileName是:" + sourceFileName);
    }
    return sourceFileName;
  }

  
  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;
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/156782.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号