Search This Blog

Java: detect if a stream or file is a zip archive

The first four bytes of a valid ZIP file should be {'P', 'K', 0x3, 0x4}, a.k.a. the magic bytes for ZIP. The best way to detect if a file is ZIP archive is to check the magic bytes.


The following java code includes two methods to detect if a file or a inputStream is ZIP archive:
package example;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;

public class ZipUtil {

 public static byte[] MAGIC = { 'P', 'K', 0x3, 0x4 };

 /**
  * The method to test if a input stream is a zip archive.
  * 
  * @param in
  *            the input stream to test.
  * @return
  */
 public static boolean isZipStream(InputStream in) {
  if (!in.markSupported()) {
   in = new BufferedInputStream(in);
  }
  boolean isZip = true;
  try {
   in.mark(MAGIC.length);
   for (int i = 0; i < MAGIC.length; i++) {
    if (MAGIC[i] != (byte) in.read()) {
     isZip = false;
     break;
    }
   }
   in.reset();
  } catch (IOException e) {
   isZip = false;
  }
  return isZip;
 }

 /**
  * Test if a file is a zip file.
  * 
  * @param f
  *            the file to test.
  * @return
  */
 public static boolean isZipFile(File f) {

  boolean isZip = true;
  byte[] buffer = new byte[MAGIC.length];
  try {
   RandomAccessFile raf = new RandomAccessFile(f, "r");
   raf.readFully(buffer);
   for (int i = 0; i < MAGIC.length; i++) {
    if (buffer[i] != MAGIC[i]) {
     isZip = false;
     break;
    }
   }
   raf.close();
  } catch (Throwable e) {
   isZip = false;
  }
  return isZip;
 }

 public static void main(String[] args) throws FileNotFoundException {

  
  // test if a input stream is a zip stream.
  System.out.println(isZipStream(new FileInputStream(new File("/tmp/1.zip"))));

  // test if a file is zip file.
  System.out.println(isZipFile(new File("/tmp/1.zip")));
 }
}

See also:

1 comment: