可以參考壹下這個代碼:/downloads121/sourcecode/java/detail516351.html
import java.io.*;?
public class pack?
{?
public final static byte F_PACK_MAGIC[]={ 0x73,0x6C,0x68,0x21};?
/* magic number for packed files */?
public final static byte F_NOPACK_MAGIC[]= { 0x73, 0x6C, 0x68, 0x2E};?
/* magic number for autodetect */?
private static void usage()?
{?
System.out.print("\nFile compression utility for Allegro 3.12");?
System.out.print("\nBy Shawn Hargreaves, Aug 1999. Java version by Radim Kolar, Dec 1999\n\n");?
System.out.print("Usage: 'pack <in> <out>' to pack a file\n");?
System.out.print("? 'pack u <in> <out>' to unpack a file\n");?
System.exit(1);?
}?
public static void main(String argv[]) throws IOException?
{?
String t="";?
String f1="",f2="";?
InputStream in;?
OutputStream out;?
long s1,s2;?
boolean pack=true;?
if (argv.length==2) {?
f1 = argv[0];? f2 = argv[1];? t = "Pack";?}?
else if ((argv.length==3) && (argv[0].length()==1) &&?
((argv[0].charAt(0)=='u') || (argv[0].charAt(0)=='U'))) {?
f1 = argv[1];? f2 = argv[2];? t = "Unpack";? pack=false;?}?
else?
usage();?if (f1.equals(f2))?
{?
System.out.println("\nError: Files must be different.");?
System.exit(1);?
}?
in = null;?
try?
{?in? = new FileInputStream(f1);?
}?catch ( IOException i)?
{?System.out.println("\nError: "+f1+" - Can't open.");?
System.exit(1);?
}?s1 = new File(f1).length();?
/* handle magic */?
if(!pack)? {?
byte magic[]=new byte[4];?
in.read(magic);?
int bad=0;?
for(int i=2;i>=0;i--)?
if(magic[i]!=F_PACK_MAGIC[i]) bad=1;?if(bad==0)?
if(magic[3]!=F_PACK_MAGIC[3])?if(magic[3]==F_NOPACK_MAGIC[3]) bad=2;?
else bad=1;?
if(bad==1)?
{? System.out.println("\nError: "+f1+" - Not a packed file. (No magic)");? System.exit(1);? }?if(bad==0) in? = new LZSSInputStream(in);?
}?
out = new FileOutputStream(f2);?
if(pack)
{?
out.write(F_PACK_MAGIC);? out = new LZSSOutputStream(out);?}?
System.out.println(t+"ing "+f1+" into "+f2+"...");?
byte b[]=new byte[512];?
int i;?
while ( (i=in.read(b))!=-1) {?
out.write(b,0,i);?}?
in.close();?
out.close();?
if (s1 > 0) {?
s2 = new File(f2).length();? System.out.println("\nInput size: "+s1+"\nOutput size: "+s2+"\n"+(s2*100+(s1>>1))/s1+"%");?}?
return;?
}?
}