How to check if InputStream is Gzipped?

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

    This is how to read a file that CAN BE gzipped:

    private void read(final File file)
            throws IOException {
        InputStream stream = null;
        try (final InputStream inputStream = new FileInputStream(file);
                final BufferedInputStream bInputStream = new BufferedInputStream(inputStream);) {
            bInputStream.mark(1024);
            try {
                stream = new GZIPInputStream(bInputStream);
            } catch (final ZipException e) {
                // not gzipped OR not supported zip format
                bInputStream.reset();
                stream = bInputStream;
            }
            // USE STREAM HERE
        } finally {
            if (stream != null) {
                stream.close();
            }
        }
    }
    

提交回复
热议问题