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

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

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

字串5

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

与zip文件有关的几个Java类

字串6

·类ZipEntry

字串8

public ZipEntry(String name); 字串4

name为指定的数据项名。 字串3

·类ZipOutputStream 字串4

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

字串1

public ZipOutputStream(OutputStream out);

字串4

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

public void setMethod(int method);

字串4

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

public void putNextEntry(ZipEntry newe);

字串8

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

字串1

·类ZipInputStream

字串2

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

public ZipInputStream(InputStream in);

字串2

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

字串1

public ZipEntry getNextEntry();

字串6

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

public void closeEntry(); 字串1

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

程序代码及其注释 字串5

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

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

import java.awt.event.*;

字串1

import java.awt.*;

字串3

import java.lang.Math;

字串8

import java.io.*;

字串7

public class TestZip extends Frame implements ActionListener { 字串9

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

字串8

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

字串1

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

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

字串5

public TestZip(){ 字串7

∥生成菜单 字串1

MenuBar menubar = new MenuBar();

字串1

setMenuBar(menubar);

字串7

Menu file = new Menu("File",true);

字串9

menubar.add(file);

字串8

MenuItem neww= new MenuItem("New"); 字串2

neww.addActionListener(this);

字串4

file.add(neww);

字串6

MenuItem open=new MenuItem("Open");

字串9

open.addActionListener(this); 字串9

file.add(open);

字串9

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

save.addActionListener(this); 字串5

file.add(save); 字串6

MenuItem exit=new MenuItem("Exit"); 字串4

exit.addActionListener(this); 字串8

file.add(exit); 字串8

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

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

字串8

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

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

字串2

}

字串1

public static void main(String args[]){

字串5

TestZip ok=new TestZip(); 字串8

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

ok.setSize(600,300);

字串2

ok.show();

字串4

}

字串4

private void randomData(){

字串3

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

doc="";

字串7

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

double rdm=Math.random()*10;

字串9

doc=doc+new Double(rdm).toString();

字串7

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

else doc=doc+" ";

字串6

}

字串7

doczipsize = 0; 字串3

showTextandInfo(); 字串1

} 字串9

private void openFile(){

字串2

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

字串6

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

字串3

dlg.show(); 字串7

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

try{ 字串5

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

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

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

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

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

zipis.getNextEntry();

字串6

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

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

∥用ZIP输入流构建DataInputStream

字串8

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

字串2

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

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

字串6

showTextandInfo();∥显示数据

字串8

} 字串7

catch(IOException ioe){ 字串4

System.out.println(ioe);

字串2

} 字串6

} 字串4

private void saveFile(){

字串3

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

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

字串1

dlg.show();

字串8

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

字串7

try{ 字串6

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

File f=new File(filename);

字串4

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

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

字串9

ZipOutputStream zipos=new ZipOutputStream(new FileOutputStream(f));

字串3

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

字串5

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

字串9

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

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

∥用ZIP输出流构建DataOutputStream; 字串5

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

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

字串5

doczipsize = f.length();

字串4

∥获取压缩文件的长度 字串5

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

}

字串8

catch(IOException ioe){ 字串6

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

} 字串4

}

字串4

private void showTextandInfo(){

字串3

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

textarea.replaceRange(doc,0,textarea.getText().length());

字串9

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

}

字串8

public void actionPerformed(ActionEvent e){ 字串2

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

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

字串3

else if ("Open".equals(arg)) openFile(); 字串9

else if ("Save".equals(arg)) saveFile();

字串8

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

dispose();∥关闭窗口

字串4

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

字串5

}

字串7

else {

字串1

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

} 字串9

} 字串3

}

字串5

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

开班时间-武汉北大青鸟

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