Извлечь текст из архива
Сохраняет текстовые файлы из архива отдельно или собирает их в один документ.
Что делает скрипт
Скрипт отбирает текстовые расширения, ограничивает размер каждого файла и проверяет пути. В обычном режиме файлы сохраняются с исходной структурой, а с флагом --combine весь текст собирается в один файл с заголовками. Бинарные элементы и зашифрованные записи пропускаются.
- Существующие файлы перезаписываются только с флагом --force.
Быстрый старт
python extract_text.py docs.zip --dest ./texts
Скачайте файл, откройте терминал в папке со скриптом и выполните команду.
В Linux и macOS интерпретатор может называться python3, в Windows - python или py.
Другие варианты
python extract_text.py docs.zip --dest ./textspython extract_text.py logs.tar.gz --combine all.txt --max-size 2097152python extract_text.py docs.zip --dest ./texts --flatten --force
Что понадобится
winget install Python.Python.3.12
brew install python
sudo apt install python3
extract_text.py
#!/usr/bin/env python3
"""Извлечение текстовых файлов из zip или tar."""
from __future__ import annotations
import argparse
import shutil
import tarfile
import zipfile
from pathlib import Path, PurePosixPath, PureWindowsPath
TEXT_EXTENSIONS = {
".txt", ".md", ".rst", ".log", ".csv", ".json", ".jsonl", ".yaml", ".yml",
".ini", ".cfg", ".conf", ".config", ".env", ".properties", ".toml", ".xml",
".sh", ".bash", ".zsh", ".py", ".js", ".ts", ".tsx", ".jsx", ".go", ".rs",
".java", ".php", ".rb", ".sql", ".html", ".css", ".ps1", ".bat",
}
def safe_parts(name: str) -> tuple[str, ...]:
normalized = name.replace("\\", "/")
path = PurePosixPath(normalized)
if path.is_absolute() or PureWindowsPath(normalized).drive:
raise ValueError(f"абсолютный путь: {name}")
parts = tuple(part for part in path.parts if part not in ("", "."))
if not parts or any(part == ".." for part in parts):
raise ValueError(f"небезопасный путь: {name}")
return parts
def main() -> int:
parser = argparse.ArgumentParser(description="Извлечь текст из архива")
parser.add_argument("archive", type=Path, help="Zip или tar архив")
parser.add_argument("--dest", type=Path, default=Path("texts"), help="Папка назначения")
parser.add_argument("--max-size", type=int, default=5 * 1024 * 1024, help="Максимальный размер файла")
parser.add_argument("--flatten", action="store_true", help="Сложить файлы в одну папку")
parser.add_argument("--combine", type=Path, help="Собрать текст в один файл")
parser.add_argument("--force", action="store_true", help="Перезаписать существующие файлы")
parser.add_argument("--dry-run", action="store_true", help="Только показать план")
args = parser.parse_args()
archive_path = args.archive.expanduser().resolve()
destination = args.dest.expanduser().resolve()
if not archive_path.is_file():
parser.error(f"Архив не найден: {archive_path}")
if args.combine and args.combine.exists() and not args.force:
parser.error(f"Файл уже существует: {args.combine}. Используйте --force")
plan: list[tuple[str, Path | None, int]] = []
if zipfile.is_zipfile(archive_path):
with zipfile.ZipFile(archive_path) as archive:
for info in archive.infolist():
if info.is_dir() or info.flag_bits & 0x1 or info.file_size > args.max_size:
continue
if Path(info.filename).suffix.lower() not in TEXT_EXTENSIONS:
continue
target = None
if not args.combine:
try:
parts = (Path(info.filename).name,) if args.flatten else safe_parts(info.filename)
except ValueError as error:
print(f"Пропуск: {error}")
continue
target = destination.joinpath(*parts)
if not target.resolve().is_relative_to(destination.resolve()):
continue
plan.append((info.filename, target, info.file_size))
elif tarfile.is_tarfile(archive_path):
with tarfile.open(archive_path, mode="r:*") as archive:
for member in archive:
if not member.isfile() or member.size > args.max_size:
continue
if Path(member.name).suffix.lower() not in TEXT_EXTENSIONS:
continue
target = None
if not args.combine:
try:
parts = (Path(member.name).name,) if args.flatten else safe_parts(member.name)
except ValueError as error:
print(f"Пропуск: {error}")
continue
target = destination.joinpath(*parts)
if not target.resolve().is_relative_to(destination.resolve()):
continue
plan.append((member.name, target, member.size))
else:
print("Формат не распознан. Поддерживаются zip и tar.")
return 2
if not plan:
print("Текстовые файлы не найдены.")
return 0
if not args.combine:
existing = [str(target) for _, target, _ in plan if target is not None and target.exists() and not args.force]
if existing:
print("Файлы уже существуют, добавьте --force:")
for name in existing[:20]:
print(f" {name}")
return 1
if args.dry_run:
for name, target, _ in plan:
print(f" {name} -> {target if target else args.combine}")
print("Режим проверки: ничего не записано.")
return 0
if args.combine:
if zipfile.is_zipfile(archive_path):
context = zipfile.ZipFile(archive_path)
else:
context = tarfile.open(archive_path, mode="r:*")
with context as archive:
with args.combine.open("w", encoding="utf-8") as output:
for name, _, _ in plan:
if isinstance(archive, zipfile.ZipFile):
source = archive.open(name)
else:
source = archive.extractfile(name)
if source is None:
continue
with source:
text = source.read().decode("utf-8", errors="replace")
output.write(f"===== {name} =====\n{text}\n\n")
print(f"Собрано файлов: {len(plan)} в {args.combine}")
return 0
destination.mkdir(parents=True, exist_ok=True)
if zipfile.is_zipfile(archive_path):
with zipfile.ZipFile(archive_path) as archive:
for name, target, _ in plan:
assert target is not None
target.parent.mkdir(parents=True, exist_ok=True)
with archive.open(name) as source, target.open("wb") as handle:
shutil.copyfileobj(source, handle)
else:
with tarfile.open(archive_path, mode="r:*") as archive:
for name, target, _ in plan:
assert target is not None
source = archive.extractfile(name)
if source is None:
continue
target.parent.mkdir(parents=True, exist_ok=True)
with source, target.open("wb") as handle:
shutil.copyfileobj(source, handle)
print(f"Извлечено текстовых файлов: {len(plan)} в {destination}")
return 0
if __name__ == "__main__":
raise SystemExit(main())
Скрипты работают с файлами и настройками системы. Перед запуском прочитайте код и проверьте пути. Особенно если скрипт удаляет, перезаписывает или перемещает данные.