Search code examples
javazipinputstream

Best way to detect if a stream is zipped in Java


What is the best way to find out i java.io.InputStream contains zipped data?


Solution

  • The magic bytes for the ZIP format are 50 4B. You could test the stream (using mark and reset - you may need to buffer) but I wouldn't expect this to be a 100% reliable approach. There would be no way to distinguish it from a US-ASCII encoded text file that began with the letters PK.

    The best way would be to provide metadata on the content format prior to opening the stream and then treat it appropriately.