《Thinking in Java2》第11章里有

解决方案 »

  1.   

    public boolean compress(File file, String displayname, boolean autorename) throws IOException {
            //根据指定要显示的文件名称和实际文件的扩展名来重新生成压缩文件名
            String actualname = file.getName();
            if (displayname == null || displayname.length() == 0)
                displayname = actualname;
            else {
                if (actualname.indexOf('.') != -1)
                    displayname += actualname.substring(actualname.indexOf('.'));
            }
            displayname = formatFileName(displayname);
            //判断压缩文件名是否已经存在,如是,则在前面加RENAME_PREFIX
            if (zout.size() > 0) {
                while (zout.exists(displayname)) {
                    if (!autorename) return false;
                    displayname = RENAME_PREFIX + displayname;
                    displayname = formatFileName(displayname);
                }
            }
            zout.putNextEntry(new ZipEntry(displayname));
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
            int nBytes = in.available();
            byte bt[] = new byte[10*1024];
            while ((nBytes = in.read(bt)) != -1) {
                zout.write(bt, 0, nBytes);
            }
            zout.flush();
            zout.closeEntry();
            in.close();
            in = null;
            return true;
        }
      

  2.   

    import java.io.*;
    import java.util.*;
    import java.util.zip.*;public class ZipCompress {  /**
       * 对文件进行压缩操作
       * @param strSourceFilePath 压缩文件的原文件路径
       * @param strSourceFile 压缩文件的原文件的文件名
       * @param strZipPath 压缩处理后的文件存放路径
       * @param strZipFile 压缩处理后的文件名
       * @return 压缩是否成功
       */
       public static boolean zipFile(String strSourceFilePath,String strSourceFile,String strZipPath,String strZipFile){
          try {
              FileOutputStream f = new FileOutputStream(strZipPath+strZipFile+".zip");
              ZipOutputStream out = new ZipOutputStream(new DataOutputStream(f));
              System.out.println(  "reading file " + strSourceFilePath+strSourceFile);
              DataInputStream in =new DataInputStream(new FileInputStream(strSourceFilePath+strSourceFile));
              out.putNextEntry(new ZipEntry(strSourceFile));
              int c;
              while((c = in.read()) != -1){
                 out.write(c);
              }
              in.close();
              out.close();
             return true;
          } 
          catch(Exception e) {
              e.printStackTrace();
          }
          return false;
       }  /**
       * 对数据流进行压缩处理
       * @param source 要进行压缩处理的数据流
       * @param strFileName 压缩文件的相对路径
       * @param strZipPath 压缩处理后的文件路径
       * @param strZipFile 压缩处理后的文件名
       * @return 压缩处理是否成功
       */
       public static boolean zip(byte[] source,String strFileName,String strZipPath,String strZipFile){
          try {
              FileOutputStream f = new FileOutputStream(strZipPath+strZipFile+".zip");
              ZipOutputStream out = new ZipOutputStream(new DataOutputStream(f));
              out.putNextEntry(new ZipEntry(strFileName));
              out.write(source);
              out.close();
              return true;
          } 
          catch(Exception e) {
              e.printStackTrace();
          }
          return false;
       }  /**
       * 对文件进行解压缩处理
       * @param strZipPath 解压缩的文件路径
       * @param strZipFile 解压缩的文件名
       * @param strSourceFilePath 解压缩处理后的文件存放路径
       * @param strSourceFile 解压缩处理后的文件名
       * @return 解压缩是否成功
       */
       public static boolean unizipFile(String strZipPath,String strZipFile,String strSourceFilePath,String strSourceFile){
          try {
              FileInputStream f = new FileInputStream(strZipPath+strZipFile+".zip");
              ZipInputStream in = new ZipInputStream(new DataInputStream(f));
              System.out.println(  "Writing file " + strSourceFilePath+strSourceFile);
              DataOutputStream out =new DataOutputStream(new FileOutputStream(strSourceFilePath+strSourceFile));
              in.getNextEntry();
              int c;
              while((c = in.read()) != -1){
                 out.write(c);
              }
              in.close();
    // }
              out.close();
              return true;
          } 
          catch(Exception e) {
              e.printStackTrace();
          }
          return false;
       }  /**
       * 对压缩文件进行解压缩处理
       * @param strZipPath 压缩文件存放路径
       * @param strZipFile 压缩文件的文件名
       * @return 返回解压缩处理后的文件流
       */
       public static byte[] unizip(String strZipPath,String strZipFile){
          byte[] buffer=null;
          try {
              FileInputStream f = new FileInputStream(strZipPath+strZipFile+".zip");
              ZipInputStream in = new ZipInputStream(new DataInputStream(f));
              System.out.println(  "reading file " + strZipPath+strZipFile+".zip");
              ZipEntry d=in.getNextEntry();          int c;
              byte[] by=new byte[1000];
              String str;
              while((c=in.read(by)) != -1){
                  buffer=appendbytes(buffer,by,c);
              }
              in.close();      } 
          catch(Exception e) {
              e.printStackTrace();
          }
          return buffer;
       }
       public static void main(String[] args) {
          ZipCompress a=new ZipCompress();
          try{
             a.zip("你好".getBytes(),"aa.txt","d:/","aa");
          }
          catch(Exception e){
             e.printStackTrace();
          }
          byte[] temp=a.unizip("d:/","huangzg");
          System.out.println(new String(temp));
       }  /**
       * 对字节的追加操作
       * @param a 原字节
       * @param b 添加的字节
       * @param length 添加字节的长度
       * @return 添加字节处理后的字节
       */
       private static byte[] appendbytes(byte[] a,byte[] b,int length){
          int lengtha=0;
          if(a!=null){
             lengtha=a.length;
          }
          if(b==null||length<1){
             return a;
          }
          byte[] c=new byte[lengtha+length];
          for(int i=0;i<lengtha;i++){
             c[i]=a[i];
          }
          for(int j=0;j<length;j++){
             c[j+lengtha]=b[j];
          }
          return c;
       }
    }