Files
cat/02_extract_project_json.py
2026-03-06 17:41:55 +09:00

59 lines
2.1 KiB
Python

import os
import gzip
import tarfile
import json
from io import BytesIO
def extract_project_json_from_gz(filepath, output_folder):
# 1. .gz 압축 해제 → tar 형식 데이터 얻기
with gzip.open(filepath, 'rb') as gz_file:
tar_data = gz_file.read()
# 2. tar 데이터를 메모리에서 처리
tar_bytes = BytesIO(tar_data)
with tarfile.open(fileobj=tar_bytes, mode='r:') as tar:
try:
target = tar.getmember("temp/project.json")
except KeyError:
print(f"{os.path.basename(filepath)}: temp/project.json 없음")
return
# 압축파일 내부 temp/project.json 파일의 파일 오브젝트 추출
extracted = tar.extractfile(target)
if not extracted:
print(f"{os.path.basename(filepath)}: 파일 추출 실패")
return
# 파일 오브젝트를 JSON 객체로 로드
project_data = json.load(extracted)
# 해당 경로에 project.json 파일로 저장
os.makedirs(output_folder, exist_ok=True)
output_json_path = os.path.join(output_folder, "project.json")
with open(output_json_path, "w", encoding="utf-8") as out_file:
json.dump(project_data, out_file, ensure_ascii=False, indent=2)
print(f"✅ 처리 완료: {filepath}{output_json_path}")
def process_ent_files(ent_dir, output_dir):
for filename in os.listdir(ent_dir):
if filename.lower().endswith((".ent")):
filename = filename.replace(" ","")
filepath = os.path.join(ent_dir, filename)
output_root = output_dir
output_folder = os.path.join(output_root, os.path.splitext(filename)[0])
try:
extract_project_json_from_gz(filepath, output_folder)
except Exception as e:
print(f"❌ 오류 발생 ({filename}): {e}")
# 실행 예
if __name__ == "__main__":
exam_names = ["2602_CAS_2_A"]
# exam_names = ["2512_CAS_2_A", "2512_CAS_2_B"]
for exam_name in exam_names:
ent_dir = f".\\ent\\{exam_name}"
output_dir = f".\\output\\{exam_name}"
process_ent_files(ent_dir, output_dir)