Search code examples
javaservletszip4j

Extract ZIP in java


Folks,

I am extracting the .zip files in java using zip4j API and able to extract the files

  1. I have used to zip the complete Directory to make zip and it contains the files and nested directory, using

    zipFile.addFolder(fileDirectory, parameters);//ZIP directory files/folders

  2. Extracting the zip using

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

The problem is after the extraction, files should be extracted to the path I provided with the zipFile.extractAll(path) method but one more directory is being created. How can I extract the files to the actual specified directory

Like: Extracting Path C:\ExtractionPath

Files Path C:\SelectingPath\File1

C:\SelectingPath\File2

C:\SelectingPath\Directory1\File1

C:\SelectingPath\Directory2\File1

I will select the C:\SelectingPath directory to zip and

I will select the C:\ExtractionPath directory to extract the files

after the extractions all the extracted files will be go into the

**C:\ExtractionPath\SelectingPath**

I need all the files in the directory in

**C:\ExtractionPath** itself.

Please help me to resolve this issues.

Thanks in advance


Solution

  • Thanks Andreas & esprittn!!

    We need to pass the ArrayList<File> as the arguments to the addFiles(ArrayList, ZipParameters) method so that we can archive the entire directory contents of the directory. I got the output as expected

    Follows the ARCHIVE code flow:

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

    and the EXTRACTION is

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