代码之家  ›  专栏  ›  技术社区  ›  Erin

rsync之后使用PHP提取.tar.gz

  •  0
  • Erin  · 技术社区  · 7 年前

    rsync 然后用压缩剂将其压缩 tar :

    rsync -azp --files-from=${RSYNC_UPDATE_FILE} --ignore-missing-args src update
    tar czf ${UFILE} update
    

    # 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
    

    0 回复  |  直到 7 年前
        1
  •  1
  •   Erin    7 年前

    知道了。该错误是由于未提及的进程造成的。这个 UploadedFile application/x-gzip ,扩展名为空,因此生成的文件另存为 [hashed_file_name]. 而不是 [hashed_file_name].tar.gz GuzzleHttp Symfony 猜猜分机。

    $extension = ExtensionGuesser::getInstance()->guess($contentType);
    

    由于mimetype,使用 Content-Type .gz 而不是 .tar.gz .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);