ข้ามไปยังเนื้อหาหลัก

การส่งออกและนำเข้าเนื้อหา

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 จะส่งคืน bytes ของไฟล์อาร์ไคฟ์ที่ส่งออก ตัวอย่างด้านล่างสาธิตการกำหนดตัวแปร 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 จะส่งคืน bytes ของไฟล์ที่นำเข้า ตัวอย่างด้านล่างสาธิตการกำหนดตัวแปร file_bytes และตรวจสอบเนื้อหาช่วงต้นของ Editor export archive

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 & วิเคราะห์

ฟังก์ชันระดับสูงเหล่านี้ช่วยให้คุณเรียกใช้ Export หรือ Import และสร้างรายงานการวิเคราะห์ภายในเซสชันเดียว สำหรับข้อมูลเพิ่มเติม โปรดดูลิงก์เอกสารด้านล่าง