You need to enable JavaScript to run this app. Python云函数下gzip压缩CSS出现内容截断 终端执行同命令正常
优惠活动
大模型
产品
解决方案
定价
更多

Python云函数下gzip压缩CSS出现内容截断 终端执行同命令正常

Python云函数场景下gzip压缩/tmp路径CSS文件出现内容截断问题

核心现象

  • 需实现的功能:将云函数运行环境/tmp目录下的CSS文件通过gzip算法压缩为.gz格式文件
  • macOS测试环境下对照验证结果存在明显差异:
    • 终端直接手动执行gzip压缩、解压全流程,文件内容无丢失、无损坏
    • 无论使用Python标准库gzip模块实现压缩逻辑,还是通过os.system调用系统gzip命令执行压缩,生成的压缩包解压后均出现文件截断、末尾代码内容缺失的问题
  • 大小对照:手动执行命令得到的压缩包,解压后目标CSS文件为117145字节、内容完整;Python代码生成的压缩包,解压后仅为114688字节,存在明确的内容缺失

已尝试的两种异常实现代码

基于Python标准库gzip模块的实现

def gzip_blob(self,blob_name):
    path = "/tmp"
    path_file=path+"/"+blob_name
    path_gzip_file=path+"/"+blob_name+".gz"
    with open(path_file, 'rb') as f_in, gzip.open(path_gzip_file, 'wb',compresslevel=self.compresslevel) as f_out:
        f_out.writelines(f_in)
    os.remove(path_file) 
    os.rename(path_gzip_file, path_file)

基于os.system调用系统命令的实现

def gzip_file(self,blob_name):
    path = "/tmp"
    path_file=path+"/"+blob_name
    print("Zip file:"+path_file)
    # path_gzip_file=path+"/"+blob_name+".gz"
    cmd = "gzip -c /tmp/sphere.css > /tmp/sphere-css.gz"
    os.system(cmd) 
    print("gzip done")

手动验证对照过程

在终端手动执行与Python代码中完全一致的gzip命令流程,结果无异常:

  • 执行压缩:gzip -c /tmp/sphere.css > /tmp/sphere-css.gz
  • 执行解压:gzip -d /tmp/sphere-css.gz
  • 解压后文件无信息丢失,目标CSS文件大小为117145字节,内容完整

内容的提问来源于stack exchange,提问作者brunet julien

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.30 04:24:03