日期:2014-05-20  浏览次数:20691 次

80分散分!!解决了就给分,其实是小问题
import   java.util.zip.*;
import   java.io.*;  

  public   class   Zipper
  {
  private   String   Name;
  private   ZipOutputStream   zip;
  public   String   getName()
  {
  return   Name;
  }
  public   void   setName(String   newName)
  {
  Name=newName;
  }
        //构造一个ZipOutputStream输出流利用输出流zip
 
 
  //添加方法完成的初始化
  public   int   init()
  {
  //建立并且初始化一个输入流
  try
  {
  //ZIP输出流的建立
 
  zip=new   ZipOutputStream(new   FileOutputStream(Name));  
  //Sets   the   ZIP   file   comment.  
  zip.setComment( "Zip   file   created   by   java ");
  //设置entry压缩方法,缺省值为DEFLATED  
  zip.setMethod(ZipOutputStream.DEFLATED);
                          //设置entry压缩水平,缺省值为DEFAULT_COMPRESSION  
                        zip.setLevel(compressionLevel);
  }
  catch(Exception   e)
  {
  System.out.println(e);
 
  }
  return   0;
  }
  public   int   close()
  //关闭输入流,清空缓冲区
  {
  try
  {
  zip.close();
  }
  catch(Exception   e)
  {
  System.out.println(e);
  return   1;
  }
  return   0;
  }
  public   int   addFile(String   fileName)
  {
  //编写增加压缩文件的函数
  int   entryNum;
  try
  {
  File   file   =new   File   (fileName);
  FileInputStream   in   =new   FileInputStream(file);
  byte[]   bytes=new   byte[in.available()];
  //in.available()方法返回的是输入流中可以正擦读取的字节数值,
  //     可以是读取的字节的数目,可以避免阻塞的问题,因为我们
  //     只是从流中读取能够读取的字节的序列
  in.read(bytes);
  in.close();
  //建立并且初始化zipEntry
  //getName()返回条目的名称
  ZipEntry   entry   =new   ZipEntry(file.getName());
  //设置条目修改的时间
          entry.setTime(file.lastModified());
          //向zip文件中写入数据
          zip.putNextEntry(entry);
          //如果当前的entry存在且处于激活状态时,关闭它,在zip文件中写入新的entry  
                                                //并将数据流定位于entry数据项的起始位置,压缩方法为setMethod指定的方法。
  zip.write(bytes);
  zip.closeEntry();
  //关闭当前的zip   entry,并将数据流定位于下一个entry的起始位置。  
 
  }
  catch(Exception   e)
  {
  System.out.println(e);
  }
  entryNum++;
  return   0;
  }


public   static   void   main(String[]   args)
  {
          int   compressionLevel=Deflater.DEFAULT_COMPRESSION;
          int   argc=args.length;
          //argc是程序运行参数的长度