Check ZIP and GZIP formats

This commit is contained in:
nazar-kutz 2021-02-22 13:14:10 +02:00
parent 584e7dbf6d
commit e44f763405
2 changed files with 43 additions and 8 deletions

View file

@ -324,6 +324,24 @@ public class Algorithms {
return test == ZIP_FILE_SIGNATURE; return test == ZIP_FILE_SIGNATURE;
} }
public static boolean checkFileSignature(InputStream inputStream, int fileSignature) throws IOException {
if (inputStream == null) return false;
int firstBytes;
if (isSmallFileSignature(fileSignature)) {
firstBytes = readSmallInt(inputStream);
} else {
firstBytes = readInt(inputStream);
}
if (inputStream.markSupported()) {
inputStream.reset();
}
return firstBytes == fileSignature;
}
public static boolean isSmallFileSignature(int fileSignature) {
return fileSignature == BZIP_FILE_SIGNATURE || fileSignature == GZIP_FILE_SIGNATURE;
}
/** /**
* Checks, whether the child directory is a subdirectory of the parent * Checks, whether the child directory is a subdirectory of the parent
* directory. * directory.
@ -547,6 +565,18 @@ public class Algorithms {
} }
} }
public static ByteArrayInputStream createByteArrayIS(InputStream inputStream) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int i = 0;
while ((i = inputStream.read()) != -1) {
outputStream.write(i);
}
inputStream.close();
byte[] byteArray = outputStream.toByteArray();
return new ByteArrayInputStream(byteArray);
}
@SuppressWarnings("ResultOfMethodCallIgnored") @SuppressWarnings("ResultOfMethodCallIgnored")
public static void updateAllExistingImgTilesToOsmandFormat(File f) { public static void updateAllExistingImgTilesToOsmandFormat(File f) {
if (f.isDirectory()) { if (f.isDirectory()) {

View file

@ -19,6 +19,7 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -28,7 +29,10 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import java.util.zip.ZipInputStream;
import static net.osmand.util.Algorithms.GZIP_FILE_SIGNATURE;
import static net.osmand.util.Algorithms.ZIP_FILE_SIGNATURE;
import static net.osmand.util.Algorithms.isEmpty; import static net.osmand.util.Algorithms.isEmpty;
public class OnlineRoutingHelper { public class OnlineRoutingHelper {
@ -118,14 +122,15 @@ public class OnlineRoutingHelper {
} }
private InputStream getInputStream(@NonNull HttpURLConnection connection) throws IOException { private InputStream getInputStream(@NonNull HttpURLConnection connection) throws IOException {
// todo check file signature correctly ByteArrayInputStream localIS = Algorithms.createByteArrayIS(connection.getInputStream());
// InputStream is = connection.getInputStream(); if (Algorithms.checkFileSignature(localIS, ZIP_FILE_SIGNATURE)) {
// int header = Algorithms.readTwoInt(is); ZipInputStream zipIS = new ZipInputStream(localIS);
// boolean isGzipFile = header == Algorithms.GZIP_FILE_SIGNATURE; zipIS.getNextEntry(); // set position to reading for the first item
// if (isGzipFile) { return zipIS;
// return new GZIPInputStream(connection.getInputStream()); } else if (Algorithms.checkFileSignature(localIS, GZIP_FILE_SIGNATURE)) {
// } return new GZIPInputStream(localIS);
return connection.getInputStream(); }
return localIS;
} }
public void saveEngine(@NonNull OnlineRoutingEngine engine) { public void saveEngine(@NonNull OnlineRoutingEngine engine) {