Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix logic to check for nested GTFS files in ZIP #1972

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading