gpt4 book ai didi

java - 无法使用 zip4j 压缩文件

转载 作者:太空宇宙 更新时间:2023-11-04 13:43:59 27 4
gpt4 key购买 nike

无法从目录列表创建 zip 文件。我能够读取目录并打印它们。然而,当我尝试压缩它们时,它抛出错误:

net.lingala.zip4j.exception.ZipException: java.io.FileNotFoundException: D:\DZipTest\sample - Copy (1) (Access is denied)
at net.lingala.zip4j.core.ZipFile.readZipInfo(ZipFile.java:431)
at net.lingala.zip4j.core.ZipFile.checkZipModel(ZipFile.java:935)
at net.lingala.zip4j.core.ZipFile.addFiles(ZipFile.java:263)
at net.lingala.zip4j.examples.zip.AddFilesWithAESEncryption2.<init>(AddFilesWithAESEncryption2.java:107)
at net.lingala.zip4j.examples.zip.AddFilesWithAESEncryption2.main(AddFilesWithAESEncryption2.java:121)
Caused by: java.io.FileNotFoundException: D:\DZipTest\sample - Copy (1) (Access is denied)
at java.io.RandomAccessFile.open(Native Method)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:241)
at net.lingala.zip4j.core.ZipFile.readZipInfo(ZipFile.java:420)
... 4 more

这是类文件:

                public AddFilesWithAESEncryption2() 
{
String ExtractedFiles = "D:/DZipTest/";
String Directories;
File folder2 = new File(ExtractedFiles);
File[] listOfFiles2 = folder2.listFiles();

for (int i = 0; i < listOfFiles2.length; i++)
{
if (listOfFiles2[i].isDirectory())
{
Directories = listOfFiles2[i].getName();
String filesToBeZipped = "D:/DZipTest/" + Directories;
System.out.println(Directories);

// Initiate ZipFile object with the path/name of the zip file.
ZipFile zipFile = new ZipFile(filesToBeZipped);

// Build the list of files to be added in the array list
// Objects of type File have to be added to the ArrayList
ArrayList filesToAdd = new ArrayList();
filesToAdd.add(new File(filesToBeZipped));

// Initiate Zip Parameters
ZipParameters parameters = new ZipParameters();
// set compression method to deflate compression
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);

// DEFLATE_LEVEL_NORMAL - Optimal balance between compression level/speed
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

// Set the encryption flag to true
parameters.setEncryptFiles(true);

// Set the encryption method to AES Zip Encryption
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);

// Set password
parameters.setPassword("test");

// Now add files to the zip file
// Note: To add a single file, the method addFile can be used
// Note: If the zip file already exists and if this zip file is a split file
// then this method throws an exception as Zip Format Specification does not
// allow updating split zip files
zipFile.addFiles(filesToAdd, parameters);
}
}

} catch (Exception e) {
e.printStackTrace();
}
}

我想做的是从 zip 中提取文件并通过加密重新压缩它们,因为我无法直接加密它们。

最佳答案

您可以尝试下面的代码

   public static void generateZip(String sourceFolder, String zipFile){
fileList = new ArrayList<String>();

generateFileList(new File(sourceFolder), sourceFolder);

byte[] buffer = new byte[1024];

try{

FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);

for(String file : fileList){

ZipEntry ze= new ZipEntry(file);
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(sourceFolder + File.separator + file);

int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
}

zos.closeEntry();
zos.close();

}catch(IOException ex){

}
}

public static void generateFileList(File node, String sourceFilePath){
if(node.isDirectory()){
String[] subNote = node.list();
for(String filename : subNote){
fileList.add(filename);
}
}
}

关于java - 无法使用 zip4j 压缩文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31041603/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com