2015-06-25 14 views
1

Невозможно создать zip-файл из списка каталогов. Я могу читать каталоги и печатать их. Как всегда, когда я пытаюсь застегнуть их, его бросали ошибки:Невозможно выполнить zip-файлы с помощью zip4j

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 и повторно сжать их с шифрованием, потому что я не могу зашифровать их напрямую.

+3

Не: "java.io.FileNotFoundException: D: \ DZipTest \ образец - Копировать (1) (доступ запрещен)" достаточно ясно? – User404

+0

Но если я прокомментирую код, который используется для zipping, то он печатает список каталогов, а не исключает исключение – kittu

+0

. Для быстрого исправления вы можете попробовать запустить свою Java-программу с правами администратора. Это _might_ обойти проблему доступа. –

ответ

0

Вы можете попробовать ниже код

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);   
     } 
    } 
    }