如何在 Python 中膨胀一些被 Peoplesoft (Java) 缩小的数据?

How to inflate in Python some data that was deflated by Peoplesoft (Java)?

免责声明:为了帮助我解决这个问题,Peoplesoft 知识不是强制性的!

我如何从 PUBDATALONG 列中提取 Peoplesoft table 的数据? table 的描述在这里: http://www.go-faster.co.uk/peopletools/psiblogdata.htm

目前我正在使用用Java编写的程序,下面是一段代码:

Inflater inflater = new Inflater();
byte[] result = new byte[rs.getInt("UNCOMPDATALEN")];
inflater.setInput(rs.getBytes("PUBDATALONG"));
int length = inflater.inflate(result);

System.out.println(new String(result, 0, length, "UTF-8"));
System.out.println();
System.out.println("-----");
System.out.println();

如何使用 Python 重写它? 这是一个在 Whosebug 上以其他形式出现但没有真正答案的问题。 我对 Java 中代码的作用有基本的了解,但我不知道 Python 中的任何库,我可以使用它来实现同样的事情。

有人推荐尝试 zlib,因为它与 Java Inflater class 使用的算法兼容,但我没有成功。 考虑到 PeopleSoft 手册中的以下事实:

When the message is received by the PeopleSoft database, the XML data is converted to UTF-8 to prevent any UCS2 byte order issues. It is also compressed using the deflate algorithm prior to storage in the database.

我试过这样的事情:

import zlib
import base64


UNCOMPDATALEN = 362 #this value is taken from the DB and is the dimension of the data after decompression.
PUBDATALONG = '789CB3B1AFC8CD51284B2D2ACECCCFB35532D43350B2B7E3E5B2F130F40C8977770D8977F4710D0A890F0E710C090D8EF70F0D09080DB183C8BAF938BAC707FBBBFB783ADA19DAE86388D904B90687FAC0F4DAD940CD70F67771B533B0D147E6DAE8A3A9D5C76B3F00E2F4355C=='


print zlib.decompress(base64.b64decode(PUBDATALONG), 0, 362)

我明白了:

zlib.error: Error -3 while decompressing data: incorrect header check

我确实做错了,但我不够聪明,无法自己解决。

该字符串不是 Base-64 编码的。它只是十六进制的。 (我不知道为什么它以 == 结尾,这使得它看起来有点像 Base-64 字符串。)您应该能够通过检查看到没有小写字母,或者大写字母F 之后的大小写字母,因为在典型的 Base-64 编码压缩字符串中,即随机出现的数据。

去掉末尾的等号,在Python2中使用.decode("hex"),或者在Python3中使用bytes.fromhex()