當前位置:成語大全網 - 書法字典 - LZSS壓縮算法的Java代碼

LZSS壓縮算法的Java代碼

可以參考壹下這段代碼:/downloads 121/source code/Java/detail 516351 . html。

導入Java . io . *;?

公共課包?

{?

公共最終靜態字節F_PACK_MAGIC[]={ 0x73,0x6C,0x68,0x 21 };?

/*打包文件的幻數*/?

public final靜態字節F_NOPACK_MAGIC[]= { 0x73,0x6C,0x68,0x2E };?

/*自動檢測的幻數*/?

私有靜態void用法()?

{?

system . out . print(" \ n Allegro 3.12的文件壓縮實用程序");?

Shawn Hargreaves,Aug 1999。Radim Kolar的Java版本,Dec 1999 \ n \ n ");?

System.out.print("用法:' pack & lt在& gt& ltout & gt'打包壹個文件\ n ");?

System.out.print("?打包u & lt在& gt& ltout & gt'解壓縮文件\ n ");?

system . exit(1);?

}?

公共靜態void main(String argv[])拋出IOException?

{?

字符串t =?

字符串f1= " ",f2 =?

InputStream in?

output流輸出;?

long s1,S2;?

布爾包=真;?

if (argv.length==2) {?

f 1 = argv[0];?

F2 = argv[1];?

t = " Pack?

}?

else if((argv . length = = 3)& amp;& amp(argv[0]。length()= = 1)& amp;& amp?

((argv[0])。charAt(0)=='u') || (argv[0]。charAt(0)= = ' U ')){?

f 1 = argv[1];?

F2 = argv[2];?

t = " Unpack?

pack = false?

}?

不然呢?

用法();?

if (f1.equals(f2))?

{?

system . out . println(" \ n錯誤:文件必須不同。);?

system . exit(1);?

}?

in = null?

試試?

{?

在?= new file inputstream(f 1);?

}?

catch ( IOException i)?

{?

system . out . println(" \ n錯誤:"+f1+" -無法打開。);?

system . exit(1);?

}?

s1 =新文件(f1)。長度();?

/*手柄魔術*/?

如果(!打包)?{?

byte magic[]= new byte[4];?

in.read(魔法);?

int bad = 0;?

for(int I = 2;我& gt=0;我-)?

如果(魔[我]!=F_PACK_MAGIC[i])

bad = 1;?

if(bad==0)?

if(魔法[3]!=F_PACK_MAGIC[3])?

if(MAGIC[3]= = F _ no pack _ MAGIC[3])bad = 2;?

else bad = 1;?

if(bad==1)?

{?

system . out . println(" \ n錯誤:"+f1+" -不是打包文件。(沒有魔法)”);?

system . exit(1);?

}?

if(bad==0) in?=新LZSSInputStream(in);?

}?

out = new file output stream(F2);?

如果(包裝)

{?

out . write(F _ PACK _ MAGIC);?

out = new LZSSOutputStream(out);?

}?

system . out . println(t+" ing "+f 1+"轉換為"+f2+" ... ");?

字節b[]=新字節[512];?

int I;?

while ( (i=in.read(b))!=-1) {?

out.write(b,0,I);?

}?

in . close();?

out . close();?

if(s 1 & gt;0) {?

s2 =新文件(f2)。長度();?

system . out . println(" \ n輸入大小:"+s 1+" \ n輸出大小:"+S2+" \ n "+(S2 * 100+(s 1 & gt;& gt1))/s 1+“%”;?

}?

返回;?

}?

}