主頁(yè) > 知識(shí)庫(kù) > 利用Java實(shí)現(xiàn)zip壓縮/解壓縮

利用Java實(shí)現(xiàn)zip壓縮/解壓縮

熱門(mén)標(biāo)簽:地圖標(biāo)注創(chuàng)業(yè)項(xiàng)目入駐 外呼系統(tǒng)啥意思 長(zhǎng)春回?fù)芡夂粝到y(tǒng)廠家 地圖標(biāo)注制作道路 廣州三五防封電銷(xiāo)卡 珠海銷(xiāo)售外呼系統(tǒng)運(yùn)營(yíng)商 山東智能云外呼管理系統(tǒng) 四川電信外呼系統(tǒng)靠譜嗎 電銷(xiāo)外呼系統(tǒng) 排行榜
由于網(wǎng)絡(luò)帶寬有限,所以數(shù)據(jù)文件的壓縮有利于數(shù)據(jù)在Internet上的快速傳輸,同時(shí)也節(jié)

省服務(wù)器的外存空間。

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

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

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

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

數(shù)據(jù)項(xiàng)存儲(chǔ)壓縮數(shù)據(jù)。

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

  ·類(lèi)ZipEntry

  public ZipEntry(String name);

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

  ·類(lèi)ZipOutputStream

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

幾個(gè)函數(shù):

  public ZipOutputStream(OutputStream out);

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

  public void setMethod(int method);

  ∥設(shè)置entry壓縮方法,缺省值為DEFLATED。

  public void putNextEntry(ZipEntry newe);

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

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

  ·類(lèi)ZipInputStream

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

幾個(gè)函數(shù):

  public ZipInputStream(InputStream in);

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

  public ZipEntry getNextEntry();

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

  public void closeEntry();

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

  程序代碼及其注釋

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

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

將隨機(jī)生成的數(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; ∥存儲(chǔ)隨機(jī)生成的數(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);

  ∥隨機(jī)生成的數(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(){

  ∥隨機(jī)生成50個(gè)double數(shù)據(jù),并放在doc字符串變量中。

  doc="";

  for(int i=1;i51;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(){

  ∥打開(kāi)zip文件,將文件內(nèi)容讀入doc字符串變量中。

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

  dlg.show();

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

  try{

  ∥創(chuàng)建一個(gè)文件實(shí)例

  File f=new File(filename);

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

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

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

  zipis.getNextEntry();

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

  DataInputStream dis=new DataInputStream(zipis);

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

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

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

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

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

  }

  catch(IOException ioe){

  System.out.println(ioe);

  }

  }

  private void saveFile(){

  ∥打開(kāi)zip文件,將doc字符串變量寫(xiě)入zip文件中。

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

  dlg.show();

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

  try{

  ∥創(chuàng)建一個(gè)文件實(shí)例

  File f=new File(filename);

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

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

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

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

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

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

  DataOutputStream os=new DataOutputStream(zipos);

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

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

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

  doczipsize = f.length();

  ∥獲取壓縮文件的長(zhǎng)度

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

  }

  }

  }  

您可能感興趣的文章:
  • 詳解Java無(wú)需解壓直接讀取Zip文件和文件內(nèi)容
  • Java解壓zip文件完整代碼分享
  • java使用gzip實(shí)現(xiàn)文件解壓縮示例
  • java ant包中的org.apache.tools.zip實(shí)現(xiàn)壓縮和解壓縮實(shí)例詳解
  • Java解壓和壓縮帶密碼的zip文件過(guò)程詳解
  • Java解壓zip文件的關(guān)鍵代碼
  • 使用java基礎(chǔ)類(lèi)實(shí)現(xiàn)zip壓縮和zip解壓工具類(lèi)分享
  • Java解壓縮zip - 解壓縮多個(gè)文件或文件夾實(shí)例
  • java 壓縮和解壓縮Zip、Jar、Gzip文件實(shí)例代碼
  • Java如何不解壓讀取.zip的文件內(nèi)容

標(biāo)簽:吳忠 保定 潮州 紹興 北海 廣元 玉樹(shù) 肇慶

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《利用Java實(shí)現(xiàn)zip壓縮/解壓縮》,本文關(guān)鍵詞  利用,Java,實(shí)現(xiàn),zip,壓縮,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《利用Java實(shí)現(xiàn)zip壓縮/解壓縮》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于利用Java實(shí)現(xiàn)zip壓縮/解壓縮的相關(guān)信息資訊供網(wǎng)民參考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推薦文章