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

本篇文章主要介绍了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; /** * 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 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; } /** * 校验提取出的原文件名字是否带格式 * @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; } } 

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

以上就是Android zip4j压缩、解压、加解密的示例代码的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动