简体   繁体   中英

How to add empty string to the end of tgz file in Python

I am running below command to add empty string to the tgz file so that I can have new sha256 sum;

cat orginal.tgz  <(echo ''| gzip)> modified.tgz

$sha256sum *.tgz
 3b49d79af670d0aea9d1e0ead2f512ad107d54b657df6ecb60a1b13ef796cb66  modified.tgz
 574ddb9b269bc2e69fffff81c2f29d932d3fd95e4c9ca1ed259517ac83b1421a  orginal.tgz

I can untar the modified.tgz file without any issue; Now I wrote this simple python program to do the same.

with open("new.tgz", "w") as outfile:
   with open("orginal.tgz", "r") as infile:
       buff = infile.read()
       outfile.write(buff)
       outfile.write(' ')

I can see different sha256sum for new.tgz file but it is failing to untar with below error

 $ sha256sum *.tgz
 3b49d79af670d0aea9d1e0ead2f512ad107d54b657df6ecb60a1b13ef796cb66  modified.tgz
 574ddb9b269bc2e69fffff81c2f29d932d3fd95e4c9ca1ed259517ac83b1421a  orginal.tgz
 0458bb8342faf0482739069a1a70aef89d32fb9336f2f7926dfe6d54a0dc6cc1  new.tgz



 $ tar -xvf new.tgz
 google.txt
 gzip: stdin: unexpected end of file
 tar: Child returned status 1
 tar: Error is not recoverable: exiting now

What you are doing in the linux command is adding a file with an empty string to the tgz. What you are doing in the python is modifying the tgz file itself.

You cannot update the tgz directly. You need to add a file to it. Use a library like tar, and create a file with empty string and add it to it:

archive = tarfile.open("original.tgz", "w|gz")
archive.add(empty_file_path, arcname=empty_file_path)
archive.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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