'Is FL_ENC_RAW a valid flag for ZipArchive::addFromString?

I have a "string" variable that contains binary jpeg data. I want to add this jpeg to a zip file. According to the documentation the $flags parameter is a

Bitmask consisting of ZipArchive::FL_OVERWRITE, ZipArchive::FL_ENC_GUESS, ZipArchive::FL_ENC_UTF_8, ZipArchive::FL_ENC_CP437.

ZipArchive::FL_ENC_GUESS would probably work, but it sounds like that is going to try to heuristically guess encoding based on the contents of my variable. If I want to be explicit, should I set $flags to ZipArchive::FL_ENC_UTF_8 or ZipArchive::FL_ENC_CP437? Between the 2, ZipArchive::FL_ENC_UTF_8 seems like the better choice.

ZipArchive::FL_ENC_RAW seems like what I should really be using, but according to the docs this is not a valid flag for this particular function. Is this an omission? As best I can tell it's not listed as a valid flag for any function.



Solution 1:[1]

ZipArchive::addFromString is calling zip_file_add under the hood. If we take a look at the documentation for that function in libzip we find more descriptive explanations for what each of the flags is doing:

  • ZIP_FL_ENC_UTF_8: Interpret name as UTF-8.
  • ZIP_FL_ENC_CP437: Interpret name as code page 437 (CP-437).

ZipArchive::FL_ENC_UTF_8 and ZipArchive::FL_ENC_CP437 affect how the filename (aka the $name parameter) is interpreted, not the data inside the file itself. The contents of the file are processed the same regardless of if the data is UTF-8, CP-437, binary data, or anything else.

This is confirmed in the php tests for this library where these 4 lines

$zip->addEmptyDir(chr(0x82), ZipArchive::FL_ENC_CP437);
$zip->addEmptyDir('è', ZipArchive::FL_ENC_UTF_8);
$zip->addFromString(chr(0x91), __FILE__, ZipArchive::FL_ENC_CP437);
$zip->addFromString('€', __FILE__, ZipArchive::FL_ENC_UTF_8);

are expected to produce files with the following names

  • é (corresponding to page 437 character hex:82 dec:130)
  • è
  • æ (corresponding to page 437 character hex:91 dec:145)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 9072997