Xuất và nhập nội dung
Glasswall cung cấp khả năng xuất và nhập các mục nội dung cho các loại tệp được hỗ trợ. Điều này cho phép các thành phần nội bộ của các tệp đã được xử lý được cung cấp cho các quy trình và ứng dụng bên ngoài để xử lý bổ sung ngoài phạm vi của Glasswall Embedded Engine. Sau khi được xuất, các thành phần này có thể được xác thực bên ngoài trước khi Glasswall Engine nhập các thành phần và tái cấu thành các tệp.
Tệp phải được xử lý bởi Glasswall Embedded Engine hai lần; một lần để trích xuất một gói chứa các thành phần cấu thành tệp (xuất), và lần thứ hai để tích hợp lại vào tệp các thành phần đã được phân tích bên ngoài và/hoặc chỉnh sửa (nhập). Xem Xuất & Nhập Nội dung.
Có thể xuất từng tệp riêng lẻ từ một đường dẫn tệp hoặc trong bộ nhớ bằng phương thức export_file, hoặc có thể xuất tất cả các tệp từ một thư mục bằng phương thức export_directory.
Ví dụ
- Export
- Xuất từ đường dẫn tệp sang đường dẫn tệp
- Xuất từ đường dẫn tệp vào bộ nhớ
- Xuất từ bộ nhớ
- Xuất các tệp trong một thư mục
- Xuất các tệp trong một thư mục có thể chứa các loại tệp không được hỗ trợ
- Xuất các tệp trong một thư mục bằng policy quản lý nội dung tùy chỉnh
- Xuất các tệp trong một thư mục có điều kiện dựa trên định dạng tệp
- Import
- Nhập từ đường dẫn tệp sang đường dẫn tệp
- Nhập từ đường dẫn tệp vào bộ nhớ
- Nhập từ bộ nhớ
- Nhập các tệp trong một thư mục
- Nhập các tệp trong một thư mục có thể chứa các loại tệp không được hỗ trợ
- Nhập các tệp trong một thư mục bằng policy quản lý nội dung tùy chỉnh
- Nhập các tệp trong một thư mục theo điều kiện dựa trên định dạng tệp
- Xuất/Nhập và Phân tích
Xuất
Xuất từ đường dẫn tệp sang đường dẫn tệp
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",
)
Xuất từ đường dẫn tệp vào bộ nhớ
export_file trả về các byte của tệp kho lưu trữ đã xuất. Ví dụ bên dưới minh họa việc gán biến export_archive và kiểm tra nội dung ở phần đầu của một kho lưu trữ xuất của 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'
Xuất từ bộ nhớ
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'
Xuất các tệp trong một thư mục
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"
)
Xuất các tệp trong một thư mục có thể chứa các loại tệp không được hỗ trợ
Hành vi mặc định của trình bao bọc Python của Glasswall là đưa ra ngoại lệ tương ứng (xem: glasswall.libraries.editor.errors) nếu quá trình xử lý thất bại. Việc truyền raise_unsupported=False sẽ ngăn không cho ngoại lệ được đưa ra và có thể hữu ích khi làm việc với một thư mục chứa hỗn hợp cả loại tệp được hỗ trợ và không được hỗ trợ, trong trường hợp mong muốn xử lý càng nhiều tệp càng tốt thay vì chấm dứt ở lần thất bại đầu tiên.
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
)
Xuất các tệp trong một thư mục bằng policy quản lý nội dung tùy chỉnh
Sử dụng 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
)
Xuất tệp trong một thư mục có điều kiện dựa trên định dạng tệp
Ví dụ dưới đây minh họa việc chỉ xử lý các tệp doc và docx từ một thư mục lồng nhau chứa nhiều định dạng tệp.
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)
Nhập
Các kho lưu trữ xuất có thể được nhập riêng lẻ từ một đường dẫn tệp hoặc trong bộ nhớ bằng phương thức import_file, hoặc tất cả các kho lưu trữ xuất từ một thư mục có thể được nhập bằng phương thức import_directory.
Nhập từ đường dẫn tệp sang đường dẫn tệp
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",
)
Nhập từ đường dẫn tệp vào bộ nhớ
import_file trả về bytes của tệp đã nhập. Ví dụ dưới đây minh họa việc gán biến file_bytes và kiểm tra nội dung ở phần đầu của một kho lưu trữ xuất 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'
Nhập từ bộ nhớ
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'
Nhập các tệp trong một thư mục
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"
)
Nhập các tệp trong một thư mục có thể chứa các loại tệp không được hỗ trợ
Hành vi mặc định của trình bao bọc Python của Glasswall là đưa ra ngoại lệ tương ứng (xem: glasswall.libraries.editor.errors) nếu quá trình xử lý thất bại. Việc truyền raise_unsupported=False sẽ ngăn không cho ngoại lệ được đưa ra và có thể hữu ích khi làm việc với một thư mục chứa hỗn hợp cả loại tệp được hỗ trợ và không được hỗ trợ, trong trường hợp mong muốn xử lý càng nhiều tệp càng tốt thay vì chấm dứt ở lần thất bại đầu tiên.
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
)
Nhập các tệp trong một thư mục bằng policy quản lý nội dung tùy chỉnh
Sử dụng 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
)
Nhập các tệp trong một thư mục theo điều kiện dựa trên định dạng tệp
Ví dụ dưới đây minh họa việc chỉ xử lý các tệp doc và docx từ một thư mục lồng nhau chứa nhiều định dạng tệp.
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)
Xuất/Nhập & Phân tích
Các hàm cấp cao này cho phép bạn chạy Export hoặc Import và tạo báo cáo phân tích trong một phiên duy nhất. Để biết thêm thông tin, hãy xem các liên kết tài liệu bên dưới.