Skip to content

Commit 22ee726

Browse files
authored
fix: fix logic to check for nested GTFS files in ZIP (#1972)
* #1912 Fix logic to check for nested GTFS files in ZIP
1 parent 426d638 commit 22ee726

File tree

1 file changed

+11
-27
lines changed

1 file changed

+11
-27
lines changed

core/src/main/java/org/mobilitydata/gtfsvalidator/input/GtfsInput.java

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,20 @@ public abstract class GtfsInput implements Closeable {
4949
*/
5050
public static GtfsInput createFromPath(Path path, NoticeContainer noticeContainer)
5151
throws IOException {
52-
ZipFile zipFile;
5352
if (!Files.exists(path)) {
5453
throw new FileNotFoundException(path.toString());
5554
}
5655
if (Files.isDirectory(path)) {
5756
return new GtfsUnarchivedInput(path);
5857
}
5958
String fileName = path.getFileName().toString().replace(".zip", "");
60-
if (path.getFileSystem().equals(FileSystems.getDefault())) {
61-
// Read from a local ZIP file.
62-
zipFile = new ZipFile(path.toFile());
63-
if (hasSubfolderWithGtfsFile(path)) {
64-
noticeContainer.addValidationNotice(
65-
new InvalidInputFilesInSubfolderNotice(invalidInputMessage));
66-
}
59+
ZipFile zipFile =
60+
path.getFileSystem().equals(FileSystems.getDefault())
61+
// Read from a local ZIP file.
62+
? new ZipFile(path.toFile())
63+
// Load a remote ZIP file to memory.
64+
: new ZipFile(new SeekableInMemoryByteChannel(Files.readAllBytes(path)));
6765

68-
return new GtfsZipFileInput(zipFile, fileName);
69-
}
70-
// Load a remote ZIP file to memory.
71-
zipFile = new ZipFile(new SeekableInMemoryByteChannel(Files.readAllBytes(path)));
7266
if (hasSubfolderWithGtfsFile(path)) {
7367
noticeContainer.addValidationNotice(
7468
new InvalidInputFilesInSubfolderNotice(invalidInputMessage));
@@ -98,23 +92,13 @@ public static boolean hasSubfolderWithGtfsFile(Path path) throws IOException {
9892
*/
9993
private static boolean containsGtfsFileInSubfolder(ZipInputStream zipInputStream)
10094
throws IOException {
101-
boolean containsSubfolder = false;
102-
String subfolder = null;
10395
ZipEntry entry;
10496
while ((entry = zipInputStream.getNextEntry()) != null) {
105-
String entryName = entry.getName();
106-
107-
if (entry.isDirectory()) {
108-
subfolder = entryName;
109-
containsSubfolder = true;
110-
}
111-
if (containsSubfolder && entryName.contains(subfolder) && entryName.endsWith(".txt")) {
112-
String[] files = entryName.split("/");
113-
String lastElement = files[files.length - 1];
114-
115-
if (GtfsFiles.containsGtfsFile(lastElement)) {
116-
return true;
117-
}
97+
String[] nameParts = entry.getName().split("/");
98+
boolean isInSubfolder = nameParts.length > 1;
99+
boolean isGtfsFile = GtfsFiles.containsGtfsFile(nameParts[nameParts.length - 1]);
100+
if (isInSubfolder && isGtfsFile) {
101+
return true;
118102
}
119103
}
120104
return false;

0 commit comments

Comments
 (0)