使用zip4j加密压缩文件

标签

Java,zip4j,zip

前言

最近做项目需要生成压缩文件,并在打开时需要输入密码。千辛万苦的在搜索引擎的海洋上寻找,zip4j向我招手。记录下来,以便后用。

环境

Eclipse-luna+maven

添加依赖

<dependency>

<groupId>net.lingala.zip4jgroupId>

                  <artifactId>zip4jartifactId>

         <version>1.3.1version>

dependency>

关键代码

/**

          * 压缩文件,并对压缩文件进行加密

          *

          * @param zipFile

          *            目标压缩文件存储地址,如已存在就抛异常

          * @param srcDir

          *            需要被压缩的源文件目录,

          * @param pwd

          *            打开压缩文件需要的密码

          * @throws Exception

          * @author huaxq

          */

         public static void compress(String zipFile, String srcDir, String pwd)

                            throws Exception {

                   File zipFileD = new File(zipFile);

                   if (zipFileD.exists())

                            throw new Exception("zipFile已存在~!");

                   File srcDirD = new File(srcDir);

                   if (srcDirD == null || !srcDirD.isDirectory()

                                     || srcDirD.listFiles() == null

                                     || srcDirD.listFiles().length < 1)

                            throw new Exception("srcDir不是文件夹或者该文件夹下没有文件");

 

                   ZipFile zipFileT = new ZipFile(zipFileD);

                   ArrayList filesToAdd = new ArrayList();

                   // File[] files = srcDirD.listFiles();

                   List files = generateFiles(srcDirD);

                   for (File f : files) {

                            filesToAdd.add(f);

                   }

                   ZipParameters parameters = new ZipParameters();

                   parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);

 

                   parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

                   parameters.setEncryptFiles(true);

 

                   parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);

 

                   parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);

                   parameters.setPassword(pwd);

 

                   zipFileT.addFiles(filesToAdd, parameters);

         }

         /**

          * 遍历目录里所有文件,包括子文件夹中的

          * @param srcDirD        

          *  @author huaxq

          *  @return

          */

         private static List generateFiles(File srcDirD) {

                   // TODO Auto-generated method stub

                   List result = new ArrayList();

                   File[] files = srcDirD.listFiles();

 

                   for (File f : files) {

 

                            if (f.isDirectory()) {

                                     List t = generateFiles(f);

                                     result.addAll(t);

                            } else {

                                     result.add(f);

                            }

 

                   }

                   return result;

         }

后记

对文件加密是非常常见的一种情形,以后还要多学习加密相关的知识 !~

转载于:https://my.oschina.net/huaxiaoqiang/blog/2986386

你可能感兴趣的:(使用zip4j加密压缩文件)