From 4fd4acef2ae7047f6b29bf9610acd20f1c775cc8 Mon Sep 17 00:00:00 2001 From: Luis Nassif Date: Thu, 21 Sep 2023 16:22:04 -0300 Subject: [PATCH] '#1861: use workaround just if needed, convert relative path to absolute --- .../iped/utils/FileInputStreamFactory.java | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/iped-utils/src/main/java/iped/utils/FileInputStreamFactory.java b/iped-utils/src/main/java/iped/utils/FileInputStreamFactory.java index 387c510bb4..a1484a0244 100644 --- a/iped-utils/src/main/java/iped/utils/FileInputStreamFactory.java +++ b/iped-utils/src/main/java/iped/utils/FileInputStreamFactory.java @@ -2,22 +2,36 @@ import java.io.File; import java.io.IOException; +import java.nio.file.InvalidPathException; import java.nio.file.Path; +import java.nio.file.Paths; import iped.io.SeekableInputStream; public class FileInputStreamFactory extends SeekableInputStreamFactory { + private static final boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase().startsWith("windows"); + public FileInputStreamFactory(Path dataSource) { super(dataSource.toUri()); } public File getFile(String subPath) { - if (System.getProperty("os.name").toLowerCase().startsWith("windows")) { - //this is a workaround to support cases where there are folders with trailing spaces on the name - return new File("\\\\?\\" + new File(subPath).getAbsolutePath()); - } else { - return new File(subPath); + Path source = Paths.get(this.dataSource); + try { + return source.resolve(subPath).toFile(); + + } catch (InvalidPathException e) { + File file = new File(subPath); + if (!file.isAbsolute()) { + file = new File(source.toFile(), subPath); + } + if (IS_WINDOWS) { + //this is a workaround to support cases where there are folders with trailing spaces on the name + return new File("\\\\?\\" + file.getAbsolutePath()); + } else { + return file; + } } }