利用Java实现zip压缩/解压缩
由于网络带宽有限,所以数据文件的压缩有利于数据在Internet上的快速传输,同时也节省服务器的外存空间。 字串1
Java 1.1实现了I/O数据流与网络数据流的单一接口,因此数据的压缩、网络传输和解 压缩的实现比较容易,下面介绍利用ZipEntry、ZipInputStream和ZipOutputStream三个Java类实现zip数据压缩方式的编程方法。
zip压缩文件结构:一个zip文件由多个entry组成,每个entry有一个唯一的名称,entry的数据项存储压缩数据。 字串8
与zip文件有关的几个Java类
字串6
·类ZipEntry
public ZipEntry(String name); 字串4
name为指定的数据项名。 字串3
·类ZipOutputStream 字串4
ZipOutputStream实现了zip压缩文件的写输出流,支持压缩和非压缩entry。下面是它的几个函数:
public ZipOutputStream(OutputStream out);
∥利用输出流out构造一个ZIP输出流。 字串2
public void setMethod(int method);
字串4
∥设置entry压缩方法,缺省值为DEFLATED。 字串4
public void putNextEntry(ZipEntry newe);
∥如果当前的entry存在且处于激活状态时,关闭它,在zip文件中写入新的entry-newe 并将数据流定位于entry数据项的起始位置,压缩方法为setMethod指定的方法。
字串1
·类ZipInputStream
ZipInputStream实现了zip压缩文件的读输入流,支持压缩和非压缩entry。下面是它的几个函数: 字串1
public ZipInputStream(InputStream in);
字串2
∥利用输入流in构造一个ZIP输出流。
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.*;
import java.lang.Math;
字串8
import java.io.*;
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);
Menu file = new Menu("File",true);
menubar.add(file);
MenuItem neww= new MenuItem("New"); 字串2
neww.addActionListener(this);
file.add(neww);
MenuItem open=new MenuItem("Open");
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());
}
public static void main(String args[]){
TestZip ok=new TestZip(); 字串8
ok.setTitle("zip sample"); 字串7
ok.setSize(600,300);
字串2
ok.show();
}
private void randomData(){
字串3
∥随机生成50个double数据,并放在doc字符串变量中。 字串7
doc="";
for(int i=1;i<51;i++){ 字串7
double rdm=Math.random()*10;
doc=doc+new Double(rdm).toString();
字串7
if(i%5 == 0) doc=doc+"\n"; 字串4
else doc=doc+" ";
}
doczipsize = 0; 字串3
showTextandInfo(); 字串1
} 字串9
private void openFile(){
字串2
∥打开zip文件,将文件内容读入doc字符串变量中。
FileDialog dlg=new FileDialog(this,"Open",FileDialog.LOA D);
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();
∥将输入流定位在当前entry数据项位置 字串4
DataInputStream dis=new DataInputStream(zipis); 字串2
∥用ZIP输入流构建DataInputStream
doc=dis.readUTF();∥读取文件内容
字串2
dis.close();∥关闭文件 字串6
doczipsize = f.length();∥获取ZIP文件长度
showTextandInfo();∥显示数据
} 字串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);
dlg.show();
字串8
String filename=dlg.getDirectory()+dlg.getFile();
try{ 字串6
∥创建一个文件实例 字串4
File f=new File(filename);
字串4
if(!f.exists()) return; ∥文件不存在,则返回 字串5
∥用文件输出流构建ZIP压缩输出流
ZipOutputStream zipos=new ZipOutputStream(new FileOutputStream(f));
字串3
zipos.setMethod(ZipOutputStream.DEFLATED); ∥设置压缩方法
zipos.putNextEntry(new ZipEntry("zip"));
∥生成一个ZIP entry,写入文件输出流中,并将输出流定位于entry起始处。 字串8
DataOutputStream os=new DataOutputStream(zipos); 字串4
∥用ZIP输出流构建DataOutputStream; 字串5
os.writeUTF(doc);∥将随机生成的数据写入文件中 字串4
os.close();∥关闭数据流
doczipsize = f.length();
∥获取压缩文件的长度 字串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());
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();∥关闭窗口
System.exit(0);∥关闭程序
字串5
}
字串7
else {
字串1
System.out.println("no this command!"); 字串9
} 字串9
} 字串3
}
开班时间-武汉北大青鸟

