How to check if InputStream is Gzipped?

后端 未结 10 1972
一个人的身影
一个人的身影 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:39

    This function works perfectly well in Java:

    public static boolean isGZipped(File f) {   
        val raf = new RandomAccessFile(file, "r")
        return GZIPInputStream.GZIP_MAGIC == (raf.read() & 0xff | ((raf.read() << 8) & 0xff00))
    }
    

    In scala:

    def isGZip(file:File): Boolean = {
       int gzip = 0
       RandomAccessFile raf = new RandomAccessFile(f, "r")
       gzip = raf.read() & 0xff | ((raf.read() << 8) & 0xff00)
       raf.close()
       return gzip == GZIPInputStream.GZIP_MAGIC
    }
    

提交回复
热议问题