国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android zip4j壓縮、解壓、加解密的示例代碼

2019-12-12 01:22:34
字體:
來源:轉載
供稿:網友

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 柯坪县| 南安市| 德钦县| 桐庐县| 东丽区| 南开区| 页游| 毕节市| 恩平市| 吉林省| 揭东县| 陆川县| 韶山市| 泌阳县| 延长县| 石景山区| 社旗县| 汪清县| 社会| 随州市| 任丘市| 徐闻县| 六盘水市| 景泰县| 兰溪市| 罗平县| 拜城县| 平南县| 米泉市| 华亭县| 泸水县| 莱芜市| 莆田市| 忻州市| 鄂温| 南江县| 亳州市| 峨眉山市| 诏安县| 丹东市| 南陵县|