生活随笔
收集整理的這篇文章主要介紹了
JAVA实现zip压缩/解压缩
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
JAVA實現了I/O數據流與網絡數據流的單一接口,因此數據的壓縮、網絡傳輸和解壓縮的實現比較容易,下面介紹利用ZipEntry、ZipInputStream和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的起始位置。
程序代碼及其注釋
下列的程序實現了數據文件zip方式的壓縮和解壓縮方法。randomData()函數隨機生成50個double數據,并放在doc字符串變量中;openFile()函數讀取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 ActionListener {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+" ";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 FileInputStream(f));zipis.getNextEntry();∥將輸入流定位在當前entry數據項位置DataInputStream dis=new DataInputStream(zipis);∥用ZIP輸入流構建DataInputStreamdoc=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.SAVE);dlg.show();String filename=dlg.getDirectory()+dlg.getFile();try{∥創建一個文件實例File f=new File(filename);if(!f.exists()) return; ∥文件不存在,則返回∥用文件輸出流構建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輸出流構建DataOutputStream;os.writeUTF(doc);∥將隨機生成的數據寫入文件中os.close();∥關閉數據流doczipsize = f.length();∥獲取壓縮文件的長度showTextandInfo();∥顯示數據}catch(IOException ioe){System.out.println(ioe);}}private void showTextandInfo(){∥顯示數據文件和壓縮信息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();∥關閉窗口System.exit(0);∥關閉程序}else {System.out.println("no this command!");}}}
總結
以上是生活随笔為你收集整理的JAVA实现zip压缩/解压缩的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。