Fix zip entry names in path

This commit is contained in:
2026-04-18 20:24:27 +02:00
parent 46b6f4867e
commit 3cc928ebc8
2 changed files with 13 additions and 14 deletions

View File

@@ -8,7 +8,6 @@ import java.net.URLDecoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import sh.rhiobet.lalafin.api.configuration.FileApiConfiguration; import sh.rhiobet.lalafin.api.configuration.FileApiConfiguration;
@@ -34,11 +33,7 @@ public class PathFactory {
if (zipIndex >= 0 && zipIndex < names.size() - 1) { if (zipIndex >= 0 && zipIndex < names.size() - 1) {
FileSystemPath zipFilePath = FileSystemPath zipFilePath =
new FileSystemPath(names.subList(0, zipIndex + 1), rootFolderPath); new FileSystemPath(names.subList(0, zipIndex + 1), rootFolderPath);
String zipEntryName = names.stream() return new ZipEntryPath(zipFilePath, names.subList(zipIndex + 1, names.size()));
.skip(zipIndex + 1)
.map(PathSegment::getPath)
.collect(Collectors.joining("/"));
return new ZipEntryPath(zipFilePath, zipEntryName);
} else { } else {
return new FileSystemPath(names, rootFolderPath); return new FileSystemPath(names, rootFolderPath);
} }

View File

@@ -4,8 +4,9 @@ import jakarta.ws.rs.core.PathSegment;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Stream; import java.util.stream.Collectors;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
@@ -17,20 +18,23 @@ public final class ZipEntryPath implements Path {
FileSystemPath zipFilePath; FileSystemPath zipFilePath;
List<String> segments; List<String> segments;
String entryName;
ZipEntryPath(List<PathSegment> names, java.nio.file.Path rootFolderPath) { ZipEntryPath(FileSystemPath parent, List<PathSegment> entryName) {
this(new FileSystemPath(names.subList(0, names.size() - 1), rootFolderPath), this.zipFilePath = parent;
names.getLast().getPath()); this.segments = entryName.stream().map(PathSegment::getPath).toList();
this.entryName = this.segments.stream().collect(Collectors.joining("/"));
} }
ZipEntryPath(FileSystemPath parent, String entryName) { ZipEntryPath(FileSystemPath parent, String entryName) {
this.segments = Stream.concat(parent.segments.stream(), Stream.of(entryName)).toList();
this.zipFilePath = parent; this.zipFilePath = parent;
this.segments = Arrays.asList(entryName.split("/"));
this.entryName = entryName;
} }
@Override @Override
public String getURI() { public String getURI() {
return this.segments.stream() return this.zipFilePath.getURI() + this.segments.stream()
.map(s -> "/" + Encode.encodePathSegment(s)) .map(s -> "/" + Encode.encodePathSegment(s))
.reduce("", String::concat); .reduce("", String::concat);
} }
@@ -70,12 +74,12 @@ public final class ZipEntryPath implements Path {
public InputStream getInputStream() throws IOException { public InputStream getInputStream() throws IOException {
ZipFile zipFile = new ZipFile(this.zipFilePath.absolutePath.toFile()); ZipFile zipFile = new ZipFile(this.zipFilePath.absolutePath.toFile());
return new ZipEntryInputStream(zipFile, return new ZipEntryInputStream(zipFile,
zipFile.getInputStream(zipFile.getEntry(this.segments.getLast()))); zipFile.getInputStream(zipFile.getEntry(this.entryName)));
} }
private void fetchEntryData() { private void fetchEntryData() {
try (ZipFile zipFile = new ZipFile(this.zipFilePath.absolutePath.toFile())) { try (ZipFile zipFile = new ZipFile(this.zipFilePath.absolutePath.toFile())) {
ZipEntry zipEntry = zipFile.getEntry(this.segments.getLast()); ZipEntry zipEntry = zipFile.getEntry(this.entryName);
this.exists = zipEntry != null; this.exists = zipEntry != null;
if (this.exists) { if (this.exists) {