streamDownload(static function () use ($contracts, $manifest): void { $zip = new ZipStream(defaultCompressionMethod: CompressionMethod::STORE, sendHttpHeaders: false); $disk = Storage::disk('uploads'); try { fwrite($manifest, "\xEF\xBB\xBF"); fputcsv($manifest, ['供应商', '期限状态', '合同编号', '开始日期', '结束日期', '距到期天数', '甲方签署主体', '乙方签署主体', '签署单号', '签署原件'], ',', '"', ''); foreach ($contracts->supplierExportRows() as $row) { $path = $row['signed_path']; $attachment = '归档附件缺失'; if (filled($path) && $disk->exists($path)) { $pdf = $disk->readStream($path); if (is_resource($pdf)) { $attachment = '签署原件/合同-' . $row['id'] . '.pdf'; try { $zip->addFileFromStream($attachment, $pdf); } finally { fclose($pdf); } } else { $attachment = '归档附件读取失败'; } } $values = array_map(static function ($value): string|int { if (is_int($value)) { return $value; } $value = (string) $value; return preg_match('/^[\s\x00-\x1f]*[=+@-]|^[\t\r\n]/u', $value) ? "'" . $value : $value; }, [ $row['courier_name'], CourierContractPageData::STATUSES[$row['status']], $row['number'], $row['start_date'], $row['end_date'], $row['days'], $row['party_a'], $row['party_b'], $row['business_no'], $attachment, ]); fputcsv($manifest, $values, ',', '"', ''); } rewind($manifest); $zip->addFileFromStream('供应商合同清单.csv', $manifest); $zip->finish(); } finally { fclose($manifest); } }, '供应商合作合同-' . now()->format('Ymd-His') . '.zip', [ 'Content-Type' => 'application/zip', 'Cache-Control' => 'private, no-store', ]); } }