1

当我运行以下命令时,我希望 infile 和 outfile 相同,但它们不在我测试过的文件中。大小略有不同,tar tvf 显示我在 dirs 上获得了尾随 /

我在 Windows 和 python 2.7.9 上运行

我究竟做错了什么?

import tarfile
import io

fn = 'infile.tar'
outf = 'outfile.tar'

with open(fn, 'rb') as f:
    data = f.read()  # read the rest

tf = tarfile.open(fileobj=io.BytesIO(data))

iofile = io.BytesIO()
out = tarfile.open(fileobj=iofile, mode='w')

for item in tf.getmembers():
    if item.isfile():
        f = tf.extractfile(item)
        contents = f.read()
        out.addfile(item, io.BytesIO(contents))
        print('{}: {}'.format(item.name, len(contents)))
    elif item.isdir():
        t = item
        out.addfile(t)
        print('{}: **isdir**'.format(item.name))
    else:
        print('{}: **unknown**'.format(item.name))
out.close()

contents = iofile.getvalue()
with open(outf, 'wb') as f:
    f.write(contents)

编辑:部分示例:原始

-rw------- 1000/1000      1214 1969-12-31 19:00 apps/com.antutu.ABenchMark/_manifest
-rw-r--r-- 1000/1000  37044320 2015-03-12 23:40 apps/com.antutu.ABenchMark/a/base.apk
drwxrwx--x 10152/10152       0 2015-01-11 10:15 apps/com.antutu.ABenchMark/r/app_webview
drwx------ 10152/10152       0 2015-01-11 10:15 apps/com.antutu.ABenchMark/r/app_webview/Cache
-rw------- 10152/10152     844 2015-01-11 10:15 apps/com.antutu.ABenchMark/r/app_webview/Cache/213163dd3446f027_0

改变:

-rw------- 1000/1000      1214 1969-12-31 19:00 apps/com.antutu.ABenchMark/_manifest
-rw-r--r-- 1000/1000  37044320 2015-03-12 23:40 apps/com.antutu.ABenchMark/a/base.apk
drwxrwx--x 10152/10152       0 2015-01-11 10:15 apps/com.antutu.ABenchMark/r/app_webview/
drwx------ 10152/10152       0 2015-01-11 10:15 apps/com.antutu.ABenchMark/r/app_webview/Cache/
-rw------- 10152/10152     844 2015-01-11 10:15 apps/com.antutu.ABenchMark/r/app_webview/Cache/213163dd3446f027_0

我一直在测试 android 备份文件,这些文件太大而无法发布。

4

0 回答 0