How can I read the first two bytes of a .zip file to confirm that the appropriate magic number is present?

StackOverflow https://stackoverflow.com/questions/20344269

質問

I'm trying to read in a .zip file that holds a compressed .txt file. How can I check for the magic number 0x00BC? Thanks.

Edit: Sorry, should have specified that I'm trying to do this in java.

役に立ちましたか?

解決

First two bytes from the ZIP file:

InputStream is = new FileInputStream(FILE NAME);
byte b1 = (byte)is.read();
byte b2 = (byte)is.read();
is.close();

First two bytes from the first compressed file:

InputStream is = new FileInputStream(FILE NAME);
ZipInputStream zis = new ZipInputStream(is);
zis.getNextEntry();
byte[] bytes = new byte[2];
zis.read(bytes, 0, 2);
zis.close();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top