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")));
}
}
Se ve interesante
ReplyDelete