Improve zip resources handling
This commit is contained in:
@@ -224,8 +224,7 @@ public class FileInfoService {
|
|||||||
archiveInfo.viewUrl = "/view" + requestedUri + "/1";
|
archiveInfo.viewUrl = "/view" + requestedUri + "/1";
|
||||||
|
|
||||||
Path zipPath = Paths.get(fileApiConfiguration.directory() + "/file/" + requestedPath);
|
Path zipPath = Paths.get(fileApiConfiguration.directory() + "/file/" + requestedPath);
|
||||||
try {
|
try (ZipFile zipFile = new ZipFile(zipPath.toFile())) {
|
||||||
ZipFile zipFile = new ZipFile(zipPath.toFile());
|
|
||||||
zipFile.stream().filter(e -> !e.isDirectory()).forEach(e -> {
|
zipFile.stream().filter(e -> !e.isDirectory()).forEach(e -> {
|
||||||
String zipEntryName = e.getName();
|
String zipEntryName = e.getName();
|
||||||
String zipEntryNameUri =
|
String zipEntryNameUri =
|
||||||
|
|||||||
@@ -112,13 +112,14 @@ public class FileServeService {
|
|||||||
ZipEntry zipEntry = zipFile.getEntry(name);
|
ZipEntry zipEntry = zipFile.getEntry(name);
|
||||||
|
|
||||||
if (zipEntry == null) {
|
if (zipEntry == null) {
|
||||||
|
zipFile.close();
|
||||||
throw new IOException();
|
throw new IOException();
|
||||||
}
|
}
|
||||||
|
|
||||||
ResponseBuilder response;
|
ResponseBuilder response;
|
||||||
long fileSize = zipEntry.getSize();
|
long fileSize = zipEntry.getSize();
|
||||||
|
|
||||||
response = Response.ok(zipFile.getInputStream(zipEntry));
|
response = Response.ok(new ZipEntryInputStream(zipFile, zipFile.getInputStream(zipEntry)));
|
||||||
response.header("Content-Length", Long.toString(fileSize));
|
response.header("Content-Length", Long.toString(fileSize));
|
||||||
|
|
||||||
response.header("Accept-Ranges", "bytes");
|
response.header("Accept-Ranges", "bytes");
|
||||||
@@ -221,3 +222,62 @@ class FileServeInputStream extends InputStream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ZipEntryInputStream extends InputStream {
|
||||||
|
private final ZipFile zipFile;
|
||||||
|
private final InputStream is;
|
||||||
|
|
||||||
|
public ZipEntryInputStream(ZipFile zipFile, InputStream is) {
|
||||||
|
this.zipFile = zipFile;
|
||||||
|
this.is = is;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int read() throws IOException {
|
||||||
|
return this.is.read();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int read(byte[] buffer) throws IOException {
|
||||||
|
return this.is.read(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int read(byte[] buffer, int off, int len) throws IOException {
|
||||||
|
return this.is.read(buffer, off, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] readAllBytes() throws IOException {
|
||||||
|
return this.is.readAllBytes();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] readNBytes(int len) throws IOException {
|
||||||
|
return this.is.readNBytes(len);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int readNBytes(byte[] buf, int off, int len) throws IOException {
|
||||||
|
return this.is.readNBytes(buf, off, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long skip(long n) throws IOException {
|
||||||
|
return this.is.skip(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int available() throws IOException {
|
||||||
|
return this.is.available();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
try {
|
||||||
|
this.is.close();
|
||||||
|
} finally {
|
||||||
|
this.zipFile.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user