⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tculzw.java

📁 为公司做的质量考核接口源码,用spring,hibernate,XML实现,对XML接口编程很有帮助
💻 JAVA
字号:
package com.jr81.source.compression;/* * LZW.java * * Created on 01 Dec 2005 * * Implementation of LZW compression/decompression algorithm */import java.io.* ;import java.util.zip.GZIPOutputStream;/** * * @author  Moshe Fresko * @course	Algorithmic Programming 1 * @exercise	3 */public class TcuLZW implements Compression{	boolean stopped = false ;	Dict dict ;		// The bits that should be written for each code	int numOfBits ;		// The previous string that we should remember		// in order to insert into the dictionary	final ByteArray emptyBA = new ByteArray() ;	ByteArray w=emptyBA ;		// Constructor gets the number of bits to be written for each code	public TcuLZW()	{		numOfBits = 12 ;			// Create a new Limited Dictionary			// For maximum of 2^bits entries		dict = new LimitedDict(1<<numOfBits) ;			// Add all ascii characters to the dictionary		for (int i=0;i<256;++i)			dict.add(new ByteArray((byte)i)) ;	}		// Encodes the next character.		// If there is a code generated returns it.		// If not returns -1.	int encodeOneChar(int n) {		byte c = (byte) n ;		ByteArray nw = w.conc(c) ;		int code = dict.numFromStr(nw) ;			// if it exists then we continue to search for a longer string		if (code!=-1) {			w = nw ;			return -1 ;		} else {			dict.add(nw) ;			nw = w ;			w = new ByteArray(c) ;			return dict.numFromStr(nw) ;		}	}		// If there is something left in w, returns its code	int encodeLast() {		ByteArray nw = w ;		w = emptyBA ;		return dict.numFromStr(nw) ;	}		// Write the code in bits into output stream	void writeCode(OutputStream os, int code) throws IOException	{		for (int i=0;i<numOfBits;++i) {			os.write(code&1) ;			code /= 2 ;		}	}	int readCode(InputStream is) throws IOException	{		int num = 0 ;		for (int i=0;i<numOfBits;++i) {			int next = is.read() ;			if (next<0)				return -1 ;			num += next<<i ;		}		return num ;	}	public void compress(InputStream is, OutputStream os) throws IOException {		os = new BitOutputStream(os) ;		int next ;	// next input character		int code ;	// next code generated		while ((next=is.read())>=0) {			if (stopped)				break ;			code = encodeOneChar(next) ;			if (code>=0)				writeCode(os,code) ;		}		code = encodeLast() ;		if (code>=0)			writeCode(os,code) ;    os.flush() ;	}	ByteArray decodeOne(int code) {			// Either "ABA" or null, w="AB"		ByteArray str = dict.strFromNum(code) ;		if (str==null) {			str = w.conc(w.getAt(0)) ;			dict.add(str) ;		} else			if (! w.isEmpty())				dict.add(w.conc(str.getAt(0))) ;            w = str ;		return w ;	}	public void decompress(InputStream is, OutputStream os) throws IOException {		is = new BitInputStream(is) ;		ByteArray str ;	// Next entry		int code ;		// Next code to be read		while ((code=readCode(is))>=0) {			if (stopped)				break ;			str = decodeOne(code) ;			os.write(str.getBytes()) ;		}	}	public void stop()		{ stopped = true ; }		static final int BUFFER = 2048;	public static void main(String[] args) throws IOException {		// TODO Auto-generated method stub		/*TcuLZW ld = new TcuLZW();		FileInputStream file = new FileInputStream("c:\\dd.xml");		FileOutputStream file1 = new FileOutputStream("c:\\dx.lz");		ld.compress(file,file1);		*/		/*		try{            BufferedReader in=new BufferedReader(                                new InputStreamReader(new FileInputStream("c:\\dd.xml"),"ISO8859_1"));            FileOutputStream f=new FileOutputStream("c:\\dx.zip");            CheckedOutputStream ch=new CheckedOutputStream(f,new CRC32());            ZipOutputStream out=new ZipOutputStream(                                   new BufferedOutputStream(ch));                int c;           out.putNextEntry(new ZipEntry("dd.xml"));           while((c=in.read())!=-1)               out.write(c);             in.close();             out.close();           }        catch(Exception e){            e.printStackTrace();           }       */        /* try {        	BufferedOutputStream dest = null;        	BufferedInputStream is = null;        	ZipEntry entry;        	ZipFile zipfile = new ZipFile("c:\\dx.zip");        	Enumeration e = zipfile.entries();        	while(e.hasMoreElements()) {        	entry = (ZipEntry) e.nextElement();        	System.out.println("Extracting: " +entry);        	is = new BufferedInputStream        	(zipfile.getInputStream(entry));        	int count;        	byte data[] = new byte[BUFFER];        	FileOutputStream fos = new         	FileOutputStream("c:\\" + entry.getName());        	dest = new         	BufferedOutputStream(fos, BUFFER);        	while ((count = is.read(data, 0, BUFFER))         	!= -1) {        	dest.write(data, 0, count);        	}        	dest.flush();        	dest.close();        	is.close();        	}        	} catch(Exception e) {        	e.printStackTrace();        	}       */		try 		{ //		打开需压缩文件作为文件输入流 		FileInputStream fin=new FileInputStream("c:\\dd.xml"); //		建立压缩文件输出流 		FileOutputStream fout=new FileOutputStream("c:\\dd1.zip"); //		建立gzip压缩输出流 		GZIPOutputStream gzout=new GZIPOutputStream(fout); 		byte[] buf=new byte[1024];//设定读入缓冲区尺寸 		int num; 		 		while ((num=fin.read(buf)) != -1) 		{ 		gzout.write(buf,0,num); 		} 		gzout.close();//!!!关闭流,必须关闭所有输入输出流.保证输入输出完整和释放系统资源.		 		fout.close(); 		fin.close(); 		}catch(IOException e) 		{ 		System.out.println(e); 		} 		/*		try 		{ //		建立gzip压缩文件输入流 		FileInputStream fin=new FileInputStream("c:\\ddd1.zip"); //		建立gzip解压工作流 		GZIPInputStream gzin=new GZIPInputStream(fin); //		建立解压文件输出流 		FileOutputStream fout=new FileOutputStream("c:\\ddd122.xml"); 		byte[] buf=new byte[1024]; 		int num; 		 		while ((num=gzin.read(buf,0,buf.length)) != -1) 		{ 		fout.write(buf,0,num); 		} 		gzin.close(); 		fout.close(); 		fin.close(); 		}catch(IOException e) 		{ 		System.out.println(e); 		} 		*/        	   }		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -