最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年12月28日 08:39:46   作者:小王_同志  
本篇文章主要介紹了Android zip4j壓縮、解壓、加解密的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

jdk有原生的zip包,因為用起來沒有達到想要的效果,所以此次用的是第三方zip4j開源

zip4j.jar官網(wǎng)下載鏈接

直接代碼:

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"); //設置編碼格式(支持中文)

      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"); //設置編碼格式(支持中文)
      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;
  }

}

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

相關文章

最新評論

郴州市| 隆德县| 临邑县| 彰化市| 汉沽区| 仁怀市| 万宁市| 阳江市| 临漳县| 松阳县| 龙南县| 阜阳市| 水城县| 丹棱县| 紫阳县| 珲春市| 石屏县| 沂南县| 海丰县| 大荔县| 卓尼县| 大城县| 莲花县| 虹口区| 黄冈市| 修武县| 禄丰县| 栾川县| 绿春县| 赣榆县| 凤冈县| 进贤县| 循化| 高邑县| 舟曲县| 澄江县| 莲花县| 开远市| 雷波县| 吉木乃县| 湘潭市|