Commit b312c808 authored by Riegel's avatar Riegel
Browse files

Refactor: Rework CityGmlArchive to use Streams for parsing

parent 7dea019b
Showing with 15 additions and 15 deletions
+15 -15
......@@ -8,6 +8,7 @@ import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serial;
......@@ -17,8 +18,11 @@ import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class CityGmlArchive {
......@@ -31,29 +35,25 @@ public class CityGmlArchive {
public static CityGmlArchive fromZipFile(String zipFile, ParserConfiguration config) {
ArrayList<CityGmlZipEntry> archiveEntries = new ArrayList<>();
Path tmpDir = null;
try (ZipFile zipFileObj = new ZipFile(zipFile)) {
tmpDir = Files.createTempDirectory("zipTmp");
tmpDir.toFile().deleteOnExit();
for (Enumeration<? extends ZipEntry> entries = zipFileObj.entries(); entries.hasMoreElements();) {
ZipEntry entry = entries.nextElement();
if (entry.isDirectory()) {
ZipFile zip = null;
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));) {
zip = new ZipFile(zipFile);
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null )
{
if (ze.isDirectory()) {
continue;
}
if (entry.getName().endsWith(".gml")) {
InputStream is = zipFileObj.getInputStream(entry);
archiveEntries.add(CityGmlZipEntry.of(entry, config, zipFile, tmpDir, is));
if (ze.getName().endsWith(".gml")) {
archiveEntries.add(CityGmlZipEntry.of(ze, zip, config));
}
}
} catch (IOException e) {
logger.error(e);
} catch (Exception e){
// Catch and rethrow other Exceptions to ensure tmpDir deletion in finally block
throw e;
} finally {
if (tmpDir != null) {
if(zip != null){
try {
FileUtils.deleteDirectory(tmpDir.toFile());
zip.close();
} catch (IOException e) {
logger.error(e);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment