Search code examples
javazip

Java zip Header


Im trying to get the Headers from files in a zip file. I've found this code

List<FileHeader> fileHeaders = new ZipFile("C:\\Users\\TH24\\Downloads\\Neuer Ordner (5)\\Extrahieren\\49805.zip").getFileHeaders();
fileHeaders.stream().forEach(fileHeader -> System.out.println(fileHeader.getFileName()));

Ouput: 13811.zip

using the zip4j Library. This code will output the right answer but its a normal console output. Idk how to convert it into a String as i never worked with lists (i guess) before. Note that there will always only be one file(also a zip) in the zip.


Solution

  • Instead of forEach, use a map() and add a default value for when nothing is found. Something like,

    String fileName = fileHeaders.stream().map(FileHeader::getFileName)
            .findFirst().orElse("Not found");