在Python中,我想浏览其中的所有子目录,并在检查其内容后仅选择性地提取7z文件。我不想提取所有文件,但我应该能够迭代/递归地查看内容。
主要问题是 .7z zip 的大小为 15 GB,但解压后大小为 225 GB。现在我的硬盘是160GB。在这 225 GB 中,我可能只需要有效的 60 GB 数据。只有当我可以浏览单个文件中的数据时,我才能搜索它。 .7z 文件上有 os.walk 类型的函数吗?
https://dumps.wikimedia.org/other/static_html_dumps/current/en/*.7z
是我正在探索的文件。
7z l *.7z
7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=C.UTF-8,Utf16=on,HugeFiles=on,64 bits,4 CPUs Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz (406E3),ASM,AES-NI)
Scanning the drive for archives:
1 file, 15363543213 bytes (15 GiB)
Listing archive: wikipedia-en-html.tar.7z
--
Path = wikipedia-en-html.tar.7z
Type = 7z
Physical Size = 15363543213
Headers Size = 100
Method = LZMA:22
Solid = -
Blocks = 1
Date Time Attr Size Compressed Name
------------------- ----- ------------ ------------ ------------------------
2008-06-18 23:32:15 ..... 223674511360 15363543113 wikipedia-en-html.tar
------------------- ----- ------------ ------------ ------------------------
2008-06-18 23:32:15 223674511360 15363543113 1 files
import lzma
f7file = r"C:\Users\padmaraj.bhat\OneDrive - Accenture\Downloads\wiki-html\wikipedia-en-html.tar.7z"
f = lzma.open(f7file, 'rb')
for line in f:
lzma.decompress(line)
break
<小时/>
Traceback (most recent call last)
<ipython-input-5-d1a496a0c194> in <module>()
4
5 f = lzma.open(f7file, 'rb')
----> 6 for line in f:
7 lzma.decompress(line)
8 break
~\AppData\Local\Continuum\anaconda3\lib\lzma.py in readline(self, size)
220 """
221 self._check_can_read()
--> 222 return self._buffer.readline(size)
223
224 def write(self, data):
~\AppData\Local\Continuum\anaconda3\lib\_compression.py in readinto(self, b)
66 def readinto(self, b):
67 with memoryview(b) as view, view.cast("B") as byte_view:
---> 68 data = self.read(len(byte_view))
69 byte_view[:len(data)] = data
70 return len(data)
~\AppData\Local\Continuum\anaconda3\lib\_compression.py in read(self, size)
101 else:
102 rawblock = b""
--> 103 data = self._decompressor.decompress(rawblock, size)
104 if data:
105 break
LZMAError: Input format not supported by decoder
最佳答案
当我必须做类似的事情时,我必须通过 subprocess()
调用 7z
CLI。通过这种方式,您可以确定文件中的文件列表以及文件内容。
例如,要将文件直接提取到 stdout,您可以使用 the -so
option .
关于python - 如何在 Python3.x 中检索单个 7zip 文件而不解压所有文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54142952/