Android zip4j壓縮、解壓、加解密的示例代碼
更新時間:2017年12月28日 08:39:46 作者:小王_同志
本篇文章主要介紹了Android zip4j壓縮、解壓、加解密的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
jdk有原生的zip包,因為用起來沒有達(dá)到想要的效果,所以此次用的是第三方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; //狀態(tài)返回值 private static final String TAG = "CompressOperate_zip4j"; /** * zip4j壓縮 * @param filePath 要壓縮的文件路徑(可文件,可目錄) * @param zipFilePath zip生成的文件路徑 * @param password 密碼 * @return 狀態(tài)返回值 */ 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"); //設(shè)置編碼格式(支持中文) 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 狀態(tài)返回值 */ 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"); //設(shè)置編碼格式(支持中文) 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; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 滑動返回Activity的實現(xiàn)代碼
這篇文章主要介紹了Android 滑動返回Activity的實現(xiàn)代碼的相關(guān)資料,這里是訪微信滑動返回主頁的功能,需要的朋友可以參考下2017-07-07Android Handler,Message,MessageQueue,Loper源碼解析詳解
這篇文章主要介紹了Android Handler,Message,MessageQueue,Loper源碼解析詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09使用IntelliJ IDEA 配置安卓(Android)開發(fā)環(huán)境的教程詳解(新手必看)
這篇文章主要介紹了使用IntelliJ IDEA 配置安卓(Android)開發(fā)環(huán)境的教程詳解(新手必看),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09