1

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 ?

jeremyvillalobos
  • 1,795
  • 2
  • 19
  • 39

4 Answers4

3

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

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.

Sage
  • 15,290
  • 3
  • 33
  • 38
  • This is on Android, plus the zip must be encrypted. zip4j is doing a good job so far. – jeremyvillalobos Oct 12 '13 at 02:23
  • Please [Android java.util.zip](http://developer.android.com/reference/java/util/zip/package-summary.html) class is similar. compare the java documentation with this one. you can easily adopt the working example. – Sage Oct 12 '13 at 03:34
  • @Sage but does it handle encrypted zip files? – Fredrik Oct 12 '13 at 06:12
  • no, it doesn't. sorry i missed to notice his mentioning of `encrypted`. However i have updated the answer. See that if it helps. – Sage Oct 12 '13 at 12:53
0

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



KuLdip PaTel
  • 1,069
  • 7
  • 19
0

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);
            }
        }
    }
Sepehr GH
  • 1,297
  • 1
  • 17
  • 39