java压缩解压缩_第1页
java压缩解压缩_第2页
java压缩解压缩_第3页
java压缩解压缩_第4页
java压缩解压缩_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Java     1.1实现了I/O数据流与网络数据流的单一接口,因此数据的压缩、网络传输和解         压缩的实现比较容易,下面介绍利用ZipEntry、ZipInputStream和ZipOutputStream三个Java         类实现zip数据压缩方式的编程方法。             zip压缩文件结构:一个zip文件由多个entry组成,每个entry有一个唯一的名称,entry的    

2、;         数据项存储压缩数据。             与zip文件有关的几个Java类             ·类ZipEntry             public     ZipEntry(String     name);            

3、name为指定的数据项名。             ·类ZipOutputStream             ZipOutputStream实现了zip压缩文件的写输出流,支持压缩和非压缩entry。下面是它的             几个函数:             public     ZipOutputStream(O

4、utputStream     out);             利用输出流out构造一个ZIP输出流。             public     void     setMethod(int     method);             设置entry压缩方法,缺省值为DEFLATED。      

5、;       public     void     putNextEntry(ZipEntry     newe);             如果当前的entry存在且处于激活状态时,关闭它,在zip文件中写入新的entry-newe             并将数据流定位于entry数据项的起始位置,压缩方法为setMethod指定的方法。      

6、       ·类ZipInputStream             ZipInputStream实现了zip压缩文件的读输入流,支持压缩和非压缩entry。下面是它的             几个函数:             public     ZipInputStream(InputStream     in);  

7、          利用输入流in构造一个ZIP输出流。             public     ZipEntry     getNextEntry();             返回ZIP文件中的下一个entry,并将输出流定位在此entry数据项的起始位置。             public   

8、0; void     closeEntry();             关闭当前的zip     entry,并将数据流定位于下一个entry的起始位置。             程序代码及其注释             下列的程序实现了数据文件zip方式的压缩和解压缩方法。randomData()函数随机生成         

9、60;   50个double数据,并放在doc字符串变量中;openFile()函数读取ZIP压缩文件;saveFile()函数             将随机生成的数据存到ZIP格式的压缩文件中。             import     java.util.zip.*;             import     java.awt.event.*; 

10、60;           import     java.awt.*;             import     java.lang.Math;             import     java.io.*;             public     class  

11、   TestZip     extends     Frame     implements     ActionListener                 TextArea     textarea;     显示数据文件的多行文本显示域             TextField     info

12、tip;     显示数据文件未压缩大小及压缩大小单行文本显示域             String     doc;     存储随机生成的数据             long     doczipsize     =     0;压缩数据文件的大小             public &#

13、160;   TestZip()             生成菜单             MenuBar     menubar     =     new     MenuBar();             setMenuBar(menubar);           &#

14、160; Menu     file     =     new     Menu("File",true);             menubar.add(file);             MenuItem     neww=     new     MenuItem("New"); 

15、60;           neww.addActionListener(this);             file.add(neww);             MenuItem     open=new     MenuItem("Open");             open.addActionL

16、istener(this);             file.add(open);             MenuItem     save=new     MenuItem("Save");             save.addActionListener(this);           

17、0; file.add(save);             MenuItem     exit=new     MenuItem("Exit");             exit.addActionListener(this);             file.add(exit);          

18、  随机生成的数据文件的多行文本显示域             add("Center",textarea     =     new     TextArea();             提示文本原始大小、压缩大小的单行文本显示域             add("South",infotip  

19、;   =     new     TextField();                         public     static     void     main(String     args)             TestZip     ok=

20、new     TestZip();             ok.setTitle("zip     sample");             ok.setSize(600,300);             ok.show();                  

21、       private     void     randomData()             随机生成50个double数据,并放在doc字符串变量中。             doc=""             for(int     i=1;i<51;i+)    

22、             double     rdm=Math.random()*10;                 doc=doc+new     Double(rdm).toString();                 if(i%5     =     0)    

23、; doc=doc+"n"                 else     doc=doc+"     "                         doczipsize     =     0;           &#

24、160; showTextandInfo();                         private     void     openFile()         打开zip文件,将文件内容读入doc字符串变量中。         FileDialog     dlg=new     FileDialog(

25、this,"Open",FileDialog.LOA     D);         dlg.show();         String     filename=dlg.getDirectory()+dlg.getFile();         try         创建一个文件实例         File   

26、0; f=new     File(filename);         if(!f.exists()     return;     文件不存在,则返回         用文件输入流构建ZIP压缩输入流         ZipInputStream     zipis=new     ZipInputStream(new     FileInputStre

27、am(f);         zipis.getNextEntry();         将输入流定位在当前entry数据项位置         DataInputStream     dis=new     DataInputStream(zipis);         用ZIP输入流构建DataInputStream         doc=dis.re

28、adUTF();读取文件内容         dis.close();关闭文件         doczipsize     =     f.length();获取ZIP文件长度         showTextandInfo();显示数据                 catch(IOException     ioe)  

29、       System.out.println(ioe);                         private     void     saveFile()         打开zip文件,将doc字符串变量写入zip文件中。         FileDialog     dlg=new &

30、#160;   FileDialog(this,"Save",FileDialog.SAVE);         dlg.show();         String     filename=dlg.getDirectory()+dlg.getFile();     try         创建一个文件实例         File     f=

31、new     File(filename);         if(!f.exists()     return;     文件不存在,则返回         用文件输出流构建ZIP压缩输出流         ZipOutputStream     zipos=new     ZipOutputStream(new     FileOutputStream

32、(f);         zipos.setMethod(ZipOutputStream.DEFLATED);     设置压缩方法         zipos.putNextEntry(new     ZipEntry("zip");         生成一个ZIP     entry,写入文件输出流中,并将输出流定位于entry起始处。        

33、DataOutputStream     os=new     DataOutputStream(zipos);         用ZIP输出流构建DataOutputStream;         os.writeUTF(doc);将随机生成的数据写入文件中         os.close();关闭数据流         doczipsize     =   &#

34、160; f.length();         获取压缩文件的长度         showTextandInfo();显示数据                 catch(IOException     ioe)         System.out.println(ioe);              

35、           private     void     showTextandInfo()         显示数据文件和压缩信息         textarea.replaceRange(doc,0,textarea.getText().length();         infotip.setText("uncompressed     siz

36、e:     "+doc.length()+"compressed     size:     "+dc     zipsize);                 public     void     actionPerformed(ActionEvent     e)         String &#

37、160;   arg     =     e.getActionCommand();         if     ("New".equals(arg)     randomData();         else     if     ("Open".equals(arg)     openFile();   

38、60;     else     if     ("Save".equals(arg)     saveFile();         else     if     ("Exit".equals(arg)             dispose();关闭窗口           

39、0; System.exit(0);关闭程序                 else                 System.out.println("no     this     command!");                        

40、;     给你个从zip包中读取图片文件然后显示的例子吧     package   catalog.servlet;         import   javax.servlet.http.HttpServlet;     import   javax.servlet.http.HttpServletRequest;     import   javax.servlet.http.HttpServletResponse;  

41、;   import   javax.servlet.ServletException;     import   java.io.*;     import   java.util.zip.ZipInputStream;     import   java.util.zip.ZipEntry;     import   java.util.zip.InflaterInputStream;     import   .URLD

42、ecoder;         /*       *   author   red       *   version   1.0   2004-12-17       */     public   class   ShowImageServlet   extends   HttpServlet         &#

43、160;     public   void   doGet(HttpServletRequest   request,   HttpServletResponse   response)                             throws   ServletException,   IOException      

44、                doProcess(request,   response);                             public   void   doPost(HttpServletRequest   request,   HttpServletResponse   respons

45、e)                             throws   ServletException,   IOException                       doProcess(request,   response);          

46、                   public   void   doProcess(HttpServletRequest   req,   HttpServletResponse   res)                             throws   ServletException,

47、  IOException                                                                                 &

48、#160;                                                   String   name   =   req.getParameter("name");              

49、       name   =   URLDecoder.decode(name,   "UTF-8");                     String   zipFile   =   "c:/1.zip"                     File &#

50、160; file   =   new   File(zipFile);                     FileInputStream   fis   =   new   FileInputStream(file);                         ZipInputStream &#

51、160; zis   =   new   ZipInputStream(fis);                     ZipEntry   entry   =   null;                                 do    

52、                              entry   =   zis.getNextEntry();                             if   (entry   =   null)     &#

53、160;                               continue;                                 if   (!entry.getName().equals(name)        

54、                               continue;                                                     &#

55、160;           byte   buf   =   getData(zis);                             OutputStream   toClient   =   res.getOutputStream();   /得到向客户端输出二进制数据的对象       &#

56、160;                     toClient.write(buf);   /输出数据                             toClient.close();                      

57、;       break;                       while   (entry   !=   null);                         zis.close();               

58、60;     fis.close();                             private   byte   getData(InflaterInputStream   zis)                       try           

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论