diff --git a/lottie/src/main/java/com/airbnb/lottie/network/FileExtension.java b/lottie/src/main/java/com/airbnb/lottie/network/FileExtension.java index 2c3527088f..7a50562693 100644 --- a/lottie/src/main/java/com/airbnb/lottie/network/FileExtension.java +++ b/lottie/src/main/java/com/airbnb/lottie/network/FileExtension.java @@ -8,7 +8,8 @@ @RestrictTo(RestrictTo.Scope.LIBRARY) public enum FileExtension { JSON(".json"), - ZIP(".zip"); + ZIP(".zip"), + GZIP(".gz"); public final String extension; diff --git a/lottie/src/main/java/com/airbnb/lottie/network/NetworkCache.java b/lottie/src/main/java/com/airbnb/lottie/network/NetworkCache.java index d6a83ef0df..65e9a02d8d 100644 --- a/lottie/src/main/java/com/airbnb/lottie/network/NetworkCache.java +++ b/lottie/src/main/java/com/airbnb/lottie/network/NetworkCache.java @@ -76,6 +76,8 @@ Pair fetch(String url) { FileExtension extension; if (cachedFile.getAbsolutePath().endsWith(".zip")) { extension = FileExtension.ZIP; + } else if (cachedFile.getAbsolutePath().endsWith(".gz")) { + extension = FileExtension.GZIP; } else { extension = FileExtension.JSON; } @@ -143,6 +145,10 @@ private File getCachedFile(String url) throws FileNotFoundException { if (zipFile.exists()) { return zipFile; } + File gzipFile = new File(parentDir(), filenameForUrl(url, FileExtension.GZIP, false)); + if (gzipFile.exists()) { + return gzipFile; + } return null; } diff --git a/lottie/src/main/java/com/airbnb/lottie/network/NetworkFetcher.java b/lottie/src/main/java/com/airbnb/lottie/network/NetworkFetcher.java index 0d37823136..9fa2c7af38 100644 --- a/lottie/src/main/java/com/airbnb/lottie/network/NetworkFetcher.java +++ b/lottie/src/main/java/com/airbnb/lottie/network/NetworkFetcher.java @@ -17,6 +17,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.util.zip.GZIPInputStream; import java.util.zip.ZipInputStream; @RestrictTo(RestrictTo.Scope.LIBRARY) @@ -59,10 +60,19 @@ private LottieComposition fetchFromCache(Context context, @NonNull String url, @ FileExtension extension = cacheResult.first; InputStream inputStream = cacheResult.second; LottieResult result; - if (extension == FileExtension.ZIP) { - result = LottieCompositionFactory.fromZipStreamSync(context, new ZipInputStream(inputStream), cacheKey); - } else { - result = LottieCompositionFactory.fromJsonInputStreamSync(inputStream, cacheKey); + switch (extension) { + case ZIP: + result = LottieCompositionFactory.fromZipStreamSync(context, new ZipInputStream(inputStream), cacheKey); + break; + case GZIP: + try { + result = LottieCompositionFactory.fromJsonInputStreamSync(new GZIPInputStream(inputStream), cacheKey); + } catch (IOException e) { + result = new LottieResult<>(e); + } + break; + default: + result = LottieCompositionFactory.fromJsonInputStreamSync(inputStream, cacheKey); } if (result.getValue() != null) { return result.getValue(); @@ -117,6 +127,12 @@ private LottieResult fromInputStream(Context context, @NonNul Logger.debug("Handling zip response."); extension = FileExtension.ZIP; result = fromZipStream(context, url, inputStream, cacheKey); + } else if (contentType.contains("application/gzip") || + contentType.contains("application/x-gzip") || + url.split("\\?")[0].endsWith(".tgs")) { + Logger.debug("Handling gzip response."); + extension = FileExtension.GZIP; + result = fromGzipStream(url, inputStream, cacheKey); } else { Logger.debug("Received json response."); extension = FileExtension.JSON; @@ -140,6 +156,16 @@ private LottieResult fromZipStream(Context context, @NonNull return LottieCompositionFactory.fromZipStreamSync(context, new ZipInputStream(new FileInputStream(file)), url); } + @NonNull + private LottieResult fromGzipStream(@NonNull String url, @NonNull InputStream inputStream, @Nullable String cacheKey) + throws IOException { + if (cacheKey == null || networkCache == null) { + return LottieCompositionFactory.fromJsonInputStreamSync(new GZIPInputStream(inputStream), null); + } + File file = networkCache.writeTempCacheFile(url, inputStream, FileExtension.GZIP); + return LottieCompositionFactory.fromJsonInputStreamSync(new GZIPInputStream(new FileInputStream(file)), url); + } + @NonNull private LottieResult fromJsonStream(@NonNull String url, @NonNull InputStream inputStream, @Nullable String cacheKey) throws IOException {