简体   繁体   中英

zip4j How to extract a directory

I need to extract a directory from a zip using zip4j. I could find each file in the directory and extract it.

How do I list the files inside the directory ?

Or, is there a utility to extract the directory to a path ?

From Zip4j's ZipFile , you can get the list of all file headers in this zip file. And then you can check from this file header, if this "file" is a directory. If yes, extract it.

Below is a sample code to extract just the directories from a zip file

import java.util.List;

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

public class ExtractDirectory {

    public static void main(String[] args) {
        try {
            ZipFile zipFile = new ZipFile("myZipWithDirectories.zip");
            if (zipFile.isEncrypted()) {
                zipFile.setPassword("test".toCharArray());
            }

            @SuppressWarnings("unchecked")
            List<FileHeader> fileHeaders = zipFile.getFileHeaders();

            for(FileHeader fileHeader : fileHeaders) {
                if (fileHeader.isDirectory()) {
                    zipFile.extractFile(fileHeader, "anyValidPathToExtractTo");
                }
                //Alternatively, if you want to extract a directory by its name
                //if (fileHeader.isDirectory() && fileHeader.getFileName().equals("myDirectoryName")) {
                //  zipFile.extractFile(fileHeader, "anyValidPathToExtractTo");
                //}
            }

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

In zip4j, you should be able to take advantage myZipFile.getFileHeaders() or zipFile.getFileHeader("TargetFolder"); function to extract a target folder.

//List<FileHeader> fHeaders = myZipFile.getFileHeaders(); for all file Header

FileHeader targetFileHeader = zipFile.getFileHeader("TargetFolder"); 

if (targetFileHeader.isDirectory()) {
      File f = new File("anyGivenDirectory/" + targetFileHeader.getFileName());
    f.mkdirs();  // mkdirs() is different from mkdir()
    zipFile.extractFile(fileHeader, f.toString());
   }

The other known good library to my knowledge: zt-zip . But i am not sure whither it supports decryption.

Use this library www.lingala.net/zip4j/

add this jar file in lib folder of app.

check your import like that

import net.lingala.zip4j.core.ZipFile;

import net.lingala.zip4j.exception.ZipException;

import net.lingala.zip4j.model.FileHeader;

use below method like this

unzip("/sdcard/file.zip","/sdcard/unzipFolder")


 public static void unzip(String Filepath, String DestinationFolderPath) {

         try {
            ZipFile zipFile = new ZipFile(Filepath);
            List fileHeaders = zipFile.getFileHeaders();
            for(FileHeader fileHeader : fileHeaders) {
                String fileName = fileHeader.getFileName();

                if (fileName.contains("\\")) {
                    fileName=fileName.replace("\\","\\\\");
                    String[] Folders=fileName.split("\\\\");
                    StringBuilder newFilepath=new StringBuilder();
                    newFilepath.append(DestinationFolderPath);
                    for (int i=0;i< Folders.length-1;i++){
                        newFilepath.append(File.separator);
                        newFilepath.append(Folders[i]);
                    }
                    zipFile.extractFile(fileHeader, newFilepath.toString(),null,Folders[Folders.length-1]);
                }else {
                    zipFile.extractFile(fileHeader,DestinationFolderPath);
                }
            }

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



Using extractAll(destination) method does the extraction with files and directories. If you want to manually do it, try something this:

public void unzip(File zip, String destinationFolderPath) throws net.lingala.zip4j.exception.ZipException {
        ZipFile zipFile = new ZipFile(zip);
        List<FileHeader> fileHeaders = zipFile.getFileHeaders();
        for(FileHeader fileHeader : fileHeaders) {
            if (fileHeader.isDirectory()) {
                System.out.println(destinationFolderPath + fileHeader.getFileName());
                File f = new File(destinationFolderPath + fileHeader.getFileName());
                f.mkdirs();  // mkdirs() is different from mkdir()
                zipFile.extractFile(fileHeader, f.toString());
            }else {
                zipFile.extractFile(fileHeader, destinationFolderPath);
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM