PHP 解压字符串

Ahm*_*man 1 php unzip google-drive-api

我正在通过 API 将 Google Drive 电子表格加载到 PHP 中。该请求返回XLSX电子表格,我需要将其解压缩。为了节省我将响应写入临时文件然后调用,例如,zip_open()有没有一种方法可以将字符串传递给此类方法?

Flo*_*ent 5

我认为最好的选择是创建一个临时文件,然后将其解压缩。

// Create a temporary file which creates file with unique file name
$tmp = tempnam(sys_get_temp_dir(), md5(uniqid(microtime(true))));

// Write the zipped content inside
file_put_contents($tmp, $zippedContent);

// Uncompress and read the ZIP archive
$zip = new ZipArchive;
if (true === $zip->open($tmp)) {
    // Do whatever you want with the archive... 
    // such as $zip->extractTo($dir); $zip->close();
}

// Delete the temporary file
unlink($tmp);
Run Code Online (Sandbox Code Playgroud)