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