safer natural earth unzip

safe-natural-earth-unzip
Mike Barry 2024-03-03 16:46:44 -05:00
rodzic 40e64a2fd3
commit fa7af07f17
2 zmienionych plików z 6 dodań i 3 usunięć

Wyświetl plik

@ -15,7 +15,6 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
@ -101,10 +100,14 @@ public class NaturalEarthReader extends SimpleReader<SimpleFeature> {
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("No .sqlite file found inside " + path));
extracted = unzippedDir.resolve(URLEncoder.encode(zipEntry.toString(), StandardCharsets.UTF_8));
if (!extracted.startsWith(unzippedDir)) {
throw new IllegalArgumentException(
"Zip file tried to extract child outside of folder: " + zipEntry.getFileName());
}
FileUtils.createParentDirectories(extracted);
if (!keepUnzipped || FileUtils.isNewer(path, extracted)) {
LOGGER.info("unzipping {} to {}", path.toAbsolutePath(), extracted);
Files.copy(Files.newInputStream(zipEntry), extracted, StandardCopyOption.REPLACE_EXISTING);
FileUtils.safeCopy(Files.newInputStream(zipEntry), extracted);
}
if (!keepUnzipped) {
extracted.toFile().deleteOnExit();

Wyświetl plik

@ -268,7 +268,7 @@ public class FileUtils {
* @throws UncheckedIOException if an IO exception occurs
*/
public static void safeCopy(InputStream inputStream, Path destPath) {
try (var outputStream = Files.newOutputStream(destPath, StandardOpenOption.CREATE, WRITE)) {
try (var outputStream = Files.newOutputStream(destPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
int totalSize = 0;
int nBytes;