學 Lazarus 字串壓縮 解壓縮

2022-02-17 11:58:17 字數 3864 閱讀 7228

背景:

1、原有c#開發的wince系統+webservice;

2、客戶說wince上的終端程式執行速度慢,我也說慢;

3、希望改造乙個wince上的終端程式,因為熟悉delphi所以lazarus;

4、lazarus匯入的webservice**,我看不明白,還涉及到資料型別轉換神馬的,決定放棄webservice;

5、打算採用lnet寫個伺服器,接收資料來處理和資料庫之間的互動。

----------------------------------------我是分割線-----------------------------------------

解決思路:

1、lazarus 安裝 lnet元件;(這個不難)

2、把lnet中 那個tcp示例改成多執行緒的;(這個也不難)

3、但資料傳輸過程中效率如何解決?對壓縮一下!如何壓縮?

a、官網上檢索,  ;

b、但這裡面用到了 indy元件,而這個元件點名說了,我暫時不支援wince;

c、找個方法把  indy元件功能替換掉  ;

4、成功了。

**如下:

function tformmain.gzbase64(const s: string): string;

var outstream, inpstream, gzstream, b64stream: tstream;

begin

outstream := tstringstream.create('');

tryb64stream := tbase64encodingstream.create(outstream);

trygzstream := tcompressionstream.create(clmax,b64stream);

tryinpstream := tstringstream.create(s);

try// copy input stream

gzstream.copyfrom(inpstream,inpstream.size);

finally

inpstream.free;

end;

finally

gzstream.free;

end;

finally

b64stream.free;

end;

result := tstringstream(outstream).datastring;

finally

outstream.free;

end;

end;

function tformmain.ungzbase64(const s: string): string;

var outstream,decompressstream: tstream;

sl:tstringlist;

begin

if(s='')

then

begin

result:='';

abort;

end;

sl := tstringlist.create;

outstream := tstringstream.create(base64tostring(s));

decompressstream := tdecompressionstream.create(outstream);

trysl.loadfromstream(decompressstream);

result:=sl.text;

finally

decompressstream.free;

outstream.free;

sl.free;

end;

end;

function   tformmain.base64tostring(const   value:   string):   string;

varx,   y,   n,   l:   integer;

d:   array[0..3]   of   byte;

table   :   string;

begin

table   :=

#$40   +#$40   +#$40   +#$40   +#$40   +#$40   +#$40   +#$40   +#$40   +#$40   +#$3e   +#$40

+#$40   +#$40   +#$3f   +#$34   +#$35   +#$36   +#$37   +#$38   +#$39   +#$3a   +#$3b   +#$3c

+#$3d   +#$40   +#$40   +#$40   +#$40   +#$40   +#$40   +#$40   +#$00   +#$01   +#$02   +#$03

+#$04   +#$05   +#$06   +#$07   +#$08   +#$09   +#$0a   +#$0b   +#$0c   +#$0d   +#$0e   +#$0f

+#$10   +#$11   +#$12   +#$13   +#$14   +#$15   +#$16   +#$17   +#$18   +#$19   +#$40   +#$40

+#$40   +#$40   +#$40   +#$40   +#$1a   +#$1b   +#$1c   +#$1d   +#$1e   +#$1f   +#$20   +#$21

+#$22   +#$23   +#$24   +#$25   +#$26   +#$27   +#$28   +#$29   +#$2a   +#$2b   +#$2c   +#$2d

+#$2e   +#$2f   +#$30   +#$31   +#$32   +#$33   +#$40   +#$40   +#$40   +#$40   +#$40   +#$40;

setlength(result,   length(value));

x   :=   1;

l   :=   1;

while   x   <   length(value)   do

begin

for   n   :=   0   to   3   do

begin

if   x   >   length(value)   then

d[n]   :=   64

else

begin

y   :=   ord(value[x]);

if   (y   <   33)   or   (y   >   127)   then

d[n]   :=   64

else

d[n]   :=   ord(table[y   -   32]);

end;

inc(x);

end;

result[l]   :=   char((d[0]   and   $3f)   shl   2   +   (d[1]   and   $30)   shr   4);

inc(l);

if   d[2]   <>   64   then

begin

result[l]   :=   char((d[1]   and   $0f)   shl   4   +   (d[2]   and   $3c)   shr   2);

inc(l);

if   d[3]   <>   64   then

begin

result[l]   :=   char((d[2]   and   $03)   shl   6   +   (d[3]   and   $3f));

inc(l);

end;

end;

end;

dec(l);

setlength(result,   l);

end;

C 壓縮 解壓字串

呼叫壓縮方法 string compstr abcdefg var rawstr getstringbydataset compstr region 壓縮 根據dataset壓縮字串 public static string getstringbydataset string ds 將傳入字串以gz...

字串的解壓縮

問題描述 對字串進行解壓縮 例如 5w1g2b wwwwwgbb 解決方案 1.將輸入字串分別為集合5w 1g 2b,每個集合由乙個數字和乙個字元構成 2.從尾到頭讀取字串,當遇到乙個字元的時候,儲存下來 當遇到乙個數字的時候,我們開始建立乙個數,直到遇到下乙個字元 3.當遇到下乙個字元之前,我們應...

字串壓縮 解壓縮(時間優先)

字串壓縮 解壓縮 時間優先 壓縮 1 得到字串長度 利用長度申請一片新的記憶體newstr來儲存 2 遍歷源字串,如果不是 0 進入迴圈 宣告 pindex 來記錄當前 字元 repetitionlength 記錄重複的字元次數 3 pindex 與 pindex 1 進行比較 如果相等 repet...