无法使用zip4j压缩文件

时间:2015-06-25 05:04:50

标签: java zip4j

无法从目录列表创建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中提取文件并使用加密重新压缩它们,因为我无法直接加密它们。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码

Flexible Space Bar Button Item