【问题标题】:.rpm file size increased (getting corrupted) during upload in python.rpm 文件大小在 python 中上传期间增加(损坏)
【发布时间】:2014-03-21 07:55:57
【问题描述】:

我正在从我的 Django UI 上传一个 .rpm 文件。我能够在所需位置成功上传文件..

问题: 上传后文件大小增加,因此我在尝试提取 .rpm 文件时收到错误 --“error reading header from package” /p>

以下是我用来上传文件的函数:

// RPM_DIR = some DIR path where I am saveing the file"

def save_temporarily(file, name):

with open(os.path.join(RPM_DIR,str(name)),"wb+") as destination:
        for chunk in file.chunks():
            destination.write(chunk)
destination.closed
f.seek(0)
return os.path.join(RPM_DIR,str(name))

ls -ltr的输出

-rw-r--r-- 1 root root 3748319 Feb 20 new_file.rpm(用于新上传的文件)

-rw-r--r-- 1 root root 3735417 Feb 20 xyz.rpm(用于原始文件)

尺寸变大了……

请建议如何摆脱这个问题... 特别是如果可能的话,我正在寻找以下解决方案

  1. 我们能否介绍一下如何从文件中删除多余的字节并将其提取出来。
  2. 有没有办法在 python 中上传文件而无需打开并保存到指定位置。
  3. 为什么额外的字节会附加到文件中。

编辑
我还尝试将写入功能更改为

    output_file_path = "/u001/Test/"+ file.name  
    result_file = open(output_file_path,"wb")  
    while True:  
       file_content = file.read(1024)             ''' or simply  file.read() '''  
       if not file_content:  
          break  
       result_file.write(file_content)  
    result_file.write(file_content)  
    result_file.close()  

我得到了相同的输出没有变化...我实际上是在保存 .rpm 文件后运行以下命令(请参阅details):

rpm2cpio '+str(patch_path)+' | cpio -idm  

并得到以下错误:

<open file 'rpm2cpio /u001/Test/php-5.1.4-1.esp1.x86_64.rpm | cpio -idm ', mode 'r' at 0x7f6334239030>
error: rpm2cpio: headerRead failed: region trailer: BAD, tag 491913216 type 508690432 offset -525467648 count 542113792
error reading header from package
cpio: premature end of archive

PS:这可能有助于更多地了解正在发生的事情

谢谢,

【问题讨论】:

    标签: python django python-2.7 file-upload rpm


    【解决方案1】:

    您是否对两个文件进行了二进制差异(unix: cmp)以查看 new_file.rpm 损坏的位置?我想知道您的问题是否不是多余的字节而是损坏。

    Python 文件对象 write() 方法接受一个字符串,其处理可能因您的字符编码而异。 RPM 文件是二进制的。从您的代码示例中不清楚 file.chunks() 返回的对象类型。

    您可能需要执行此处建议的操作:Python how to write to a binary file?

    【讨论】:

    • 是的,您是正确的,文件已损坏。 file.chunk() 返回 string 类型的对象
    • 我编写了以下测试程序,并且副本运行良好:import os fileName = os.path.expanduser('~/Downloads/somefile.rpm') f = open(fileName) output_file_path = "/tmp/Test-"+ os.path.basename(f.name) result_file = open(output_file_path,"wb") while True: file_content = f.read(1024) if not file_content: break result_file.write(file_content) result_file.write(file_content) result_file.close() 我怀疑您的问题与您正在阅读的内容有关。
    • 我尝试更改我正在读取的文件,其中一些文件有效,而其中一些文件失败。我认为您是对的,问题出在我正在阅读的文件上。感谢您的努力,这真的很有帮助:)
    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2020-06-18
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2010-09-27
    相关资源
    最近更新 更多