zoukankan      html  css  js  c++  java
  • Delphi中System.Zip中文文件名乱码解决

    新版本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;
  • 相关阅读:
    面向对象-类
    模块04
    总结
    昨天的新的解决方法
    感冒了~ vs中py和vb实现一个小算法
    vs2015社区版不支持installshield
    网站被黑了
    2018/11/18(python)
    2018/11/14
    2018/11/12(python)
  • 原文地址:https://www.cnblogs.com/huixch/p/9839422.html
Copyright © 2011-2022 走看看