A typical application, using Delphi as the client, Java service side, between the two with XML as data exchange, in order to improve efficiency, the XML data compression, to find a compression/decompression algorithm to two platform interaction between the use of zlib algorithm is a good solution.
1, Java implementation
In the JDK, the implementation of the zlib is already built into the Java.util.zip package, and the sample code is as follows:
1//解压
2 public String decompressData(String encdata) {
3 try {
4 ByteArrayOutputStream bos = new ByteArrayOutputStream();
5 InflaterOutputStream zos = new InflaterOutputStream(bos);
6 zos.write(convertFromBase64(encdata));
7 zos.close();
8 return new String(bos.toByteArray());
9 } catch (Exception ex) {
10 ex.printStackTrace();
11 return "UNZIP_ERR";
12 }
13 }
14
15 //压缩
16 public String compressData(String data) {
17 try {
18 ByteArrayOutputStream bos = new ByteArrayOutputStream();
19 DeflaterOutputStream zos = new DeflaterOutputStream(bos);
20 zos.write(data.getBytes());
21 zos.close();
22 return new String(convertToBase64(bos.toByteArray()));
23 } catch (Exception ex) {
24 ex.printStackTrace();
25 return "ZIP_ERR";
26 }
27 }
2, the realization of Delphi
In Delphi, there are 3rd party controls can be used to achieve compression/decompression, where we choose Vclzip V3.04, you can download http://www.vclzip.net from here. To improve versatility, we can write a standard DLL that can be invoked at random on the Win32 platform, and the key code is as follows:
function Cmip_CompressStr(txt: PChar): pchar; stdcall;
var
zip: TVclZip;
compr: string;
data: PChar;
begin
zip := TVclZip.Create(nil);
compr := zip.ZLibCompressString(txt);
data := pchar(Base64EncodeStr(compr));
Result := StrNew(data);
zip.Free
end;
function Cmip_DeCompressStr(txt: PChar): pchar; stdcall;
var
zip: TVCLUnZip;
compr: string;
data: PChar;
begin
zip := TVCLUnZip.Create(nil);
compr := zip.ZLibDecompressString(Base64DecodeStr(txt));
data := StrNew(pchar(compr));
Result := data;
zip.Free
end;