Java语言实现文本文档压缩Word文档下载推荐.docx

上传人:b****5 文档编号:19974413 上传时间:2023-01-13 格式:DOCX 页数:23 大小:20.49KB
下载 相关 举报
Java语言实现文本文档压缩Word文档下载推荐.docx_第1页
第1页 / 共23页
Java语言实现文本文档压缩Word文档下载推荐.docx_第2页
第2页 / 共23页
Java语言实现文本文档压缩Word文档下载推荐.docx_第3页
第3页 / 共23页
Java语言实现文本文档压缩Word文档下载推荐.docx_第4页
第4页 / 共23页
Java语言实现文本文档压缩Word文档下载推荐.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

Java语言实现文本文档压缩Word文档下载推荐.docx

《Java语言实现文本文档压缩Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《Java语言实现文本文档压缩Word文档下载推荐.docx(23页珍藏版)》请在冰豆网上搜索。

Java语言实现文本文档压缩Word文档下载推荐.docx

shezhi2ssssss;

publicjie()

{

con=this.getContentPane();

chushi();

this.setTitle("

文本文件压缩"

);

this.setSize(700,400);

this.setVisible(true);

this.addWindowListener(this);

publicvoidchushi()

JPanelpanel=newJPanel();

panel.setLayout(null);

label=newJButton();

label.setEnabled(false);

//button不能点击

label.setBounds(0,0,700,100);

label.setBackground(Color.BLUE);

panel.add(label);

JButtonlab=newJButton("

选择你要压缩或者解压的文件"

lab.setEnabled(false);

lab.setBounds(0,100,300,100);

//label.setBackground(Color.WHITE);

panel.add(lab);

lab=newJButton("

选择保存路径"

lab.setBounds(0,200,300,100);

//label.setBackground(Color.WHITE);

text=newJTextArea();

text.setFont(newFont("

黑体"

Font.BOLD,32));

text.setForeground(Color.RED);

scroll=newJScrollPane(text);

scroll.setBounds(300,100,300,80);

panel.add(scroll);

text2=newJTextArea();

text2.setFont(newFont("

text2.setForeground(Color.RED);

scroll=newJScrollPane(text2);

scroll.setBounds(300,200,300,80);

JButtonbutton=newJButton("

压缩"

button.addActionListener(this);

button.setBounds(300,300,100,50);

panel.add(button);

button=newJButton("

解压"

button.setBounds(400,300,100,50);

panel.add(button);

浏览"

button.setBounds(600,100,100,80);

button=newJButton("

浏览2"

button.setBounds(600,200,100,80);

con.add(panel);

publicvoidwindowActivated(WindowEventarg0){

//TODOAuto-generatedmethodstub

publicvoidwindowClosed(WindowEventarg0){

publicvoidwindowClosing(WindowEventarg0){

System.exit(0);

publicvoidwindowDeactivated(WindowEventarg0){

publicvoidwindowDeiconified(WindowEventarg0){

publicvoidwindowIconified(WindowEventarg0){

publicvoidwindowOpened(WindowEventarg0){

publicvoidactionPerformed(ActionEvente){

Stringmsg=e.getActionCommand();

if(msg.equals("

))

{

sssss=newshezhi();

text.setText(sssss.fan);

return;

}

ssssss=newshezhi2();

intlength=sssss.fan.length();

intg;

charr;

Stringf="

"

;

if(sssss.fan.charAt(length-1)=='

t'

{

for(g=length-5;

g>

0;

g--)

{f=sssss.fan.charAt(g)+f;

if(sssss.fan.charAt(g)=='

\\'

{

text2.setText(ssssss.fan+"

\\"

+f);

break;

}

}

}

else

if(sssss.fan.charAt(length-1)=='

l'

{

for(g=length-5;

{f=sssss.fan.charAt(g)+f;

if(sssss.fan.charAt(g)=='

{

text2.setText(ssssss.fan+"

+f);

break;

}

}

Strings=text.getText();

Strings2=text2.getText();

Filefile=newFile(s);

if(!

file.exists())//判断文件是否存在

label.setText("

文件不存在"

intn=s.length();

Stringzhui=s.substring(n-4,n);

if(!

zhui.equals("

.txt"

))//判断是否为文本文件

label.setText("

不是文本文件"

return;

if(newYa().yasuo(file,s2))//将文件压缩

Filefile2=newFile(s2+"

.lll"

doublelv=((double)file2.length())/file.length()*100;

压缩成功"

+"

压缩率"

+lv+"

%"

压缩失败"

))//判断是否为压缩文件

不是压缩文件"

if(newYa().jieya(file,s2))//解压

解压成功"

解压失败"

classshezhiextendsJFrame{

Containera;

Stringfan=null;

JFileChooserchoose;

publicshezhi()

{a=getContentPane();

chooser();

a.add(choose);

publicvoidchooser()

{

choose=newJFileChooser();

//choose.setFileSelectionMode(choose.);

intresult=choose.showOpenDialog(this);

if(result==JFileChooser.APPROVE_OPTION)

fan=choose.getSelectedFile().getPath();

classshezhi2extendsJFrame{

publicshezhi2()

choose.setFileSelectionMode(choose.DIRECTORIES_ONLY);

publicclassMinheap{

TreeNodea[];

intlength;

publicMinheap(TreeNodezu[],intn)

a=zu;

length=n;

publicTreeNodedel()//删除最小节点

if(length==0)

returnnull;

TreeNodedata=a[0];

length--;

a[0]=a[length];

inti=1;

intci=2;

TreeNoder;

while(2*i<

=length)

r=a[ci-1];

if((ci<

length)&

&

(a[ci].data.num<

r.data.num))

r=a[ci];

ci++;

if(a[i-1].data.num<

=r.data.num)

break;

a[ci-1]=a[i-1];

a[i-1]=r;

i=ci;

ci=ci*2;

returndata;

publicvoidintilize()//最小堆的初始化

inti=length/2;

while(i>

=1)

TreeNodey=a[i-1];

intci=i*2;

while(ci<

if((ci<

(a[ci-1].data.num>

a[ci].data.num))

ci++;

if(y.data.num<

=a[ci-1].data.num)

break;

a[ci/2-1]=a[ci-1];

ci=ci*2;

a[ci/2-1]=y;

i--;

publicvoidins(TreeNodedata)//增加节点

length++;

inti=length;

while((i>

1)&

(a[i/2-1].data.num>

data.data.num))

a[i-1]=a[i/2-1];

i=i/2;

a[i-1]=data;

publicclassTreeNode{

Datadata;

//数据成员,为Data类型的

TreeNodel;

//左孩子

TreeNoder;

//右孩子

importjava.io.BufferedReader;

importjava.io.FileInputStream;

importjava.io.FileNotFoundException;

importjava.io.FileOutputStream;

importjava.io.FileReader;

importjava.io.IOException;

importjava.io.InputStreamReader;

importjava.io.ObjectInputStream;

importjava.io.ObjectOutputStream;

importjava.io.OutputStream;

importjava.io.PrintWriter;

publicclassYa{

Data[]biao;

intk;

Stringma;

Stringyuan;

Stringmudi;

publicbooleanyasuo(Filefile,Strings2)

{//读取文件内容

mudi=s2;

BufferedReaderin;

Stringwenben="

try{

in=newBufferedReader(newFileReader(file));

Strings=in.readLine();

while(s!

=null)

wenben=wenben+s+"

\r"

\n"

//每次读取的是一行,所以要加上回车和换行符,来表示回车和换行

s=in.readLine();

yuan=wenben;

tongji(wenben);

in.close();

}

catch(Exceptione){

//TODOAuto-generatedcatchblock

returnfalse;

returntrue;

publicvoidtongji(Strings)throwsException

intlen=s.length();

Data[]zu=newData[len];

intindex=0;

loop:

for(inti=0;

i<

s.length();

i++)

charr=s.charAt(i);

for(intj=0;

j<

index;

j++)

if(r==zu[j].sign)//如果以前出现过就在那个的次数上加1

zu[j].num++;

continueloop;

zu[index]=newData();

zu[index].sign=r;

zu[index].num=1;

index++;

/*for(inti=0;

System.out.println(zu[i].sign+"

+zu[i].num);

}*/

huffman(zu,index);

//霍夫曼树的构造

publicvoidhuffman(Datazu[],intn)throwsException

TreeNode[]shu=newTreeNode[n];

for(inti=0;

n;

shu[i]=newTreeNode();

shu[i].data=zu[i];

shu[i].l=shu[i].r=null;

Minheapheap=newMinheap(shu,n);

//建立最小堆

heap.intilize();

//最小堆初始化

for(intj=n;

j>

1;

j--)//霍夫曼树构造的方法

TreeNodex,y;

x=heap.del();

y=heap.del();

TreeNodez=newTreeNode();

z.data=newData();

z.data.num=x.data.num+y.data.num;

z.l=x;

z.r=y;

heap.ins(z);

TreeNodetree=heap.del();

ma="

biao=newData[n];

k=0;

hout(tree);

//霍夫曼编码

suo();

publicvoidhout(TreeNodetree)

{//得到霍夫曼编码

if(tree.l!

ma=ma+"

0"

hout(tree.l);

1"

hout(tree.r);

else

{

biao[k]=newData();

biao[k].sign=tree.data.sign;

biao[k].ma=ma;

k++;

intl=ma.length();

if(l!

=0)

ma=ma.substring(0,l-1);

publicvoidsuo()throwsException

biao.length;

System.out.println(biao[i].sign+"

:

+biao[i].ma);

Stringmu="

yuan.length();

i++)//对着编码将文本变成用霍夫曼编码的01串,最后01串保存在mu中

charr=yuan.charAt(i);

loop1:

for(intj=0;

if(r==biao[j].sign)

mu=mu+biao[j].ma;

breakloop1;

//System.out.println(mu);

mudi=mudi+"

ObjectOutputStreamout=n

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 设计艺术

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1