正确解压包含中文文件名Winzip压缩包 - asdic - JavaEye技术网站

来源:百度文库 编辑:神马文学网 时间:2024/04/28 08:58:20
一、问题的出现:

  有一天有人发了个.zip的文件给我。我unzip它,却发现中文名的文件解出来文件名是错的。

  二、上网寻找问题答案:

  有网友告诉我,是因为winzip用某种OEM方式修改文件名所致(后来证明这个说法是错的), 要用另外一个某某更强大的zip并关闭OEM方式再压缩才行。 但我想我们不应让别人必须用某某个软件来将就Unzip。

  三、临时解决问题:*本文所提供的方法是临时性的解决办法*

  1. 到底发生了什么问题?

  ---------------------------

  实验1:在Linux下用zip压缩两个中文名文件并命名为1.zip,

  结果1:在windows98下用Winzip打开中文件名没问题。

  实验2: 在windows98下用Winzip压缩相同的两个中文名文件并命名为2.zip,

  结果2:在Linux下用Unzip打开中文名出来的完全不对,但长度一致。

  实验3: 用二进制查看器比较两个压缩文件(1.zip,2.zip),

  结果3:发现两个压缩文件中的文件名二进制编码是完全一样的。

  实验4: Unzip比较两个压缩文件的信息

  结果4: 除文件名不同以外,就是类型(type)不同,一个是Unix,一个是fat。

  结论:

  winzip存文件名的方法与Unzip完全相同,

  问题在Unzip中,它将类型为fat的压缩文件中的文件名作了另外的解释。

  2.解决问题的方法

  ***************************************

  *修改Unzip让它对文件名不作另外的解释。*

  ***************************************

  找呀找......,终于找到了(我是通过学习了emacs及etags来找的)

  ==============================================================================

  在 Unzip-5.42 源文件包中的文件 unzpriv.h 约第 2396行上下

  ==============================================================================

  /* Convert filename (and file comment string) into "internal" charset.

  * This macro assumes that Zip entry filenames are coded in OEM (IBM DOS)

  * codepage when made on

  * -> DOS (this includes 16-bit Windows 3.1) (FS_FAT_)

  * -> OS/2 (FS_HPFS_)

  * -> Win95/WinNT with Nico Mak's WinZip (FS_NTFS_ && hostver == "5.0")

  * EXCEPTIONS:

  * PKZIP for Windows 2.5 and 2.6 flag their entries as "FS_FAT_", but the

  * filename stored in the local header is coded in Windows ANSI (ISO 8859-1).

  * Likewise, PKZIP for UNIX 2.51 flags its entries as "FS_FAT_", but the

  * filenames stored in BOTH the local and the central header are coded

  * in the local system's codepage (usually ANSI codings like ISO 8859-1).

  *

  * All other ports are assumed to code zip entry filenames in ISO 8859-1.

  */

  #ifndef Ext_ASCII_TO_Native

  # define Ext_ASCII_TO_Native(string, hostnum, hostver, isuxatt, islochdr) \

  if (((hostnum) == FS_FAT_ && \

  !(((islochdr) || (isuxatt)) && \

  (hostver) >= 25 && (hostver) <= 26)) || \

  (hostnum) == FS_HPFS_ || \

  ((hostnum) == FS_NTFS_ && (hostver) == 50)) { \

  _OEM_INTERN((string)); \

  } else { \

  _ISO_INTERN((string)); \

  }

  #endif

  =============================================================================

  *******************************************

  *临时修正的方法:全部都用函数_ISO_INTERN()*

  *******************************************

  修改如下:

  =============================================================================

  #ifndef Ext_ASCII_TO_Native

  # define Ext_ASCII_TO_Native(string, hostnum, hostver, isuxatt, islochdr) \

  /* if (((hostnum) == FS_FAT_ && \

  * !(((islochdr) || (isuxatt)) && \

  * (hostver) >= 25 && (hostver) <= 26)) || \

  * (hostnum) == FS_HPFS_ || \

  * ((hostnum) == FS_NTFS_ && (hostver) == 50)) { \

  * _OEM_INTERN((string)); \

  * } else { \ */

  _ISO_INTERN((string)); \

  /* } */

  #endif

  ==============================================================================

  编译成功,试一试,正确认出winzip压缩的中文名!!!

  ==============================================================================

  四、方法的局限性和临时性:

  ***************************************************************

  *方法的缺点很明显,不能正确支持那几种作了特殊处理的文件名了!!!*

  *所以请高手指教更优的方法!!!