2016-09-24 38 views
0

夥計們,提取ZIP在Java

我使用zip4j API提取Java中的.zip文件,並能夠提取文件

  1. 我已經習慣了拉鍊的完整目錄,使拉鍊和它包含的文件和嵌套目錄中,使用

    zipFile.addFolder(文件目錄,參數); // ZIP目錄中的文件/文件夾

  2. 提取使用

    ZipFile zipFile = new ZipFile(stringArchievedFile); 
    //Extracts all files to the path specified 
    zipFile.extractAll(stringExtractingFilePath); 
    

問題是提取後,文件應該被提取到我設置有zipFile.extractAll(path)方法,但正在創建一個多個目錄的路徑拉鍊。我如何可以將文件解壓到實際的指定目錄

像: 提取路徑 C:\ ExtractionPath

文件路徑 C:\ SelectingPath \文件1

C:\ SelectingPath \ File2

C:\ SelectingPath \ Directory1 \ File1

C:\ SelectingPath \ Directory2 \文件1

我將選擇C:\ SelectingPath目錄壓縮和

我將選擇C:\ ExtractionPath目錄提取文件

在提取後所有提取的文件將進入

** C:\ ExtractionPath \ SelectingPath **

我需要目錄中的所有文件

** C:\ ExtractionPath ** 本身。

請幫我解決這個問題。

在此先感謝

+2

不要壓縮了'SelectingPath'文件夾。壓縮內容。如果你打開zip文件,你會看到所有文件的路徑都以'SelectingPath'開始,而你不需要。 – Andreas

+0

您的意思是,我需要將所有文件和目錄添加到'ArrayList'並將其作爲參數傳遞給zipFile.addFiles()方法,如下所示:zipFile.addFiles(arrayListFilesFolders,parameters); – Bhat

+0

@Andreas,如果我使用ArrayList作爲文件和目錄並將它作爲參數傳遞給zipFile.addFile()方法,我會得到以下例外,並且無法將我選擇的完整目錄歸檔爲「ex」 =(net.lingala.zip4j.exception.ZipException)net.lingala.zip4j.exception.ZipException:輸入ArrayList中的一個或多個元素的類型不是File' – Bhat

回答

1

感謝安德烈亞斯& esprittn!

我們需要將ArrayList<File>作爲參數傳遞給addFiles(ArrayList, ZipParameters)方法,以便我們可以存檔目錄的整個目錄內容。我得到的輸出如預期

沿用了ARCHIVE碼流:

public void archieveFiles(File fileDirectory, String stringPassword) throws Exception { 
     try{ 

      String[] filesDirectoryList = fileDirectory.list(); 

      ArrayList<File> listFileDirectory = new ArrayList<>(); //To list the files to archive 
      for(int iListCount = 0; iListCount < filesDirectoryList.length; iListCount++){ 
       listFileDirectory.add(new File(fileDirectory+"\\"+filesDirectoryList[iListCount])); 
      } 

      ZipFile zipFile = new ZipFile("C:\\CreateZIP\\FileArchive.zip"); 
      //Initiate Zip Parameters which define various properties 
      ZipParameters parameters = new ZipParameters(); 
      // Set compression method to deflate compression 
      parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); 
      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); 
      //file encrypted with key strength of 192, then Zip4j can decrypt this file 
      parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256); 
      //Set password 
      parameters.setPassword(stringPassword); 
      // Zip the directory files 
      zipFile.addFiles(listFileDirectory, parameters); 
     } 
     catch(ZipException ex){ 
      Logj.errorLog(ex); 
     } 
     catch(Exception ex){ 
      Logj.errorLog(ex); 
     } 
    } 

提取

public void extractFilesForFirmwareZip(String stringArchievedFile, String stringExtractingFilePath, String stringFileEncrypt) throws Exception{ 
     try{ 
      // Initiate ZipFile object with the path/name of the zip file. 
      ZipFile zipFile = new ZipFile(stringArchievedFile); 
      //Initiate Zip Parameters which define various properties 
      //UnzipParameters parameters = new UnzipParameters(); 
      if(zipFile.isEncrypted()) 
       zipFile.setPassword(stringFileEncrypt); 
      //Extracts all files to the path specified 
      zipFile.extractAll(stringExtractingFilePath); 
     } 
     catch(ZipException ex){ 
      isValidArchiveFile = false; 
      Logj.doLog(ex.getMessage(), ex); 
     } 
     catch(Exception ex){ 
      throw ex; 
     } 
    } 
1

你嘗試從Zip4j site例子像這樣的:

/* 
* Copyright 2010 Srikanth Reddy Lingala 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, 
* software distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 

package net.lingala.zip4j.examples.extract; 

import net.lingala.zip4j.core.ZipFile; 
import net.lingala.zip4j.exception.ZipException; 

/** 
* Demonstrates extracting all files from a zip file 
* 
* @author Srikanth Reddy Lingala 
* 
*/ 
public class ExtractAllFiles { 

    public ExtractAllFiles() { 

     try { 
      // Initiate ZipFile object with the path/name of the zip file. 
      ZipFile zipFile = new ZipFile("c:\\ZipTest\\ExtractAllFiles.zip"); 

      // Extracts all files to the path specified 
      zipFile.extractAll("c:\\ZipTest"); 

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

    } 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     new ExtractAllFiles(); 
    } 

} 
+0

是的。我也使用了這個例子。但我面對的問題是一樣的。 – Bhat