用Java创建受密码保护的 Zip 文件
首先, Java没有任何函数或包可以在不使用某些本机代码的情况下创建受密码保护的 zip 文件。 Java有一些很好的有用的库,但是它们使用一些本机代码来执行它们的任务,这使得它们的使用平台在某种程度上依赖。而在 zip4j 库中,我们只使用Java代码而不支持任何本机代码,这就是人们使用 jip4j 而不是其他内置库的原因。
有一个名为“zip4j”的库,它是由“ Srikanth Reddy Lingala”在 2008/2009 年编写的。它是最全面的 zip 文件Java库。
zip4j 库的一些重要特性如下:
- 它将创建、添加、提取、更新、删除 zip 文件中的文件。
- 它支持像 ZipInputStream 和 ZipOuputStream 这样的流。
- 我们可以使用这个库在Java创建任何受密码保护的 zip 文件。
- 我们可以通过指定文件路径来压缩/解压缩任何现有的文件或目录。
- 它还提供对 AES 128/256 加密和标准 Zip 加密的支持。
Note: ‘zip4j’ is written on JDK 8 and if we try to run this library on JDK 7, then not all features will be supported.
方法:
- 使用Java.util.zip 包的幼稚方法
- 有效的方法
方法 1:天真的方法
我们没有任何标准的Java内置库来创建受密码保护的 zip 文件。我们只能借助Java.util.zip包制作一个简单的 zip 文件。
例子
Java
// Java Program to Create Password Protected Zip File
// Importing all input output classes
import java.io.*;
// Importing java.nio utility package API
// used for manipulating files and directories
// mostly working on Path object
import java.nio.file.*;
// Importing all zip classes from
// java.util package
import java.util.zip.*;
// Class
// Main class
public class ZipFile {
// Method 1
// Creating an existing file as a zip file
private static void zipFile(String filePath) {
// Try block to check if any exception occurs
try {
// Creating an object of File type
// getting filePath by passing as an argument
File file = new File(filePath);
// Getting the name of the above file
// as an ZIP format
String zipFileName = file.getName().concat(".zip");
// Creating FileOutputStream object and passing the
// zip file as an argument
FileOutputStream fos = new FileOutputStream(zipFileName);
// Creating sn object of FileOutputStream and
// passing the above object as an argument
ZipOutputStream zos = new ZipOutputStream(fos);
zos.putNextEntry(new ZipEntry(file.getName()));
// Writing the ZipEntry to stream is not enough
// as we need to write its content as well
// using the putNextEntry() method
byte[] bytes = Files.readAllBytes(Paths.get(filePath));
zos.write(bytes, 0, bytes.length);
// ZipFile can only hold 1 entry stream at a go, &
// if another entry is to be passed then
// current entry stream has to be closed closeEntry
// Closing the current entry stream
// using the closeEntry() method
zos.closeEntry();
// Closing the entire stream completely
zos.close();
// If file dose not find then it will return the file does not exist
}
// Catch block 1
// Catch block to handle FieNotFoundException
catch (FileNotFoundException ex) {
// Print and display message
System.err.format("The file does not exist", filePath);
}
// Catch block 2
// Catch block to handle input output exception
catch (IOException ex) {
// Print and display message
System.err.println("ERROR: " + ex);
}
}
// Method 2
// Main driver method
public static void main(String[] args) {
String filePath = args[0];
// Calling the above method 1
zipFile(filePath);
}
}
Java
// Java Program to Create Password Protected Zip File
// Importing required libraries
// Here, lingala is the name of men
// who created zip4j library
import java.io.File;
import java.util.ArrayList;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
// Class
// To create password protected ZIP file
public class GFG {
// Main driver method
public static void main(String[] args) {
// Try block to check if any exception occurs
try {
// Creating encryption zipParameters
// for passward protection
ZipParameters zipParameters = new ZipParameters();
// Setting encryption files
zipParameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
zipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
// Setting encryption of files to true
zipParameters.setEncryptFiles(true);
// Setting encryption method
zipParameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
// Set the key strength
zipParameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
// Set the password
zipParameters.setPassword("password");
// *********CREATE ZIP FILE***************
//Zip file name
String destinationZipFilePath = "D:/myZipFile.zip";
// Creating ZIP file
ZipFile zipFile = new ZipFile(destinationZipFilePath);
// Creating list of files to be added to ZIP file
ArrayList list = new ArrayList();
//Add SPECIFIC files
list.add(new File("D:/myFile1.txt"));
list.add(new File("D:/myFile2.txt"));
// Pass and ZIP parameters
// for Zip file to be created
zipFile.addFiles(list, zipParameters);
// Print the destination in the local directory
// where ZIP file is created
System.out.println("Password protected Zip file"
+ "have been created at " + destinationZipFilePath);
// Catch block to handle the exceptions
} catch (ZipException e) {
// Print the exception and line number
// where tit occured
e.printStackTrace();
}
}
}
方法二:高效方法
程序:
- 首先,创建要添加到 ZIP 文件的文件列表,然后
- 指定路径您可以将其添加到列表中。
例子
Java
// Java Program to Create Password Protected Zip File
// Importing required libraries
// Here, lingala is the name of men
// who created zip4j library
import java.io.File;
import java.util.ArrayList;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
// Class
// To create password protected ZIP file
public class GFG {
// Main driver method
public static void main(String[] args) {
// Try block to check if any exception occurs
try {
// Creating encryption zipParameters
// for passward protection
ZipParameters zipParameters = new ZipParameters();
// Setting encryption files
zipParameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
zipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
// Setting encryption of files to true
zipParameters.setEncryptFiles(true);
// Setting encryption method
zipParameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
// Set the key strength
zipParameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
// Set the password
zipParameters.setPassword("password");
// *********CREATE ZIP FILE***************
//Zip file name
String destinationZipFilePath = "D:/myZipFile.zip";
// Creating ZIP file
ZipFile zipFile = new ZipFile(destinationZipFilePath);
// Creating list of files to be added to ZIP file
ArrayList list = new ArrayList();
//Add SPECIFIC files
list.add(new File("D:/myFile1.txt"));
list.add(new File("D:/myFile2.txt"));
// Pass and ZIP parameters
// for Zip file to be created
zipFile.addFiles(list, zipParameters);
// Print the destination in the local directory
// where ZIP file is created
System.out.println("Password protected Zip file"
+ "have been created at " + destinationZipFilePath);
// Catch block to handle the exceptions
} catch (ZipException e) {
// Print the exception and line number
// where tit occured
e.printStackTrace();
}
}
}
输出:运行此代码后,特定文件的受密码保护的 Zip 文件已在 D:/myZipFile.zip 中创建。
输出说明:
执行I N上面的图片前,我们可以看到1个目录和2个文件名为GeeksForGeeks.txt和krishana_97
现在执行后,我们可以看到创建的新 zip 文件。这个 zip 文件是密码抗议的 zip 文件。您必须提供正确的密码才能访问此 zip 中的文件。