- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在努力将我的备份脚本从 shell 转换为 Python。我的旧脚本的功能之一是通过执行以下操作检查创建的 tarfile 的完整性:gzip -t。
这在 Python 中似乎有点棘手。
似乎唯一的方法就是读取 tar 文件中的每个压缩 TarInfo 对象。
有没有一种方法可以检查 tarfile 的完整性,而无需将其提取到磁盘或将其保存在内存中(完整)?
freenode 上 #python 上的好人建议我应该逐 block 读取每个 TarInfo 对象,丢弃读取的每个 block 。
我必须承认我不知道如何做到这一点,因为我刚刚开始使用 Python。
假设我有一个 30GB 的 tarfile,其中包含 1kb 到 10GB 的文件...
这是我开始编写的解决方案:
try:
tardude = tarfile.open("zero.tar.gz")
except:
print "There was an error opening tarfile. The file might be corrupt or missing."
for member_info in tardude.getmembers():
try:
check = tardude.extractfile(member_info.name)
except:
print "File: %r is corrupt." % member_info.name
tardude.close()
这段代码远未完成。我不敢在一个巨大的 30GB 的 tar 存档上运行它,因为在某一时刻,check 将是一个 10+GB 的对象(如果我在 tar 存档中有这么大的文件)
奖励:我尝试手动破坏 zero.tar.gz(十六进制编辑器 - 编辑几个字节的中间文件)。第一个 except 没有捕捉到 IOError...这是输出:
Traceback (most recent call last):
File "./test.py", line 31, in <module>
for member_info in tardude.getmembers():
File "/usr/lib/python2.7/tarfile.py", line 1805, in getmembers
self._load() # all members, we first have to
File "/usr/lib/python2.7/tarfile.py", line 2380, in _load
tarinfo = self.next()
File "/usr/lib/python2.7/tarfile.py", line 2315, in next
self.fileobj.seek(self.offset)
File "/usr/lib/python2.7/gzip.py", line 429, in seek
self.read(1024)
File "/usr/lib/python2.7/gzip.py", line 256, in read
self._read(readsize)
File "/usr/lib/python2.7/gzip.py", line 320, in _read
self._read_eof()
File "/usr/lib/python2.7/gzip.py", line 342, in _read_eof
hex(self.crc)))
IOError: CRC check failed 0xe5384b87 != 0xdfe91e1L
最佳答案
只是对 Aya's 的一个小改进answer 使事情变得更加地道(尽管我删除了一些错误检查以使机制更加可见):
BLOCK_SIZE = 1024
with tarfile.open("zero.tar.gz") as tardude:
for member in tardude.getmembers():
with tardude.extractfile(member.name) as target:
for chunk in iter(lambda: target.read(BLOCK_SIZE), b''):
pass
这实际上只是删除了 while 1:
(有时被认为是轻微的代码味道)和 if not data:
检查。另请注意,使用 with
将其限制为 Python 2.7+
关于python - 在 Python 中检查 tarfile 完整性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16013291/
我制作了一个如下所示的小脚本来读取文件组并将它们压缩,一切正常,接受压缩文件在解压缩时包含文件的完整路径。有没有没有目录结构的方法? 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
我是一名优秀的程序员,十分优秀!