Android zip4j压缩、解压、加解密的示例代码
jdk有原生的zip包,因为用起来没有达到想要的效果,所以此次用的是第三方zip4j开源
zip4j.jar官网下载链接
直接代码:
packagecom.dfxh.wang.compress_operate;
importandroid.util.Log;
importnet.lingala.zip4j.core.ZipFile;
importnet.lingala.zip4j.exception.ZipException;
importnet.lingala.zip4j.model.ZipParameters;
importnet.lingala.zip4j.util.Zip4jConstants;
importjava.io.File;
/**
*CreatedbyWangChaoweion2017/12/27.
*
*此类是用第三方开源的zip4j操作文件(目录)的压缩、解压、加解密
*/
publicclassCompressOperate_zip4j{
privateZipFilezipFile;
privateZipParameterszipParameters;
privateintresult=0;//状态返回值
privatestaticfinalStringTAG="CompressOperate_zip4j";
/**
*zip4j压缩
*@paramfilePath要压缩的文件路径(可文件,可目录)
*@paramzipFilePathzip生成的文件路径
*@parampassword密码
*@return状态返回值
*/
publicintcompressZip4j(StringfilePath,StringzipFilePath,Stringpassword){
FilesourceFile=newFile(filePath);
FilezipFile_=newFile(zipFilePath);
try{
zipFile=newZipFile(zipFile_);
zipFile.setFileNameCharset("GBK");//设置编码格式(支持中文)
zipParameters=newZipParameters();
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()){
StringsourceFileName=checkString(sourceFile.getName());//文件校验
zipFilePath=zipFilePath+"/"+sourceFileName+".zip";
Log.i(TAG,"保存压缩文件的路径(zipFilePath):"+zipFilePath);
compressZip4j(filePath,zipFilePath,password);
}
if(sourceFile.isDirectory()){
//File[]files=sourceFile.listFiles();
//ArrayListarrayList=newArrayList();
//Collections.addAll(arrayList,files);
zipFile.addFolder(sourceFile,zipParameters);
}else{
zipFile.addFile(sourceFile,zipParameters);
}
Log.i(TAG,"compressZip4j:压缩成功");
}catch(ZipExceptione){
Log.e(TAG,"compressZip4j:异常:"+e);
result=-1;
returnresult;
}
returnresult;
}
/**
*校验提取出的原文件名字是否带格式
*@paramsourceFileName要压缩的文件名
*@return
*/
privateStringcheckString(StringsourceFileName){
if(sourceFileName.indexOf(".")>0){
sourceFileName=sourceFileName.substring(0,sourceFileName.length()-4);
Log.i(TAG,"checkString:校验过的sourceFileName是:"+sourceFileName);
}
returnsourceFileName;
}
/**
*zip4j解压
*@paramzipFilePath待解压的zip文件(目录)路径
*@paramfilePath解压到的保存路径
*@parampassword密码
*@return状态返回值
*/
publicintuncompressZip4j(StringzipFilePath,StringfilePath,Stringpassword){
FilezipFile_=newFile(zipFilePath);
FilesourceFile=newFile(filePath);
try{
zipFile=newZipFile(zipFile_);
zipFile.setFileNameCharset("GBK");//设置编码格式(支持中文)
if(!zipFile.isValidZipFile()){//检查输入的zip文件是否是有效的zip文件
thrownewZipException("压缩文件不合法,可能被损坏.");
}
if(sourceFile.isDirectory()&&!sourceFile.exists()){
sourceFile.mkdir();
}
if(zipFile.isEncrypted()){
zipFile.setPassword(password.toCharArray());
}
zipFile.extractAll(filePath);//解压
Log.i(TAG,"uncompressZip4j:解压成功");
}catch(ZipExceptione){
Log.e(TAG,"uncompressZip4j:异常:"+e);
result=-1;
returnresult;
}
returnresult;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。