简体   繁体   中英

Transmitting zip data as part of a webservice response / reversible mb_detect_encoding

I have a PHP webservice which currently returns a zip archive as its only output. I'm reading the zip archive from disk using file_get_contents and sending it back as the body of the response.

I'd like it to return some additional metadata, in a JSON format:

{
    "generatedDate": "2012-11-28 12:00:00",
    "status": "unchanged",
    "rawData": <zip file in raw form>
}

The iOS app which talks to this service will receive this response, parse the JSON, and then store the zip file locally for its own use.

However, if I try to stuff the result of file_get_contents into json_encode, it rightfully complains that the string is not in UTF-8 format. If I UTF-8-encode it using mb_convert_encoding($rawData, 'UTF-8', mb_detect_encoding($rawData, 'UTF-8, ISO-8859-1', true)); , it will encode it happily, but I can't find a way to reverse the operation on the client (calling [dataString dataUsingEncoding:NSUTF8StringEncoding] and then treating the result as a zip file fails with BOM could not extract archive: Couldn't read pkzip local header .

Can anyone suggest a good way to insert a blob of raw data as one field in a JSON response?

Surely if you successfully included the raw data in the JSON then you'd have the opposite problem at the other end, when you try to decode the JSON and whatever you use to decode can't handle the raw data?

Instead, I would suggest that you send the raw data only in the response body, and use headers to send the metadata.

Strike this question.

It turns out that UTF-8 encoding raw data like this is nonstandard at best, and the standard solution is base-64 encoding it and then using a base-64 decoder to recover it on the client:

$this->response(200, array('rawData' => base64_encode($rawData)));

...

NSString *rawDataString = [[response responseJSON] objectForKey:@"rawData"];
NSData *rawData = [Base64 decode:rawDataString];

ZIP archives are not text —they are binary files! Trying to convert your archive from ISO-8859-1 to UTF-8 makes as much sense as trying to rotate it.

There're several algorithms to serialize binary streams as text but they'll all increase the file size. If that's not an issue, have a look at:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM