📄 decompress.java~1~
字号:
package compress;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.ObjectInputStream;
import java.io.RandomAccessFile;
import java.util.Calendar;
public class DeCompress extends Thread{
/**
* @param args
*/
//Test testor;
String codeChar;
String code;
String text;
String remain;
String textFile,codeFile;
public long filelength;
public double currentlength;
// public static void main(String[] args) {
// // TODO Auto-generated method stub
// DeCompress decomer=new DeCompress();
// Calendar cal=Calendar.getInstance();
// System.out.println("Start Time: "+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE)+":"+cal.get(Calendar.SECOND)+":"+cal.get(Calendar.MILLISECOND));
//
// decomer.deCompress("e://test.dat", "e://testresult.txt");
// cal=Calendar.getInstance();
// System.out.println("End Time: "+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE)+":"+cal.get(Calendar.SECOND)+":"+cal.get(Calendar.MILLISECOND));
// }
public DeCompress(){
this.initialize();
}
public DeCompress(String codeFile,String textFile){
this();
this.setFile(codeFile, textFile);
}
public void setFile(String codeFile,String textFile){
this.codeFile=codeFile;
this.textFile=textFile;
}
void initialize(){
codeChar=code=text=remain="";
}
/*
* 把String 串转化为01代码
*
*/
String ConvertCharToBits(String chars){
String result="";
for(int i=0;i<chars.length();i++){
int number=(int)chars.charAt(i);
//covernt number to bits
while(number!=0){
//对number除2取余,如果余数是1,就在result前 添加1,如果余数是零就在result前添加0
if(number%2==0)
result="0"+result;
else //number%2==1
result="1"+result;
number=number/2;
}
number=result.length()%16;
if(number!=0){
for(int t=0;t<16-number;t++)
result="0"+result;
}
}
return result;
}
String ConvertCharToBits(char cha){
String result="";
int count=0;
//先把char转化为整数
int number=(int)cha;
//System.out.print(number+" ");
//把整数通过取模运算转化成01代码
while(number!=0){
if(number%2==0)
result="0"+result;
else //number%2==1
result="1"+result;
number=number/2;
}
count=result.length();
if(count<16){
for(int i=0;i<16-count;i++)
result="0"+result;
}
return result;
}
String ConvertIntToBits(int number){
String result="";
int count=0;
//先把char转化为整数
//System.out.print(number+" ");
//把整数通过取模运算转化成01代码
while(number!=0){
if(number%2==0)
result="0"+result;
else //number%2==1
result="1"+result;
number=number/2;
}
count=result.length();
if(count<16){
for(int i=0;i<16-count;i++)
result="0"+result;
}
return result;
}
String ConvertByteToBits(byte num){
String result="";
int count=0;
//先把char转化为整数
int number=num;
//System.out.print(number+" ");
//把整数通过取模运算转化成01代码
while(number!=0){
if(number%2==0)
result="0"+result;
else //number%2==1
result="1"+result;
number=number/2;
}
count=result.length();
if(count<8){
for(int i=0;i<8-count;i++)
result="0"+result;
}
return result;
}
String decode(String codes, CodeNode[] map) {
//根据字符映射表把原文件解压缩
int p = 0;
String result = "";
codes=remain+codes;
for (int i = 0, n = 0; i <= codes.length() && n <= codes.length(); n++)
{
String temp = codes.substring(i, n);
for (int j = 0; j < map.length; j++) {
if (map[j].code.equals(temp)) { //将结果字符保存在char数组res中
result += map[j].cha;
p++;
i = n; //改向搜索下一个编码
}
}
remain=codes.substring(i,codes.length());
}
return result;
}
public void deCompress(String codeFile,String textFile){
File cFile = new File(textFile);
if(cFile.exists())
{//删除重名文件
cFile.delete();
}
PrintWriter writer = null;
CodeNode[] map;//字符映射表
char cha;
String temp="";
FileInputStream input;//文件流
DataInputStream in ;
ObjectInputStream objectReader;//读对象
RandomAccessFile randomReader;
String[] lines;
try{
input=new FileInputStream(codeFile);//建立读文件的流
in = new DataInputStream(input);//读字符数据
objectReader=new ObjectInputStream (input);//建立度对象的流
randomReader=new RandomAccessFile(codeFile,"rw");//建立随机读取流
filelength=randomReader.length(); //获得文件长度
randomReader.seek(filelength-4); //定位到最后的int 值之前
int leafsLength=randomReader.readInt();//读取对象个数
// System.out.println(leafsLength);
randomReader.seek(filelength-8);
int actualBitsLength=randomReader.readInt();//读取最后一串01的实际长度
//System.out.println(actualBitsLength);
randomReader.close();//到此为止,已经不再需要randomReader类,close it
// System.out.println("Codes: "+leafsLength);
map=new CodeNode[leafsLength];
for(int i=0;i<map.length;i++){
map[i]=(CodeNode)objectReader.readObject();//读取字符映射表
}
writer = new PrintWriter(new FileOutputStream(textFile,true));
while(currentlength<this.filelength-10){
//10=最后一个字符:2+ 最后一个01串的实际长度(int):4+对象的数目(int):4
this.currentlength+=2;//当前已经处理了多少
cha=in.readChar();
//if(cha==-1) break;
this.code=this.ConvertCharToBits(cha);//把字符转化为01
this.text=this.decode(this.code, map);//把01解压为文本
text=temp+text;
lines=this.text.split("\n");
if(text.endsWith("\n")){
for(int i=0;i<lines.length;i++)
writer.println(lines[i]);
temp="";
}
else {
// System.out.println("Codes below this line must throw IOException");
for(int i=0;i<lines.length-1;i++){
writer.println(lines[i]);
//System.out.println(i+" "+lines[i]);
}
temp=lines[lines.length-1];//保存上次未写完的字符串
}
}
// System.out.println("Codes below this line must throw IOException");
String lastBits="";
if(actualBitsLength>0){
//如果有剩余的01串,那么读出来
char lastOne=in.readChar();
lastBits=this.ConvertCharToBits(lastOne);
lastBits=lastBits.substring(16-actualBitsLength,16);//得到最后一串01
}
lastBits=remain+lastBits;//在前面添加上次没处理的01
if(lastBits.length()>0){
this.text=this.decode(lastBits, map);//得到最后的文本
text=temp+text;//把上次未写入文件的字符串写入文件
writer.println(text);}
input.close(); //close FileInput Stream
}
catch(IOException io){
System.out.println(io.getLocalizedMessage());
// System.out.println(io.getMessage());
}
catch(ClassNotFoundException cnf){
System.err.println("Can't Find the class! "+cnf.getLocalizedMessage());
}
finally{
//this.currentlength+=10;
this.currentlength=this.filelength;
writer.close();
}
}
public void run(){//重写run方法
if(this.textFile==null||this.codeFile==null){
System.err.println("Can't get the filename");
}
else {
this.deCompress(codeFile, textFile);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -