php - rsync 后用 PHP 提取 .tar.gz
问题描述
我正在尝试提取我在管道中使用 bash 压缩的 .tar.gz。管道选择应使用更新打包的文件,rsync
然后使用以下命令压缩它们tar
:
rsync -azp --files-from=${RSYNC_UPDATE_FILE} --ignore-missing-args src update
tar czf ${UFILE} update
当我使用 WinRar 之类的程序打开 .tar.gz 时,这些文件看起来是正确的。然后我在应用程序中使用 PHP 提取更新。
# Get the full path where it should be extracted
$dirpath = $dirpath ?: File::dirname($zippath);
$phar = new \PharData($zippath);
# Check if it's compressed: e.g. tar.gz => tar
$zip = $phar->isCompressed() ? $phar->decompress() : $phar;
try {
# Extract it to the new dir
$extracted = $zip->extractTo($dirpath);
} catch (\Exception $e ) {
throw new CorruptedZip("Unable to open the archive.",424,$e);
}
提取的文件具有正确的权限、目录结构等,但我猜它们仍然是压缩的。这些文件都包含许多字符串组,而不是 PHP 代码。
02a0 048b 2235 bca8 ad5e 4f7e d9be ed1f
5b00 24d5 9248 8994 2c75 f778 e293 74db
6401 a802 0af5 55e1 52fc fb37 80ff f99f
任何人都可以看到我错过了一步吗?
解决方案
知道了。该错误是由于未提及的过程造成的。UploadedFile
Laravel 中的类将文件的 mimetype 解释为application/x-gzip
,扩展名为空,因此生成的文件保存为[hashed_file_name].
而不是[hashed_file_name].tar.gz
. 然后(在另一台服务器上)我GuzzleHttp
用来获取文件并Symfony
猜测扩展名。
$extension = ExtensionGuesser::getInstance()->guess($contentType);
由于 mimetype,使用Content-Type
header 来获取扩展名的重建文件只是.gz
代替.tar.gz
or .tgz
。对我的上传脚本的更改修复了它。
$alias = $file->getClientOriginalName();
$mimetype = $file->getMimeType();
$extension = $file->guessClientExtension() ?: pathinfo($alias, PATHINFO_EXTENSION);
if ( ends_with($mimetype, 'x-gzip') && ends_with($alias, ['tar.gz', 'tgz']) ) {
$mimetype = 'application/tar+gzip';
$extension = 'tar.gz';
}
$hash = $file->hashName();
if ( ends_with($hash, '.') ) {
$hash .= $extension;
}
$path = $file->storeAs($storage, $hash);
推荐阅读
- spring - 如果处理程序中发生异常,则无法将消息路由到标头中定义的错误通道
- python - 如何修复“字典对象没有属性键”
- django - 如果在 Django 中间件中检测到未知 GET,如何阻止黑客访问
- javascript - 来自父类对象的javascript构造函数
- visual-studio - 意外关闭文档窗口 Microsoft Visual Studio 2017
- javascript - 何时使用 res.locals 发送响应?
- python-3.x - 如何可视化pdf文件模式,因为我想解析它?
- vue.js - 从热模块更换中排除软件包
- go - 将结构接口转换为相同的结构
- javascript - 为什么 mocha 中事务的测试结果是错误的,但在 REST 测试中可以正常工作?