新版本Delphi中自带的Zip单元System.Zip使用很方便,但是在压缩文件中包含中文路径或文件名时解压后是乱码,这一点儿确实挺烦人。
之所以会出现乱码是由以下两个函数造成的:
1 function TZipFile.TBytesToString(B: TBytes): string; 2 var 3 E: TEncoding; 4 begin 5 if FUTF8Support then 6 E := TEncoding.GetEncoding(65001) 7 else 8 E := TEncoding.GetEncoding(437); 9 try 10 Result := E.GetString(B); 11 finally 12 E.Free; 13 end; 14 end;
1 function TZipFile.StringToTBytes(S: string): TBytes; 2 var 3 E: TEncoding; 4 begin 5 if FUTF8Support then 6 E := TEncoding.GetEncoding(65001) 7 else 8 E := TEncoding.GetEncoding(437); 9 try 10 Result := E.GetBytes(S); 11 finally 12 E.Free; 13 end; 14 end;
将上面红色标记的代码中CodePage修改为936即可解决,或者直接替换掉这两个函数:
1 function TZipFile.TBytesToString(B: TBytes): string; 2 begin 3 if FUTF8Support then 4 Result := TEncoding.UTF8.GetString(B) 5 else 6 Result := TEncoding.Default.GetString(B); 7 end;
1 function TZipFile.StringToTBytes(S: string): TBytes; 2 begin 3 if FUTF8Support then 4 Result := TEncoding.UTF8.GetBytes(S) 5 else 6 Result := TEncoding.Default.GetBytes(S); 7 end;
原文地址:https://www.cnblogs.com/huixch/p/9839422.html
时间: 2024-11-08 21:18:31