Implementation of zlib compression algorithm between Java and Delphi

Source: Internet
Author: User
Tags zip

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;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.