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

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);
    }
时间: 2024-10-11 13:23:06

Compress multiple files into a zip file and assign a password.的相关文章

python3 operate ZIP file

>>>import zipfile , os //infomation for zip file >>>zipFileHandle = zipfile.ZipFile('example.zip')   //create zip file handle >>>zipFileHandle.namelist()      //return all files and directorys in the zip file >>>spamInf

[java ] java.util.zip.ZipException: error in opening zip file

严重: Failed to processes JAR found at URL [jar:file:/D:/tools/apache-tomcat-7.0.64_2/webapps/bbs/WEB-INF/lib/org.restlet.ext.servlet-2.3.0.jar!/] for static resources to be included in context with name [jar:file:/D:/tools/apache-tomcat-7.0.64_2/webap

How do I create a zip file?(转)

Creating a zip file is a task that can easily be accomplished by using the classes ZipOutputStream and ZipEntry in the java.util.zip package. First of all, I'm going to present to you a complete code snippet that will create a zip file containing all

How to attach multiple files in the Send Mail Task in SSIS

Let’s say you need to create a SSIS package that creates 2 files and emails the files to someone. Your package may look something like this: Double-click the Send Mail Task to open the Send Mail Task Editor (shown below). Fill in all the relevant fie

Android Studio 首坑 Gradle sync failed: Cause: error in opening zip file 的错误

前言 本文章主要通过介绍如果Android Studio安装时出现包错误的解决方式及思路,授予人鱼不如授予人渔. 环境: 操作系统:Windows 7 SP1 安卓编译环境:Android studio 2.3.1 JDK:jdk1.8.0_102 错误: Gradle sync failed: Cause: error in opening zip file  Consult IDE log for more details (Help | Show Log) 经历: 1.尝试用是 Sync

一个坑poi大数据量导入时的java.lang.IllegalStateException: Zip File is closed

本帖子没有答案,只是说一下坑,余下的自己决定. Caused by: java.lang.IllegalStateException: Zip File is closed at org.apache.poi3.openxml4j.util.ZipFileZipEntrySource.getEntries(ZipFileZipEntrySource.java:45) at org.apache.poi3.openxml4j.opc.ZipPackage.getPartsImpl(ZipPacka

eclipse Archive for required library in project cannot be read or is not a valid ZIP file

遇到问题: 由于项目开发需要用到spark的第三方jar包,于是下载spark的jar包并引入,显示问题如下: Archive for required library: 'F:/lib/spark-assembly-1.5.2-hadoop2.4.0.jar' in project 'project' cannot be read or is not a valid ZIP file Archive for required library: 'F:/lib/spark-examples-1.

maven error: 用maven下载的spring jar包出现该异常 cannot be read or is not a valid ZIP file

使用maven获取相关的依赖jar包时,出现异常:Archive for required library: 'D:/maven/repository/org/springframework/spring-context/4.0.0.RELEASE/ in project 'webmvc' cannot be read or is not a valid ZIP file 但是我去相关的目录找到了相关的jar包,但是IDE中仍然报错:在搜索引擎上找到了一个处理的方法,就是将该jar包的上一级目录

java.util.zip.ZipException: error in opening zip file

今天项目升级,出现了很奇葩的问题.在本地和测试环境测试好好的,将项目打包成war(每次都这样打包)然后上传到阿里云服务器,启动tomcat一直报如下错误: java.util.zip.ZipException: error in opening zip file at java.util.zip.ZipFile.open(Native Method) at java.util.zip.ZipFile.<init>(ZipFile.java:220) at java.util.zip.ZipFi