利用Java实现zip压缩/解压缩

  由于网络带宽有限,所以数据文件的压缩有利于数据在Internet上的快速传输,同时也节省服务器的外存空间。 字串8

  Java 1.1实现了I/O数据流与网络数据流的单一接口,因此数据的压缩、网络传输和解 压缩的实现比较容易,下面介绍利用ZipEntry、ZipInputStream和ZipOutputStream三个Java类实现zip数据压缩方式的编程方法。

字串7

  zip压缩文件结构:一个zip文件由多个entry组成,每个entry有一个唯一的名称,entry的数据项存储压缩数据。

字串1

与zip文件有关的几个Java类

字串9

·类ZipEntry 字串4

public ZipEntry(String name); 字串1

name为指定的数据项名。

字串9

·类ZipOutputStream

字串2

ZipOutputStream实现了zip压缩文件的写输出流,支持压缩和非压缩entry。下面是它的几个函数: 字串1

public ZipOutputStream(OutputStream out);

字串2

∥利用输出流out构造一个ZIP输出流。 字串3

public void setMethod(int method); 字串2

∥设置entry压缩方法,缺省值为DEFLATED。

字串1

public void putNextEntry(ZipEntry newe);

字串1

∥如果当前的entry存在且处于激活状态时,关闭它,在zip文件中写入新的entry-newe 并将数据流定位于entry数据项的起始位置,压缩方法为setMethod指定的方法。 字串1

·类ZipInputStream

字串5

ZipInputStream实现了zip压缩文件的读输入流,支持压缩和非压缩entry。下面是它的几个函数: 字串8

public ZipInputStream(InputStream in); 字串2

∥利用输入流in构造一个ZIP输出流。 字串7

public ZipEntry getNextEntry(); 字串5

∥返回ZIP文件中的下一个entry,并将输出流定位在此entry数据项的起始位置。 字串6

public void closeEntry();

字串8

∥关闭当前的zip entry,并将数据流定位于下一个entry的起始位置。

字串7

程序代码及其注释 字串1

下列的程序实现了数据文件zip方式的压缩和解压缩方法。randomData()函数随机生成 50个double数据,并放在doc字符串变量中;openFile()函数读取ZIP压缩文件;saveFile()函数 将随机生成的数据存到ZIP格式的压缩文件中。

字串6

import java.util.zip.*; 字串7

import java.awt.event.*;

字串3

import java.awt.*;

字串1

import java.lang.Math; 字串3

import java.io.*; 字串1

public class TestZip extends Frame implements ActionListener { 字串5

TextArea textarea; ∥显示数据文件的多行文本显示域

字串5

TextField infotip; ∥显示数据文件未压缩大小及压缩大小单行文本显示域

字串1

String doc; ∥存储随机生成的数据

字串1

long doczipsize = 0;∥压缩数据文件的大小 字串5

public TestZip(){

字串8

∥生成菜单 字串5

MenuBar menubar = new MenuBar();

字串8

setMenuBar(menubar);

字串7

Menu file = new Menu("File",true); 字串4

menubar.add(file); 字串4

MenuItem neww= new MenuItem("New");

字串1

neww.addActionListener(this); 字串1

file.add(neww); 字串9

MenuItem open=new MenuItem("Open"); 字串7

open.addActionListener(this); 字串2

file.add(open); 字串6

MenuItem save=new MenuItem("Save"); 字串8

save.addActionListener(this); 字串6

file.add(save); 字串8

MenuItem exit=new MenuItem("Exit");

字串1

exit.addActionListener(this);

字串9

file.add(exit);

字串4

∥随机生成的数据文件的多行文本显示域

字串9

add("Center",textarea = new TextArea());

字串9

∥提示文本原始大小、压缩大小的单行文本显示域

字串8

add("South",infotip = new TextField());

字串5

} 字串7

public static void main(String args[]){

字串2

TestZip ok=new TestZip();

字串5

ok.setTitle("zip sample"); 字串4

ok.setSize(600,300); 字串1

ok.show();

字串8

}

字串9

private void randomData(){

字串2

∥随机生成50个double数据,并放在doc字符串变量中。

字串2

doc="";

字串6

for(int i=1;i<51;i++){ 字串2

double rdm=Math.random()*10; 字串2

doc=doc+new Double(rdm).toString(); 字串5

if(i%5 == 0) doc=doc+"\n";

字串7

else doc=doc+" ";

字串6

}

字串8

doczipsize = 0; 字串2

showTextandInfo();

字串1

} 字串7

private void openFile(){ 字串8

∥打开zip文件,将文件内容读入doc字符串变量中。

字串5

FileDialog dlg=new FileDialog(this,"Open",FileDialog.LOA D); 字串7

dlg.show(); 字串4

String filename=dlg.getDirectory()+dlg.getFile(); 字串6

try{ 字串9

∥创建一个文件实例 字串1

File f=new File(filename);

字串9

if(!f.exists()) return; ∥文件不存在,则返回 字串1

∥用文件输入流构建ZIP压缩输入流

字串1

ZipInputStream zipis=new ZipInputStream(new FileInputStream(f)); 字串2

zipis.getNextEntry(); 字串2

∥将输入流定位在当前entry数据项位置

字串5

DataInputStream dis=new DataInputStream(zipis); 字串2

∥用ZIP输入流构建DataInputStream 字串7

doc=dis.readUTF();∥读取文件内容

字串1

dis.close();∥关闭文件 字串3

doczipsize = f.length();∥获取ZIP文件长度 字串7

showTextandInfo();∥显示数据

字串4

} 字串4

catch(IOException ioe){

字串8

System.out.println(ioe);

字串3

}

字串8

} 字串9

private void saveFile(){

字串4

∥打开zip文件,将doc字符串变量写入zip文件中。

字串2

FileDialog dlg=new FileDialog(this,"Save",FileDialog.SAVE);

字串1

dlg.show();

字串4

String filename=dlg.getDirectory()+dlg.getFile();

字串9

try{

字串9

∥创建一个文件实例

字串4

File f=new File(filename); 字串3

if(!f.exists()) return; ∥文件不存在,则返回

字串7

∥用文件输出流构建ZIP压缩输出流 字串5

ZipOutputStream zipos=new ZipOutputStream(new FileOutputStream(f)); 字串5

zipos.setMethod(ZipOutputStream.DEFLATED); ∥设置压缩方法

字串7

zipos.putNextEntry(new ZipEntry("zip"));

字串9

∥生成一个ZIP entry,写入文件输出流中,并将输出流定位于entry起始处。 字串9

DataOutputStream os=new DataOutputStream(zipos); 字串1

∥用ZIP输出流构建DataOutputStream;

字串7

os.writeUTF(doc);∥将随机生成的数据写入文件中 字串5

os.close();∥关闭数据流 字串1

doczipsize = f.length(); 字串9

∥获取压缩文件的长度

字串7

showTextandInfo();∥显示数据 字串2

}

字串3

catch(IOException ioe){

字串8

System.out.println(ioe); 字串4

} 字串6

} 字串3

private void showTextandInfo(){ 字串7

∥显示数据文件和压缩信息

字串6

textarea.replaceRange(doc,0,textarea.getText().length()); 字串6

infotip.setText("uncompressed size: "+doc.length()+"compressed size: "+dc zipsize);

字串7

} 字串9

public void actionPerformed(ActionEvent e){

字串6

String arg = e.getActionCommand(); 字串6

if ("New".equals(arg)) randomData();

字串7

else if ("Open".equals(arg)) openFile();

字串4

else if ("Save".equals(arg)) saveFile(); 字串7

else if ("Exit".equals(arg)){ 字串2

dispose();∥关闭窗口 字串4

System.exit(0);∥关闭程序

字串8

} 字串8

else { 字串7

System.out.println("no this command!"); 字串1

} 字串1

}

字串5

}
字串4

作者: 武汉北大青鸟
原载: 武汉北大青鸟鲁广校区 whhpaccp.com
版权所有,转载时必须以链接形式注明作者和原始出处及本声明
高中生入口-武汉北大青鸟
大学生入口-武汉北大青鸟
三校生入口-武汉北大青鸟
去名企入口-武汉北大青鸟

开班时间-武汉北大青鸟

武汉北大青鸟报名咨询
企业人才预定热线
全国免费咨询电话
就业服务电话
教学质量监督
武汉北大青鸟地址
邮编
027-87807737 / 87807787
027-87807736
800-880-0456(电信用户)
027-87807717 李老师
027-87807727 吕老师
武汉洪山区鲁巷广场武汉数码港3楼(鲁巷广场隔壁)
430074