Python:那就解开吧

2024-09-30 03:25:26 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行以下命令时,我希望infile和outfile是相同的,但是它们不在我测试的文件中。尺寸稍有不同,tar-tvf显示我在dirs上获得了一个尾随/尾随

我运行的是windows和python2.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备份文件,这些文件太大,无法发布。你知道吗


Tags: appsiocomappcachecontentstaropen

热门问题