Check ZIP and GZIP formats
This commit is contained in:
parent
584e7dbf6d
commit
e44f763405
2 changed files with 43 additions and 8 deletions
|
@ -324,6 +324,24 @@ public class Algorithms {
|
|||
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
|
||||
* 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")
|
||||
public static void updateAllExistingImgTilesToOsmandFormat(File f) {
|
||||
if (f.isDirectory()) {
|
||||
|
|
|
@ -19,6 +19,7 @@ import org.json.JSONException;
|
|||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
@ -28,7 +29,10 @@ import java.util.LinkedHashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
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;
|
||||
|
||||
public class OnlineRoutingHelper {
|
||||
|
@ -118,14 +122,15 @@ public class OnlineRoutingHelper {
|
|||
}
|
||||
|
||||
private InputStream getInputStream(@NonNull HttpURLConnection connection) throws IOException {
|
||||
// todo check file signature correctly
|
||||
// InputStream is = connection.getInputStream();
|
||||
// int header = Algorithms.readTwoInt(is);
|
||||
// boolean isGzipFile = header == Algorithms.GZIP_FILE_SIGNATURE;
|
||||
// if (isGzipFile) {
|
||||
// return new GZIPInputStream(connection.getInputStream());
|
||||
// }
|
||||
return connection.getInputStream();
|
||||
ByteArrayInputStream localIS = Algorithms.createByteArrayIS(connection.getInputStream());
|
||||
if (Algorithms.checkFileSignature(localIS, ZIP_FILE_SIGNATURE)) {
|
||||
ZipInputStream zipIS = new ZipInputStream(localIS);
|
||||
zipIS.getNextEntry(); // set position to reading for the first item
|
||||
return zipIS;
|
||||
} else if (Algorithms.checkFileSignature(localIS, GZIP_FILE_SIGNATURE)) {
|
||||
return new GZIPInputStream(localIS);
|
||||
}
|
||||
return localIS;
|
||||
}
|
||||
|
||||
public void saveEngine(@NonNull OnlineRoutingEngine engine) {
|
||||
|
|
Loading…
Reference in a new issue