a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > Java > 正文

利用Java實現(xiàn)zip壓縮-解壓縮

2020-03-24 19:16:09
字體:
供稿:網(wǎng)友
由于網(wǎng)絡帶寬有限,所以數(shù)據(jù)文件的壓縮有利于數(shù)據(jù)在Internet上的快速傳輸,同時也節(jié)

服務器的外存空間。

Java 1.1實現(xiàn)了I/O數(shù)據(jù)流與網(wǎng)絡數(shù)據(jù)流的單一接口,因此數(shù)據(jù)的壓縮、網(wǎng)絡傳輸和解

壓縮的實現(xiàn)比較容易,下面介紹利用ZipEntry、ZipInputStream和ZipOutputStream三個Java

類實現(xiàn)zip數(shù)據(jù)壓縮方式的編程方法。

zip壓縮文件結(jié)構(gòu):一個zip文件由多個entry組成,每個entry有一個唯一的名稱,entry的

數(shù)據(jù)項存儲壓縮數(shù)據(jù)。

與zip文件有關(guān)的幾個Java類

·類ZipEntry

html' target='_blank'>public ZipEntry(String name);

name為指定的數(shù)據(jù)項名。

·類ZipOutputStream

ZipOutputStream實現(xiàn)了zip壓縮文件的寫輸出流,支持壓縮和非壓縮entry。下面是它的

幾個函數(shù):

public ZipOutputStream(OutputStream out);

∥利用輸出流out構(gòu)造一個ZIP輸出流。

public void setMethod(int method);

∥設置entry壓縮方法,缺省值為DEFLATED。

public void putNextEntry(ZipEntry newe);

∥如果當前的entry存在且處于激活狀態(tài)時,關(guān)閉它,在zip文件中寫入新的entry-newe

并將數(shù)據(jù)流定位于entry數(shù)據(jù)項的起始位置,壓縮方法為setMethod指定的方法。

·類ZipInputStream

ZipInputStream實現(xiàn)了zip壓縮文件的讀輸入流,支持壓縮和非壓縮entry。下面是它的

幾個函數(shù):

public ZipInputStream(InputStream in);

∥利用輸入流in構(gòu)造一個ZIP輸出流。

public ZipEntry getNextEntry();

∥返回ZIP文件中的下一個entry,并將輸出流定位在此entry數(shù)據(jù)項的起始位置。

public void closeEntry();

∥關(guān)閉當前的zip entry,并將數(shù)據(jù)流定位于下一個entry的起始位置。

程序代碼及其注釋

下列的程序?qū)崿F(xiàn)了數(shù)據(jù)文件zip方式的壓縮和解壓縮方法。randomData()函數(shù)隨機生成

50個double數(shù)據(jù),并放在doc字符串變量中;openFile()函數(shù)讀取ZIP壓縮文件;saveFile()函數(shù)

將隨機生成的數(shù)據(jù)存到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 ActionListener {

TextArea textarea; ∥顯示數(shù)據(jù)文件的多行文本顯示域

TextField infotip; ∥顯示數(shù)據(jù)文件未壓縮大小及壓縮大小單行文本顯示域

String doc; ∥存儲隨機生成的數(shù)據(jù)

long doczipsize = 0;∥壓縮數(shù)據(jù)文件的大小

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);

∥隨機生成的數(shù)據(jù)文件的多行文本顯示域

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數(shù)據(jù),并放在doc字符串變量中。

doc=

for(int i=1;i 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文件,將文件內(nèi)容讀入doc字符串變量中。

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

dlg.show();

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

try{

∥創(chuàng)建一個文件實例

File f=new File(filename);

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

∥用文件輸入流構(gòu)建ZIP壓縮輸入流

ZipInputStream zipis=new ZipInputStream(new FileInputStream(f));

zipis.getNextEntry();

∥將輸入流定位在當前entry數(shù)據(jù)項位置

DataInputStream dis=new DataInputStream(zipis);

∥用ZIP輸入流構(gòu)建DataInputStream

doc=dis.readUTF();∥讀取文件內(nèi)容

dis.close();∥關(guān)閉文件

doczipsize = f.length();∥獲取ZIP文件長度

showTextandInfo();∥顯示數(shù)據(jù)

}

catch(IOException ioe){

System.out.println(ioe);

}

}

private void saveFile(){

∥打開zip文件,將doc字符串變量寫入zip文件中。

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

dlg.show();

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

try{

∥創(chuàng)建一個文件實例

File f=new File(filename);

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

∥用文件輸出流構(gòu)建ZIP壓縮輸出流

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

zipos.setMethod(ZipOutputStream.DEFLATED); ∥設置壓縮方法

zipos.putNextEntry(new ZipEntry( zip ));

∥生成一個ZIP entry,寫入文件輸出流中,并將輸出流定位于entry起始處。

DataOutputStream os=new DataOutputStream(zipos);

∥用ZIP輸出流構(gòu)建DataOutputStream;

os.writeUTF(doc);∥將隨機生成的數(shù)據(jù)寫入文件中

os.close();∥關(guān)閉數(shù)據(jù)流

doczipsize = f.length();

∥獲取壓縮文件的長度

showTextandInfo();∥顯示數(shù)據(jù)

}

catch(IOException ioe){

System.out.println(ioe);

}

}

private void showTextandInfo(){

∥顯示數(shù)據(jù)文件和壓縮信息

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

infotip.setText( uncompressed size: +doc.length()+ compressed 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();∥關(guān)閉窗口

System.exit(0);∥關(guān)閉程序

}

else {

System.out.println( no this command!

}

}

}

html教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: www.日韩在线| 91啪影院| 午夜精品一区二区三区在线观看 | 亚洲最黄网站 | 射久久 | 国产精品一区二区在线观看 | 视频一二三区 | 精品国产乱码久久久久久闺蜜 | 91在线成人| 久久羞羞 | 在线看国产 | 99精品国产一区二区 | 日韩一区二区三区四区五区六区 | 欧美成人精精品一区二区频 | 精品日韩欧美一区二区在线播放 | 国产精品美腿一区在线看 | 国产91精品一区二区绿帽 | 精品久久久一区 | 日韩一区二区三区在线 | 久久电影网 | 91av原创| 日韩日b视频 | 日日射av| 欧美中文日韩 | 国产精品一区久久 | 四虎影视免费观看 | 久久国产综合 | 亚洲激情久久 | 精品亚洲视频在线 | 久久高清 | 亚州综合一区 | 日韩免费视频中文字幕 | 成人aaaa| 91视频国内 | 天天夜夜骑| 亚洲电影一区二区 | 91在线成人 | 欧美日韩99 | 99精品国产在热久久 | 成人免费视频观看视频 | 国产精品美女高潮无套久久 |