【发布时间】:2015-09-11 13:03:03
【问题描述】:
无法从目录列表创建 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 文件中提取文件,然后用加密重新压缩它们,因为我无法直接加密它们。
【问题讨论】:
-
Isn't: "java.io.FileNotFoundException: D:\DZipTest\sample - Copy (1) (Access is denied)" 够清楚了吗?
-
但是如果我注释掉用于压缩的代码,那么它会打印目录列表而不是抛出异常
-
为了快速修复,您可以尝试使用管理员权限运行 Java 程序。这可能可以解决访问问题。
-
我试过了。还修复了文件夹的权限,但仍然是同样的问题
-
列出文件/目录与打开和读取文件不同,尽管列出某些文件夹可能会遇到问题(
File#listFiles将返回null)