闂佽 鍋撻柟顖滃椤ワ拷 (0) +1 闁荤姍宥嗗 (0) +1 闂傚⿴浜烽幏锟� (0) +1
闂佽 鍋撻柟顖滃椤ユ垿鏌熺€涙ê濮囧┑顕嗘嫹闂佸搫琚崕鍐诧耿閸涙潙缁╅柟顖滃椤ワ拷>>

正在阅读:利用Java实现zip压缩/解压缩利用Java实现zip压缩/解压缩

2004-11-24 10:16 出处:CSDN 作者:aeclipser 责任编辑:linjixiong


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

察看评论详细内容 我要发表评论
作者笔名简短内容 发表时间
:
键盘也能翻页,试试“← →”键

关注我们

最新资讯离线随时看 聊天吐槽赢奖品
闂佸綊娼ч鍡椻攦閳ь剟鎮规担鍛婂仴婵☆偓鎷�闂佹悶鍎抽崑娑㈠春鐏炰勘浜滈柛顐f礀閸旓拷