Thread View: cn.bbs.comp.program
1 messages
1 total messages
Started by ���ܵ�Сʨ
Thu, 07 May 1998 00:00
����Javaʵ��zipѹ��/��ѹ��
Author: ���ܵ�Сʨ
Date: Thu, 07 May 1998 00:00
Date: Thu, 07 May 1998 00:00
181 lines
8658 bytes
8658 bytes
��������������,��������ļ���ѹ�������������Internet��
�Ŀ��ٴ���,ͬʱҲ��ʡ�����������ռ䡣
Java 1.1ʵ����I/O�����������������ĵ�һ�ӿ�,�����ݵ�
ѹ�������紫��ͽ�ѹ����ʵ�ֱȽ�����,�����������ZipEntry��Z
ipInputStream��ZipOutputStream���Java��ʵ��zip���ѹ����ʽ
�ı�̷�����
zipѹ���ļ��ṹ:һ��zip�ļ��ɶ��entry���,ÿ��entry��һ
��Ψһ�����,entry�������洢ѹ����ݡ�
��zip�ļ��йصļ���Java��
����ZipEntry
public ZipEntry(String name);
nameΪָ�����������
����ZipOutputStream
ZipOutputStreamʵ����zipѹ���ļ���д�����,֧��ѹ���ͷ�
ѹ��entry����������ļ�������:
public ZipOutputStream(OutputStream out);
�����������out����һ��ZIP�������
public void setMethod(int method);
������entryѹ������,ȱʡֵΪDEFLATED��
public void putNextEntry(ZipEntry newe);
�����ǰ��entry�����Ҵ��ڼ���״̬ʱ,�ر���,��zip�ļ���
д���µ�entry-newe�����������λ��entry��������ʼλ��,ѹ��
����ΪsetMethodָ���ķ�����
����ZipInputStream
ZipInputStreamʵ����zipѹ���ļ��Ķ�������,֧��ѹ���ͷ�ѹ
��entry����������������:
public ZipInputStream(InputStream in);
������������in����һ��ZIP�������
public ZipEntry getNextEntry();
�η���ZIP�ļ��е���һ��entry,�����������λ�ڴ�entry���
�����ʼλ�á�
public void closeEntry();
�ιرյ�ǰ��zip entry,�����������λ����һ��entry����ʼ
λ�á�
������뼰��ע��
���еij���ʵ��������ļ�zip��ʽ��ѹ���ͽ�ѹ��������rando
mData()����������50��double���,������doc�ַ������;open
File()�����ȡZIPѹ���ļ�;saveFile()���������ɵ���ݴ浽
ZIP��ʽ��ѹ���ļ��С�
import java.util.zip.*;
import java.awt.event.*;
import java.awt.*;
import java.lang.Math;
import java.io.*;
public class TestZip extends Frame implements ActionList
ener {
TextArea textarea; ����ʾ����ļ��Ķ����ı���ʾ��
TextField infotip; ����ʾ����ļ�δѹ����С��ѹ����С��
���ı���ʾ��
String doc; �δ洢�����ɵ����
long doczipsize = 0;��ѹ������ļ��Ĵ�С
public TestZip(){
����ɲ˵�
MenuBar menubar = new MenuBar();
setMenuBar(menubar);
Menu file = new Menu("File",true);
menubar.add(file);
MenuItem neww= new MenuItem("New");
neww.addActionListener(this);
file.add(neww);
MenuItem open=new MenuItem("Open");
open.addActionListener(this);
file.add(open);
MenuItem save=new MenuItem("Save");
save.addActionListener(this);
file.add(save);
MenuItem exit=new MenuItem("Exit");
exit.addActionListener(this);
file.add(exit);
�������ɵ�����ļ��Ķ����ı���ʾ��
add("Center",textarea = new TextArea());
����ʾ�ı�ԭʼ��С��ѹ����С�ĵ����ı���ʾ��
add("South",infotip = new TextField());
}
public static void main(String args[]){
TestZip ok=new TestZip();
ok.setTitle("zip sample");
ok.setSize(600,300);
ok.show();
}
private void randomData(){
��������50��double���,������doc�ַ�����С�
doc="";
for(int i=1;i<51;i++){
double rdm=Math.random()*10;
doc=doc+new Double(rdm).toString();
if(i%5 == 0) doc=doc+"\n";
else doc=doc+" ";
}
doczipsize = 0;
showTextandInfo();
}
private void openFile(){
�δ�zip�ļ�,���ļ����ݶ���doc�ַ�����С�
FileDialog dlg=new FileDialog(this,"Open",FileDialog.LOA
D);
dlg.show();
String filename=dlg.getDirectory()+dlg.getFile();
try{
�δ���һ���ļ�ʵ��
File f=new File(filename);
if(!f.exists()) return; ���������,��
�����ļ�����������ZIPѹ��������
ZipInputStream zipis=new ZipInputStream(new FileInputStr
eam(f));
zipis.getNextEntry();
�ν���������λ�ڵ�ǰentry�����λ��
DataInputStream dis=new DataInputStream(zipis);
����ZIP����������DataInputStream
doc=dis.readUTF();�ζ�ȡ�ļ�����
dis.close();�ιر��ļ�
doczipsize = f.length();�λ�ȡZIP�ļ�����
showTextandInfo();����ʾ���
}
catch(IOException ioe){
System.out.println(ioe);
}
}
private void saveFile(){
�δ�zip�ļ�,��doc�ַ����д��zip�ļ��С�
FileDialog dlg=new FileDialog(this,"Save",FileDialog.SAV
E);
dlg.show();
String filename=dlg.getDirectory()+dlg.getFile();
try{
�δ���һ���ļ�ʵ��
File f=new File(filename);
if(!f.exists()) return; ���������,��
�����ļ����������ZIPѹ�������
ZipOutputStream zipos=new ZipOutputStream(new FileOutput
Stream(f));
zipos.setMethod(ZipOutputStream.DEFLATED); ������ѹ����
��
zipos.putNextEntry(new ZipEntry("zip"));
�����һ��ZIP entry,д���ļ��������,�����������λ��ent
ry��ʼ����
DataOutputStream os=new DataOutputStream(zipos);
����ZIP���������DataOutputStream;
os.writeUTF(doc);�ν������ɵ����д���ļ���
os.close();�ιر������
doczipsize = f.length();
�λ�ȡѹ���ļ��ij���
showTextandInfo();����ʾ���
}
catch(IOException ioe){
System.out.println(ioe);
}
}
private void showTextandInfo(){
����ʾ����ļ���ѹ����Ϣ
textarea.replaceRange(doc,0,textarea.getText().length())
;
infotip.setText("uncompressed size: "+doc.length()+"comp
ressed size: "+dc zipsize);
}
public void actionPerformed(ActionEvent e){
String arg = e.getActionCommand();
if ("New".equals(arg)) randomData();
else if ("Open".equals(arg)) openFile();
else if ("Save".equals(arg)) saveFile();
else if ("Exit".equals(arg)){
dispose();�ιرմ���
System.exit(0);�ιرճ���
}
else {
System.out.println("no this command!");
}
}
}
--
________ _____ _____ ______________ _____
___ __ \___(_)________ ___(_) __ ___/___ /_ ___(_)
__ /_/ /__ / ___ __ \__ / _____ \ __ __ \__ /
_ ____/ _ / __ /_/ /_ / ____/ / _ / / /_ /
/_/ /_/ _ .___/ /_/ /____/ /_/ /_/ /_/
/_/
[m[1;36m�� ��Դ:���ϴ�С�ٺ���Ϣ����վ bbs.nju.edu.cn��[FROM: hsia.nju.edu.cn][m
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads