zoukankan      html  css  js  c++  java
  • Zip4j开源jar包的简单使用

    因为对项目突然要发送压缩加密的邮件附件,所以从网上看了一些资料说Zip4j开源框架比较好使,对中文的支持也比较好,所以从网上找了一个代码案例!自己写了一写,现在贴出来,方便以后想用的时候好找

    1、

      1 package com.fenghao.zip;
      2 
      3 import java.io.File;
      4 
      5 import java.util.ArrayList;
      6 import java.util.Collections;
      7 
      8 import net.lingala.zip4j.core.ZipFile;
      9 import net.lingala.zip4j.exception.ZipException;
     10 import net.lingala.zip4j.model.ZipParameters;
     11 import net.lingala.zip4j.util.Zip4jConstants;
     12 
     13 
     14 
     15 /**
     16  * 对文件进行压缩和加密
     17  * 对文件进行解压和解密
     18  * @author fenghao
     19  * 
     20  */
     21 public class CompressUtils {
     22 
     23     /**
     24      * 解压加密的压缩文件
     25      * @param zipfile
     26      * @param dest
     27      * @param passwd
     28      * @throws ZipException
     29      */
     30     public void unZip(File zipfile,String dest,String passwd) throws ZipException{
     31         ZipFile zfile=new ZipFile(zipfile);
     32 //        zfile.setFileNameCharset("GBK");//在GBK系统中需要设置
     33         if(!zfile.isValidZipFile()){
     34             throw new ZipException("压缩文件不合法,可能已经损坏!");
     35         }
     36         File file=new File(dest);
     37         if(file.isDirectory() && !file.exists()){
     38             file.mkdirs();
     39         }
     40         if(zfile.isEncrypted()){
     41             zfile.setPassword(passwd.toCharArray());
     42         }
     43         zfile.extractAll(dest);
     44     }
     45     /**
     46      * 压缩文件且加密
     47      * @param src
     48      * @param dest
     49      * @param is
     50      * @param passwd
     51      */
     52     public void zip(String src,String dest,boolean is,String passwd){
     53         File srcfile=new File(src);
     54         //创建目标文件
     55         String destname = buildDestFileName(srcfile, dest);
     56         ZipParameters par=new ZipParameters();
     57         par.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
     58         par.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
     59         if(passwd!=null){
     60             par.setEncryptFiles(true);
     61             par.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
     62             par.setPassword(passwd.toCharArray());
     63         }
     64         try {
     65             ZipFile zipfile=new ZipFile(destname);
     66             if(srcfile.isDirectory()){
     67                 if(!is){
     68                     File[] listFiles = srcfile.listFiles();
     69                     ArrayList<File> temp=new ArrayList<File>();
     70                     Collections.addAll(temp, listFiles);
     71                     zipfile.addFiles(temp, par);
     72                 }
     73                 zipfile.addFolder(srcfile, par);
     74             }else{
     75                 zipfile.addFile(srcfile, par);
     76             }
     77         } catch (ZipException e) {
     78             e.printStackTrace();
     79         }
     80         
     81         
     82     }
     83     /**
     84      * 目标文件名称
     85      * @param srcfile
     86      * @param dest
     87      * @return
     88      */
     89     public String buildDestFileName(File srcfile,String dest){
     90         if(dest==null){//没有给出目标路径时
     91             if(srcfile.isDirectory()){
     92                 dest=srcfile.getParent()+File.separator+srcfile.getName()+".zip";
     93             }else{
     94                 String filename=srcfile.getName().substring(0,srcfile.getName().lastIndexOf("."));
     95                 dest=srcfile.getParent()+File.separator+filename+".zip";
     96             }
     97         }else{
     98             createPath(dest);//路径的创建
     99             if(dest.endsWith(File.separator)){
    100                 String filename="";
    101                 if(srcfile.isDirectory()){
    102                     filename=srcfile.getName();
    103                 }else{
    104                     filename=srcfile.getName().substring(0, srcfile.getName().lastIndexOf("."));
    105                 }
    106                 dest+=filename+".zip";
    107             }
    108         }
    109         return dest;
    110     }
    111     /**
    112      * 路径创建
    113      * @param dest
    114      */
    115     private void createPath(String dest){
    116         File destDir=null;
    117         if(dest.endsWith(File.separator)){
    118             destDir=new File(dest);//给出的是路径时
    119         }else{
    120             destDir=new File(dest.substring(0,dest.lastIndexOf(File.separator)));
    121         }
    122         if(!destDir.exists()){
    123             destDir.mkdirs();
    124         }
    125     }
    126     
    127    @org.junit.Test
    128    public void Test(){
    129        String src="/home/fenghao/document/书籍类资料/Maven实战 高清扫描完整版.pdf";
    130        String dest="/home/fenghao/zip/maven/123.zip";
    131        zip(src, dest, true, "123456");
    132    }
    133 }

    2、因为自己创建的时maven项目,所以吧jar包依赖也贴出来!

      <!-- https://mvnrepository.com/artifact/net.lingala.zip4j/zip4j -->
        <!-- 对压缩文件和加密的支持 -->
        <dependency>
            <groupId>net.lingala.zip4j</groupId>
            <artifactId>zip4j</artifactId>
            <version>1.3.2</version>
        </dependency>

    private void createPath(String dest){
            File destDir=null;
            String separator=File.separator;
            if(dest.endsWith(separator)){
                destDir=new File(dest);
            }else{
                destDir=new File(dest.substring(0, dest.lastIndexOf("/")));//确认:/ \ 路径分割符号,第一种验证时空
                //使用/做路径分割时,
            }
            if(!destDir.exists()){
                destDir.mkdirs();
            }
        }

    在Windows测试环境可行

    private void createPath(String dest){
            File destDir=null;
            String separator=File.separator;
            if(dest.endsWith(separator)){
                destDir=new File(dest);
            }else{
                destDir=new File(dest.substring(0, dest.lastIndexOf("/")));//确认:/ \ 路径分割符号,第一种验证时空
                //使用/做路径分割时,
            }
            if(!destDir.exists()){
                destDir.mkdirs();
            }
        }

  • 相关阅读:
    centos6.5 源码安装 mysql
    centOS系统安装MySQL教程
    CENTOS下搭建SVN服务器
    定位记录,删除后定位到下一条记录上
    Delphi Edit输入+号(加号),不允许显示输入符号,清空Edit,显示事件
    [经常用此练习即可] SQL2000行转列三种方式解答,侧重于第二种方式,第一种需用临时表,第三种方式适合固定方式写入都正确
    Delphi与SQL模糊like通配符查询(转载)
    四舍五入可以用这种形式,保留2位小数!
    APP开发工具对比!!
    FastReport 使用技巧篇
  • 原文地址:https://www.cnblogs.com/nihaofenghao/p/6033231.html
Copyright © 2011-2022 走看看