주요 콘텐츠로 건너뛰기

콘텐츠 내보내기 및 가져오기

Glasswall는 지원되는 파일 유형에 대해 콘텐츠 항목을 내보내고 가져오는 기능을 제공합니다. 이를 통해 처리된 파일의 내부 구성 요소를 Glasswall Embedded Engine 도메인 외부에서 추가 처리할 수 있도록 외부 프로세스 및 애플리케이션에서 사용할 수 있습니다. 내보낸 후 이러한 구성 요소는 외부에서 검증할 수 있으며, 이후 Glasswall Engine가 해당 구성 요소를 가져와 파일을 다시 구성합니다.

파일은 Glasswall Embedded Engine에서 두 번 처리되어야 합니다. 한 번은 파일을 구성하는 구성 요소가 포함된 패키지를 추출하기 위해서(export), 두 번째는 외부에서 분석 및/또는 수정된 구성 요소를 파일에 다시 통합하기 위해서(import)입니다. Content Export & Import를 참조하세요.

파일은 export_file 메서드를 사용하여 파일 경로에서 또는 메모리 내에서 개별적으로 내보낼 수 있으며, 디렉터리의 모든 파일은 export_directory 메서드를 사용하여 내보낼 수 있습니다.

예제

내보내기

파일 경로에서 파일 경로로 내보내기

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Use the default policy to export a file, writing the export archive to a new path
editor.export_file(
input_file=r"C:\gwpw\input\TestFile_11.doc",
output_file=r"C:\gwpw\output\editor\export_f2f\TestFile_11.doc.zip",
)

파일 경로에서 메모리로 내보내기

export_file는 내보낸 아카이브 파일의 바이트를 반환합니다. 아래 예제는 export_archive 변수를 할당하고 Editor 내보내기 아카이브 시작 부분의 내용을 확인하는 방법을 보여줍니다.

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Use the default policy to export a file
export_archive = editor.export_file(
input_file=r"C:\gwpw\input\TestFile_11.doc",
)

assert export_archive[:8] == b'PK\x03\x04\x14\x00\x0e\x00'

메모리에서 내보내기

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Read file from disk to memory
with open(r"C:\gwpw\input\TestFile_11.doc", "rb") as f:
input_bytes = f.read()

# Use the default policy to export a file
export_archive = editor.export_file(
input_file=input_bytes,
)

assert export_archive[:8] == b'PK\x03\x04\x14\x00\x0e\x00'

디렉터리의 파일 내보내기

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Use the default policy to export a directory of files, writing the export archives to a new directory.
editor.export_directory(
input_directory=r"C:\gwpw\input",
output_directory=r"C:\gwpw\output\editor\export_directory"
)

지원되지 않는 파일 형식이 포함될 수 있는 디렉터리의 파일 내보내기

Glasswall Python wrapper의 기본 동작은 처리가 실패할 경우 관련 예외를 발생시키는 것입니다(참조: glasswall.libraries.editor.errors). raise_unsupported=False를 전달하면 예외가 발생하지 않도록 할 수 있으며, 지원되는 파일 형식과 지원되지 않는 파일 형식이 혼합된 디렉터리에서 작업할 때 첫 번째 실패에서 종료하는 대신 가능한 한 많은 파일을 처리하려는 경우 유용할 수 있습니다.

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Use the default policy to export a directory of files, writing the export archives to a new directory.
editor.export_directory(
input_directory=r"C:\gwpw\input_with_unsupported_file_types",
output_directory=r"C:\gwpw\output\editor\export_directory_unsupported",
raise_unsupported=False
)

사용자 지정 콘텐츠 관리 policy를 사용하여 디렉터리의 파일 내보내기

glasswall.content_management.policies.Editor 사용:

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Use a custom Editor policy to export all files in the input directory
# and write them to export_directory_custom directory. Write streams as
# ".xml" instead of the default interchange_type, ".sisl". Export embedded
# images as ".xml" instead of their default image file type.
editor.export_directory(
input_directory=r"C:\gwpw\input",
output_directory=r"C:\gwpw\output\editor\export_directory_custom",
content_management_policy=glasswall.content_management.policies.Editor(
default="sanitise",
config={
"sysConfig": {
"interchange_type": "xml",
"export_embedded_images": "true",
},
}
),
raise_unsupported=False
)

파일 형식에 따라 조건부로 디렉터리의 파일 내보내기

아래 예제는 여러 파일 형식이 포함된 중첩 디렉터리에서 doc 및 docx 파일만 처리하는 방법을 보여줍니다.

import os

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

input_directory = r"C:\gwpw\input"
output_directory = r"C:\gwpw\output\editor\export_directory_file_format"

# Iterate relative file paths from input_directory
for relative_file in glasswall.utils.list_file_paths(input_directory, absolute=False):
# Construct absolute paths
input_file = os.path.join(input_directory, relative_file)
output_file = os.path.join(output_directory, relative_file + ".zip")

# Get the file type of the file
file_type = editor.determine_file_type(
input_file=input_file,
as_string=True,
raise_unsupported=False
)

# Export only doc and docx files
if file_type in ["doc", "docx"]:
editor.export_file(input_file, output_file)


가져오기

내보내기 아카이브는 import_file 메서드를 사용하여 파일 경로에서 또는 메모리 내에서 개별적으로 가져올 수 있으며, 디렉터리의 모든 내보내기 아카이브는 import_directory 메서드를 사용하여 가져올 수 있습니다.

파일 경로에서 파일 경로로 가져오기

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Use the default policy to import an export archive, writing the imported file to a new path
editor.import_file(
input_file=r"C:\gwpw\output\editor\export_f2f\TestFile_11.doc.zip",
output_file=r"C:\gwpw\output\editor\import_f2f\TestFile_11.doc",
)

파일 경로에서 메모리로 가져오기

import_file는 가져온 파일의 바이트를 반환합니다. 아래 예제는 file_bytes 변수에 할당하고 Editor 내보내기 아카이브 시작 부분의 내용을 확인하는 방법을 보여줍니다.

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Use the default policy to import an export archive
file_bytes = editor.import_file(
input_file=r"C:\gwpw\output\editor\export_f2f\TestFile_11.doc.zip",
)

assert file_bytes[:8] == b'\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1'

메모리에서 가져오기

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Read file from disk to memory
with open(r"C:\gwpw\output\editor\export_f2f\TestFile_11.doc.zip", "rb") as f:
export_archive_bytes = f.read()

# Use the default policy to import an export archive
file_bytes = editor.import_file(
input_file=export_archive_bytes,
)

assert file_bytes[:8] == b'\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1'

디렉터리의 파일 가져오기

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Use the default policy to import a directory of export archives, writing the import archives to a new directory.
editor.import_directory(
input_directory=r"C:\gwpw\output\editor\export_directory",
output_directory=r"C:\gwpw\output\editor\import_directory"
)

지원되지 않는 파일 형식이 포함될 수 있는 디렉터리의 파일 가져오기

Glasswall Python wrapper의 기본 동작은 처리가 실패할 경우 관련 예외를 발생시키는 것입니다(참조: glasswall.libraries.editor.errors). raise_unsupported=False를 전달하면 예외가 발생하지 않도록 할 수 있으며, 지원되는 파일 형식과 지원되지 않는 파일 형식이 혼합된 디렉터리에서 작업할 때 첫 번째 실패에서 종료하는 대신 가능한 한 많은 파일을 처리하려는 경우 유용할 수 있습니다.

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Use the default policy to export a directory of export archives, writing the export archives to a new directory.
editor.import_directory(
input_directory=r"C:\gwpw\output\editor\export_directory_unsupported",
output_directory=r"C:\gwpw\output\editor\import_directory_unsupported",
raise_unsupported=False
)

사용자 지정 콘텐츠 관리 policy를 사용하여 디렉터리의 파일 가져오기

glasswall.content_management.policies.Editor 사용:

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Use a custom Editor policy to import all files in the export directory
# and write them to import_directory_custom directory. Read streams as
# ".xml" instead of the default interchange_type, ".sisl".
editor.import_directory(
input_directory=r"C:\gwpw\output\editor\export_directory_custom",
output_directory=r"C:\gwpw\output\editor\import_directory_custom",
content_management_policy=glasswall.content_management.policies.Editor(
default="sanitise",
config={
"sysConfig": {
"interchange_type": "xml",
},
}
),
raise_unsupported=False
)

파일 형식에 따라 조건부로 디렉터리의 파일 가져오기

아래 예제는 여러 파일 형식이 포함된 중첩 디렉터리에서 doc 및 docx 파일만 처리하는 방법을 보여줍니다.

import os

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

input_directory = r"C:\gwpw\output\editor\export_directory_file_format"
output_directory = r"C:\gwpw\output\editor\import_directory_file_format"

# Iterate relative file paths from input_directory
for relative_file in glasswall.utils.list_file_paths(input_directory, absolute=False):
# Construct absolute paths
input_file = os.path.join(input_directory, relative_file)
output_file = os.path.join(output_directory, os.path.splitext(relative_file)[0])

# Get the file type of the file
file_type = editor.determine_file_type(
input_file=input_file,
as_string=True,
raise_unsupported=False
)

# Import only doc.zip and docx.zip files
if file_type == "zip" and input_file.endswith(("doc.zip", "docx.zip",)):
editor.import_file(input_file, output_file)


내보내기/가져오기 및 분석

이러한 상위 수준 함수는 단일 세션 내에서 Export 또는 Import를 실행하고 분석 보고서를 생성할 수 있게 해줍니다. 자세한 내용은 아래 문서 링크를 참조하세요.