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;
  • 相关阅读:
    继承作业0920
    类与对象
    类和对象基础题
    类和对象数组
    数组
    字符串
    2.1面向对象
    7.1 Java集合概述
    Java动态代理的两种实现方法
    18.5.2动态代理和AOP
  • 原文地址:https://www.cnblogs.com/huixch/p/9839422.html
Copyright © 2011-2022 走看看