Question

I am uploading a .rpm file from my Django UI. 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

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

-rw-r--r-- 1 root root 3748319 Feb 20 new_file.rpm (for the newly uploaded file)

-rw-r--r-- 1 root root 3735417 Feb 20 xyz.rpm (for the original file)

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.
  3. Why that extra bytes are getting appended to the file.

EDIT
I also tried changing the write function to

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

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

Thanks,

Was it helpful?

Solution

Have you done a binary difference (unix: cmp) of the two files to see where the new_file.rpm is corrupted? 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. RPM files are binary. It is not clear from your code example what type of object file.chunks() returns.

It may be you need to do something like suggested here: Python how to write to a binary file?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top