1

we can extract all the files from a zoip filder using extractAll method given in zip4j, but what if i need to extract only one kind of files,say only text files or only files which have a certain sub-string in the name of the file?? is there a way to do this using zip4j

i thought this question might be relating to my problem

Read Content from Files which are inside Zip file

but that's not exactly what i want. can anyone explain in detail about using this ZipEntry things, if it helps my problem getting solved?

Community
  • 1
  • 1
Vasanth Nag K V
  • 4,860
  • 5
  • 24
  • 48
  • did you check the http://www.lingala.net/zip4j website first? Does their forum answer this question? – Mike 'Pomax' Kamermans Aug 20 '14 at 02:42
  • i see this point mentioned on the forum "Create or extract files from Split Zip files (Ex: z01, z02,...zip)" but it does not say if you can extract files based on the type of file or by specifying a substring in the file name inside the zip folder – Vasanth Nag K V Aug 20 '14 at 06:12

1 Answers1

2

Try the below code

        ZipFile zipFile = new ZipFile("myzip.zip");

        // Get the list of file headers from the zip file
        List fileHeaderList = zipFile.getFileHeaders();

        // Loop through the file headers
        for (int i = 0; i < fileHeaderList.size(); i++) {
            FileHeader fileHeader = (FileHeader)fileHeaderList.get(i);              
            String fileName = fileHeader.getFileName();
            if(fileName.contains(".java")){
                zipFile.extractFile(fileHeader, "c:\\scrap\\");
            }

        }
prashant thakre
  • 5,061
  • 3
  • 26
  • 39
  • hi prashant, what if i dont know the file name and i have to extract only the text files out (which is my actual question) – Vasanth Nag K V Aug 20 '14 at 06:07
  • @VasanthNagKV I have corrected the code please see my answer above. Which will resolve your issue. – prashant thakre Aug 20 '14 at 09:39
  • 1
    @prashantthakre your code would extract also file "somefile.java.exe". Using case insensitive variant of ```endsWith``` might be better than ```contains``` – xmojmr Aug 21 '14 at 15:58