简体   繁体   English

.rpm文件大小在python中上传时增加了(损坏了)

[英].rpm file size increased (getting corrupted) during upload in python

I am uploading a .rpm file from my Django UI. 我正在从Django UI上传一个.rpm文件。 I am able to upload the file successfully at the desired location.. 我可以在所需位置成功上传文件。

Problem: There is an increase in file size after uploading, and due to this I am getting error -- "error reading header from package" when trying to extract that .rpm file 问题:上传后文件大小增加,由于这个原因,我在尝试提取该.rpm文件时遇到错误-“从包中读取标题错误”

Following is the function that I have used to upload the file: 以下是我用来上传文件的功能:

// 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))

Output of ls -ltr ls -ltr输出

-rw-r--r-- 1 root root 3748319 Feb 20 new_file.rpm (for the newly uploaded file) -rw-r--r-- 1个根目录3748319 2月20日new_file.rpm(用于新上传的文件)

-rw-r--r-- 1 root root 3735417 Feb 20 xyz.rpm (for the original file) -rw-r--r-- 1个根目录3735417 2月20日xyz.rpm(对于原始文件)

There is increase in size... 尺寸增加了...

Please suggest how to get rid of this problem... In particular I am looking for the following solutions if possible 请提出如何解决此问题的建议...特别是,如果可能,我正在寻找以下解决方案

  1. Can we some how remove that extra bytes from the file and extract it. 我们能否通过一些方法从文件中删除多余的字节并提取出来。
  2. Is there a way to upload a file in python without opening and saving it to a specified location. 有没有一种方法可以在python中上传文件而无需打开并将其保存到指定位置。
  3. Why that extra bytes are getting appended to the file. 为什么多余的字节被追加到文件中。

EDIT 编辑
I also tried changing the write function to 我也尝试将write函数更改为

    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()  

I got the same output no change... I am actually running following command after saving the .rpm file ( see for details ) : 我得到的输出相同,没有变化...保存.rpm文件后,我实际上正在运行以下命令( 有关详细信息,请参见 ):

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

and got the following error: 并得到以下错误:

<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 : This may help to understand a bit more what is happening PS:这可能有助于更多地了解正在发生的事情

Thanks, 谢谢,

Have you done a binary difference (unix: cmp) of the two files to see where the new_file.rpm is corrupted? 您是否对两个文件做了二进制差异(unix:cmp),以查看new_file.rpm损坏的地方? I wonder if your problem is not excess bytes but corruption. 我想知道您的问题是否不是多余的字节而是损坏。

The Python file object write() method takes a string, for which processing could vary depending on your character encoding. Python文件对象的write()方法采用一个字符串,对该字符串的处理可能会根据您的字符编码而有所不同。 RPM files are binary. RPM文件是二进制的。 It is not clear from your code example what type of object file.chunks() returns. 从您的代码示例中尚不清楚对象文件.chunks()返回什么类型。

It may be you need to do something like suggested here: Python how to write to a binary file? 可能您需要执行此处建议的操作: Python如何写入二进制文件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM