增加UTF8编码以正确显示中文

This commit is contained in:
2025-09-26 15:54:46 +08:00
parent fc26733367
commit 7cf979620a

View File

@@ -5,6 +5,7 @@ import subprocess
import threading
import platform
import queue # For thread-safe communication
import locale # For getting system's default encoding
# --- Configuration ---
# Robocopy Flags for Mirroring (/MIR)
@@ -142,6 +143,8 @@ class RobocopyMirrorApp: # Renamed class for clarity
def run_robocopy_process(self, source, target):
"""The actual function running in the thread."""
try:
# Determine the console's output encoding, crucial for non-English filenames
output_encoding = locale.getpreferredencoding(False)
# Use the MIRROR_ROBOCOPY_FLAGS
command = ["robocopy", source, target] + MIRROR_ROBOCOPY_FLAGS
@@ -149,18 +152,17 @@ class RobocopyMirrorApp: # Renamed class for clarity
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, # Redirect stderr to stdout
text=True,
encoding='utf-8',
errors='replace',
# text=True and encoding are removed to read raw bytes
bufsize=1,
creationflags=subprocess.CREATE_NO_WINDOW # Prevent cmd window popup
)
# Read output line by line
for line in iter(self.process.stdout.readline, ''):
# Read output line by line as bytes and decode
for line in iter(self.process.stdout.readline, b''):
if not line: # Check if process has exited
break
self.output_queue.put(line)
# Decode using the system's preferred encoding and put into the queue
self.output_queue.put(line.decode(output_encoding, errors='replace'))
if self.process.poll() is not None and not line: # Double check if process ended and no more output
break