Skip to content

Commit

Permalink
fix: fix logic to check for nested GTFS files in ZIP (#1972)
Browse files Browse the repository at this point in the history
* #1912 Fix logic to check for nested GTFS files in ZIP
  • Loading branch information
sylvansson authored Feb 12, 2025
1 parent 426d638 commit 22ee726
Showing 1 changed file with 11 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,20 @@ public abstract class GtfsInput implements Closeable {
*/
public static GtfsInput createFromPath(Path path, NoticeContainer noticeContainer)
throws IOException {
ZipFile zipFile;
if (!Files.exists(path)) {
throw new FileNotFoundException(path.toString());
}
if (Files.isDirectory(path)) {
return new GtfsUnarchivedInput(path);
}
String fileName = path.getFileName().toString().replace(".zip", "");
if (path.getFileSystem().equals(FileSystems.getDefault())) {
// Read from a local ZIP file.
zipFile = new ZipFile(path.toFile());
if (hasSubfolderWithGtfsFile(path)) {
noticeContainer.addValidationNotice(
new InvalidInputFilesInSubfolderNotice(invalidInputMessage));
}
ZipFile zipFile =
path.getFileSystem().equals(FileSystems.getDefault())
// Read from a local ZIP file.
? new ZipFile(path.toFile())
// Load a remote ZIP file to memory.
: new ZipFile(new SeekableInMemoryByteChannel(Files.readAllBytes(path)));

return new GtfsZipFileInput(zipFile, fileName);
}
// Load a remote ZIP file to memory.
zipFile = new ZipFile(new SeekableInMemoryByteChannel(Files.readAllBytes(path)));
if (hasSubfolderWithGtfsFile(path)) {
noticeContainer.addValidationNotice(
new InvalidInputFilesInSubfolderNotice(invalidInputMessage));
Expand Down Expand Up @@ -98,23 +92,13 @@ public static boolean hasSubfolderWithGtfsFile(Path path) throws IOException {
*/
private static boolean containsGtfsFileInSubfolder(ZipInputStream zipInputStream)
throws IOException {
boolean containsSubfolder = false;
String subfolder = null;
ZipEntry entry;
while ((entry = zipInputStream.getNextEntry()) != null) {
String entryName = entry.getName();

if (entry.isDirectory()) {
subfolder = entryName;
containsSubfolder = true;
}
if (containsSubfolder && entryName.contains(subfolder) && entryName.endsWith(".txt")) {
String[] files = entryName.split("/");
String lastElement = files[files.length - 1];

if (GtfsFiles.containsGtfsFile(lastElement)) {
return true;
}
String[] nameParts = entry.getName().split("/");
boolean isInSubfolder = nameParts.length > 1;
boolean isGtfsFile = GtfsFiles.containsGtfsFile(nameParts[nameParts.length - 1]);
if (isInSubfolder && isGtfsFile) {
return true;
}
}
return false;
Expand Down

0 comments on commit 22ee726

Please sign in to comment.