新版本Delphi中自带的Zip单元System.Zip使用很方便,但是在压缩文件中包含中文路径或文件名时解压后是乱码,这一点儿确实挺烦人。
之所以会出现乱码是由以下两个函数造成的:
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
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;
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
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;
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
将上面红色标记的代码中CodePage修改为936即可解决,或者直接替换掉这两个函数:
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
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;
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
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://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)