Zip4j開源jar包的簡單使用

package com.fenghao.zip;

import java.io.File;

import java.util.ArrayList;

import java.util.Collections;

import net.lingala.zip4j.core.ZipFile;

import net.lingala.zip4j.exception.ZipException;

import net.lingala.zip4j.model.ZipParameters;

import net.lingala.zip4j.util.Zip4jConstants;

/**

* 對文件進(jìn)行壓縮和加密

* 對文件進(jìn)行解壓和解密

* @author fenghao

*

*/

public class CompressUtils {

? ? /**

? ? * 解壓加密的壓縮文件

? ? * @param zipfile

? ? * @param dest

? ? * @param passwd

? ? * @throws ZipException

? ? */

? ? public void unZip(File zipfile,String dest,String passwd) throws ZipException{

? ? ? ? ZipFile zfile=new ZipFile(zipfile);

//? ? ? ? zfile.setFileNameCharset("GBK");//在GBK系統(tǒng)中需要設(shè)置

? ? ? ? if(!zfile.isValidZipFile()){

? ? ? ? ? ? throw new ZipException("壓縮文件不合法,可能已經(jīng)損壞!");

? ? ? ? }

? ? ? ? File file=new File(dest);

? ? ? ? if(file.isDirectory() && !file.exists()){

? ? ? ? ? ? file.mkdirs();

? ? ? ? }

? ? ? ? if(zfile.isEncrypted()){

? ? ? ? ? ? zfile.setPassword(passwd.toCharArray());

? ? ? ? }

? ? ? ? zfile.extractAll(dest);

? ? }

? ? /**

? ? * 壓縮文件且加密

? ? * @param src

? ? * @param dest

? ? * @param is

? ? * @param passwd

? ? */

? ? public void zip(String src,String dest,boolean is,String passwd){

? ? ? ? File srcfile=new File(src);

? ? ? ? //創(chuàng)建目標(biāo)文件

? ? ? ? String destname = buildDestFileName(srcfile, dest);

? ? ? ? ZipParameters par=new ZipParameters();

? ? ? ? par.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);

? ? ? ? par.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

? ? ? ? if(passwd!=null){

? ? ? ? ? ? par.setEncryptFiles(true);

? ? ? ? ? ? par.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);

? ? ? ? ? ? par.setPassword(passwd.toCharArray());

? ? ? ? }

? ? ? ? try {

? ? ? ? ? ? ZipFile zipfile=new ZipFile(destname);

? ? ? ? ? ? if(srcfile.isDirectory()){

? ? ? ? ? ? ? ? if(!is){

? ? ? ? ? ? ? ? ? ? File[] listFiles = srcfile.listFiles();

? ? ? ? ? ? ? ? ? ? ArrayList<File> temp=new ArrayList<File>();

? ? ? ? ? ? ? ? ? ? Collections.addAll(temp, listFiles);

? ? ? ? ? ? ? ? ? ? zipfile.addFiles(temp, par);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? zipfile.addFolder(srcfile, par);

? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? zipfile.addFile(srcfile, par);

? ? ? ? ? ? }

? ? ? ? } catch (ZipException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }



? ? }

? ? /**

? ? * 目標(biāo)文件名稱

? ? * @param srcfile

? ? * @param dest

? ? * @return

? ? */

? ? public String buildDestFileName(File srcfile,String dest){

? ? ? ? if(dest==null){//沒有給出目標(biāo)路徑時

? ? ? ? ? ? if(srcfile.isDirectory()){

? ? ? ? ? ? ? ? dest=srcfile.getParent()+File.separator+srcfile.getName()+".zip";

? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? String filename=srcfile.getName().substring(0,srcfile.getName().lastIndexOf("."));

? ? ? ? ? ? ? ? dest=srcfile.getParent()+File.separator+filename+".zip";

? ? ? ? ? ? }

? ? ? ? }else{

? ? ? ? ? ? createPath(dest);//路徑的創(chuàng)建

? ? ? ? ? ? if(dest.endsWith(File.separator)){

? ? ? ? ? ? ? ? String filename="";

? ? ? ? ? ? ? ? if(srcfile.isDirectory()){

? ? ? ? ? ? ? ? ? ? filename=srcfile.getName();

? ? ? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? ? ? filename=srcfile.getName().substring(0, srcfile.getName().lastIndexOf("."));

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? dest+=filename+".zip";

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return dest;

? ? }

? ? /**

? ? * 路徑創(chuàng)建

? ? * @param dest

? ? */

? ? private void createPath(String dest){

? ? ? ? File destDir=null;

? ? ? ? if(dest.endsWith(File.separator)){

? ? ? ? ? ? destDir=new File(dest);//給出的是路徑時

? ? ? ? }else{

? ? ? ? ? ? destDir=new File(dest.substring(0,dest.lastIndexOf(File.separator)));

? ? ? ? }

? ? ? ? if(!destDir.exists()){

? ? ? ? ? ? destDir.mkdirs();

? ? ? ? }

? ? }


? @org.junit.Test

? public void Test(){

? ? ? String src="/home/fenghao/document/書籍類資料/Maven實(shí)戰(zhàn) 高清掃描完整版.pdf";

? ? ? String dest="/home/fenghao/zip/maven/123.zip";

? ? ? zip(src, dest, true, "123456");

? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容