Ticksy
Ticksy

Reputation: 561

How to check if encrypted Zip archive in PHP?

As I understand it, the status is archive encrypted contains in general purpose bit flag. I try to check this with ZipArchive::statname(), but it seems that can not get the information by this method.

What else I can do? Read the archive and parse the headers? I know that I can call system(), but I do not want to use this method because of its specificity (some hosting this function is disabled).

Upvotes: 0

Views: 1832

Answers (3)

Odon Garma
Odon Garma

Reputation: 1

That's not working, because the Header can repeat n-times.

To workaround this, open the zip zip_open() and try to read every entry zip_entry_open()

If there is an unreadable entry, the zip could be encrypted!

Upvotes: 0

Paul Annekov
Paul Annekov

Reputation: 3263

Here is the ZIP standard: http://www.pkware.com/documents/casestudies/APPNOTE.TXT.

From section 4.3.7:

4.3.7  Local file header:

  local file header signature     4 bytes  (0x04034b50)
  version needed to extract       2 bytes
  general purpose bit flag        2 bytes
  compression method              2 bytes
  ...

From section 4.4.4:

4.4.4 general purpose bit flag: (2 bytes)

    Bit 0: If set, indicates that the file is encrypted.
    ...

So you need to check FIRST BIT of the seventh byte and not the whole byte. You must do the checking for each file, as each file can be encrypted individually (section 4.3.6).

Upvotes: 3

Peter
Peter

Reputation: 16923

ZIP file header: (encrypted file vs normal file)

enter image description here

09 seemts to be the encryption flag.

Check 7th byte is 0x09

function zip_is_encrypted($filename) {
  $handle = fopen($filename, "rb");
  $contents = fread($handle, 7);
  fclose($handle);
  return $contents[6] == 0x09;
}

Upvotes: 5

Related Questions