BAEL-6644-read-zip-entries (#14321)
* read zip file entries * add unit test * update test code
This commit is contained in:
parent
b119316bf4
commit
8e336ba020
|
@ -0,0 +1,31 @@
|
||||||
|
package com.baeldung.zipentries;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipFile;
|
||||||
|
|
||||||
|
public class ZipEntryReader {
|
||||||
|
|
||||||
|
public static void readZipEntries(String filePath) throws IOException {
|
||||||
|
try (ZipFile zipFile = new ZipFile(filePath)) {
|
||||||
|
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
||||||
|
while (entries.hasMoreElements()) {
|
||||||
|
ZipEntry entry = entries.nextElement();
|
||||||
|
// Check if entry is a directory
|
||||||
|
if (!entry.isDirectory()) {
|
||||||
|
// Read and process the entry contents using the inputStream
|
||||||
|
try (InputStream inputStream = zipFile.getInputStream(entry);
|
||||||
|
Scanner scanner = new Scanner(inputStream);) {
|
||||||
|
while (scanner.hasNextLine()) {
|
||||||
|
String line = scanner.nextLine();
|
||||||
|
System.out.println(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.baeldung.zipentries;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
public class ZipEntryReaderTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenZipFile_thenReadEntriesAndValidateContent() throws URISyntaxException, IOException {
|
||||||
|
Path zipFilePath = Paths.get(getClass().getClassLoader().getResource("zipFile.zip").toURI());
|
||||||
|
ZipEntryReader.readZipEntries(zipFilePath.toString());
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
Loading…
Reference in New Issue