对java zip输入输出流的学习过程

 

贴上代码,写上自己一点对java zip输入输出流的学习过程。

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.zip.*;//导入

//使用ZipInputStream和ZipOutputStream简单压缩和解压缩

public class ziptext{

         String zipname;//压缩和解压缩zip文件名。

         String zipdirname;//压缩、解压缩目录名(格式:D:\\dir\\)

         int zipUnotNo;//判断是压缩还是解压缩 0 解压缩 1 压缩。

         public ziptext(String zipname,String zipfilename,int zipUnorNo)

         {

                   this.zipname=zipname;

                   this.zipdirname=zipfilename;

                   this.zipUnotNo=zipUnotNo;

                   if(zipUnorNo==1)

                   {

                   try

                   {

              File zipfile=new File(zipfilename);

                    ZipOutputStream zfo=new ZipOutputStream(new FileOutputStream(zipname));//zip输出流

                    for(File i:zipfile.listFiles())

                   {

                            byte [] buf=new byte[(int)i.length()];

                                FileInputStream fo=new FileInputStream(i.getPath());

                                     fo.read(buf);

                                     zfo.putNextEntry(new ZipEntry(i.getName()));//写入zip输出流条目

                                     zfo.write(buf);

                                     fo.close();

                   }

                   zfo.close();

                   }

                   catch(Exception e)

                   {

                            e.printStackTrace();

                   }

                   }

                   else

                   {

                            try

                            {

                                     ZipInputStream zfo2=new ZipInputStream(new FileInputStream(zipname));//zip输入流

                                     ZipEntry unfile;

                                     while(true)

                                     {

                                               unfile=zfo2.getNextEntry();

                                               if(unfile==null)

                                               {

                                                        break;

                                               }

                                               BufferedInputStream bzfo2=new BufferedInputStream(zfo2,(int)unfile.getSize());//包装zip输入流

                                               byte [] buf2=new byte[(int)unfile.getSize()];

                                               bzfo2.read(buf2);

                                               FileOutputStream unfin=new FileOutputStream(zipdirname+unfile.getName());

                                               unfin.write(buf2);

                                               unfin.close();

                                     }

                                     zfo2.close();

                            }

                            catch(Exception e)

                            {

                                     e.printStackTrace();

                            }

                   }

         }

         public static void main(String[] arge)

         {

                   //使用ZipInputStream和ZipOutputStream简单压缩和解压缩

                   new ziptext("D:\\hello.zip","D:\\",0);      

         }

}

你可能感兴趣的:(java,压缩,exception,String,File,byte,解压缩)