How to check if InputStream is Gzipped?

后端 未结 10 1966
一个人的身影
一个人的身影 2020-12-04 19:06

Is there any way to check if InputStream has been gzipped? Here\'s the code:

public static InputStream decompressStream(InputStream input) {
    try {
               


        
10条回答
  •  旧巷少年郎
    2020-12-04 19:51

    Not exactly what you are asking but could be an alternative approach if you are using HttpClient:

    private static InputStream getInputStream(HttpEntity entity) throws IOException {
      Header encoding = entity.getContentEncoding(); 
      if (encoding != null) {
         if (encoding.getValue().equals("gzip") || encoding.getValue().equals("zip") ||      encoding.getValue().equals("application/x-gzip-compressed")) {
            return new GZIPInputStream(entity.getContent());
         }
      }
      return entity.getContent();
    }
    

提交回复
热议问题