- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 tarfile 在内存中添加一个文件,然后将其写回磁盘,但我遇到的问题是在我的最终输出中,当我提取新创建的 tar.gz 文件时,我得到一个空文件。我的代码哪里做错了?
import tarfile
import io
with open('logo.png', 'rb') as f:
data = f.read()
fh = io.BytesIO()
with tarfile.open(fileobj=fh, mode='w:gz') as tar:
info = tarfile.TarInfo('some.png')
tar.addfile(info, data)
with open('/tmp/test/test.tar.gz', 'wb') as f:
f.write(fh.getvalue())
我也尝试过执行 tar.addfile(info, fh.write(data))
,但这只会创建一个损坏的 tar 文件。
最佳答案
TarFile.addfile()
接受一个类似文件的对象。
当文档说:
tarinfo.size bytes are read from it and added to the archive.
意思是tarinfo.size
用来决定读取多少字节。因此,您需要适本地设置tarinfo.size
。
您唯一需要做的就是从源读取数据,计算长度,然后将该数据加载到 BytesIO 对象中:
例如
import tarfile
import io
with open('logo.png', 'rb') as f:
data = f.read()
source_f = io.BytesIO(initial_bytes=data)
fh = io.BytesIO()
with tarfile.open(fileobj=fh, mode='w:gz') as tar:
info = tarfile.TarInfo('logo.png')
info.size = len(data)
tar.addfile(info, source_f)
with open('test.tar.gz', 'wb') as f:
f.write(fh.getvalue())
或者更高效的内存方式,寻找源文件:
f = open('logo.png', 'rb')
f.seek(0,2) # go to the end
source_len = f.tell()
f.seek(0)
fh = io.BytesIO()
with tarfile.open(fileobj=fh, mode='w:gz') as tar:
info = tarfile.TarInfo('logo.png')
info.size = source_len
tar.addfile(info, f)
with open('test.tar.gz', 'wb') as f:
f.write(fh.getvalue())
f.close()
关于Python tarfile 压缩内存中的一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59272304/
我制作了一个如下所示的小脚本来读取文件组并将它们压缩,一切正常,接受压缩文件在解压缩时包含文件的完整路径。有没有没有目录结构的方法? compressor = tarfile.open(PATH_TO
我正在尝试使用 tarfile 在内存中添加一个文件,然后将其写回磁盘,但我遇到的问题是在我的最终输出中,当我提取新创建的 tar.gz 文件时,我得到一个空文件。我的代码哪里做错了? import
我想从压缩包中读取一些文件并将其保存到新的压缩包中。这是我写的代码。 archive = 'dum/2164/archive.tar' # Read input data. input_tar = t
我可以用这种方式计算 tarfile 中文件的大小: import tarfile tf = tarfile.open(name='my.tgz', mode='r') reduce(lambda x
我有 .tar.bz2 文件,里面有很多小的 json 文件。一个存档可能有大约数千个,而且 json 很小(低于 10kB,通常也低于 1 KB)。因此,压缩后的单个存档不会超过 100kB。 根据
我有以下文件: # ls -lha total 2.4M drwxr-xr-x. 2 root root 4.0K Nov 26 19:47 . drwxrwxr-x. 5 bshus
我想创建一个 tar 文件并将其通过管道传输到 http 上传。 但是,似乎 python tarfile 模块执行搜索,这使得无法通过管道传输到下一个进程。 这是代码 tar = tarfile.o
在将文件添加到 python 中的 tar 存档时,是否有任何库可以显示进度,或者可以扩展 tarfile 模块的功能来执行此操作? 在理想情况下,我想展示 tar 创建的总体进度以及关于何时完成的预
如何在 python 中将空目录添加到 tarfile,而不先在磁盘上创建它? 在我的本地文件系统中创建一个空目录,并将其添加到 tar 文件中很容易,但会产生不必要的开销。 直接在 tar 文件中创
这是 Python 文档的摘录: If exclude is given it must be a function that takes one filename argument and retu
Create a zip file from a generator in Python?描述了将一堆文件写入 .zip 到磁盘的解决方案。 我在相反的方向也有类似的问题。我得到了一个发电机: str
我正在使用以下代码提取一个 tar 文件: import tarfile tar = tarfile.open("sample.tar.gz") tar.extractall() tar.close(
我尝试使用 with 语句和 tarfile 模块... with tarfile.open('/dir/dir/dir.tar.gz', 'w:gz') as fl: fl.add('/di
我有一个包含 bz2 压缩文件的 tarfile。我想将函数 clean_file 应用于每个 bz2 文件,并整理结果。在系列中,这很容易用一个循环: import pandas as pd imp
我正在尝试将文件添加到 python 中的 gzipped tarfile import tarfile # create test file with open("testfile.txt", "w
使用 tarfile.add 添加目录时,是否可以访问 filter lambda 中的各个文件名? 我正在使用 tarfile 模块来创建项目目录的存档。其中一些文件我不再需要,我想忽略: mypr
我正在尝试将 tar.gz 文件中的所有内容提取到同一目录中。以下代码可以提取所有文件,但文件存储在工作目录中,而不是我输入的名称路径中。 import tarfile zip_rw_data = r
是否有办法防止 tarfile.extractall ( API ) 覆盖现有文件?我所说的“防止”是指理想情况下在即将发生覆盖时引发异常。当前的行为是静默覆盖文件。 最佳答案 您可以查看 tar
我正在尝试使用 tarfile 库归档和压缩分布在多个驱动器上的多个目录。问题是即使两个文件存储在不同的驱动器中,tarfile 也会合并路径。例如: import tarfile with tarf
我的问题是对这个 one 的跟进.我想知道如何修改以下代码以便分配压缩级别: import os import tarfile home = '//global//scratch//chamar//p
我是一名优秀的程序员,十分优秀!