码迷,mamicode.com
首页 > 其他好文 > 详细

Compress multiple files into a zip file and assign a password.

时间:2015-06-22 22:10:57      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

First things first, include maven dependency to download zip4j jar files

1 <dependency>  
2     <groupId>net.lingala.zip4j</groupId>  
3     <artifactId>zip4j</artifactId>  
4     <version>1.3.1</version>  
5 </dependency>

Second, write the zip method, either can be a static util method or a private method within a class.

/**
     * 
     * @param path, the absolute path where the zip file is
     * @param fileList, a list of File objects that is going to be zipped
     * @param password, need to be entered for extraction
     * Create a zip file with password protected
     * @throws ZipException 
     */
    public static void createZipWithPassword(String path, ArrayList<File> fileList, String password) throws ZipException {
            final ZipFile zipFile = new ZipFile(path);
            final ZipParameters parameters = new ZipParameters();
            parameters.setEncryptFiles(true);
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
            parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
            parameters.setPassword(password);
            zipFile.createZipFile(fileList, parameters);
    }

 

Compress multiple files into a zip file and assign a password.

标签:

原文地址:http://www.cnblogs.com/sengao/p/4593843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!