/**
   * 打zip包
   * @param zipFilePath
   * @param toAddPath
   * @param exist
   * @return
   */
  public static boolean createZIPPackage( String filePath ,
      String addToPath ,
      boolean exist ){
    if( Utils.checkNull( filePath )
        || Utils.checkNull( addToPath ) )
      return false ;
    try{
      File file = new File( filePath ) ;
      if( !file.exists() ) return false ;
      File sf = new File( addToPath ) ;
      if( !sf.exists() && exist ){
        sf.createNewFile() ;
      }else if( !sf.exists() && !exist )
        return false ;
      ZipOutputStream zout = new ZipOutputStream( new CheckedOutputStream( new FileOutputStream( sf ) , new Adler32() ) ) ;
      Vector fs = new Vector() ;
      fs.addElement( file ) ;
      while( fs.size() > 0 ){
        file = ( File ) fs.remove( 0 ) ;
        if( file.isDirectory() ){
          File [] fl = file.listFiles() ;
          for( int loop = 0 , length = fl.length ; loop < length ; loop ++ ){
            fs.addElement( fl[loop] ) ;
          }
        }else{
          FileInputStream in = new FileInputStream( file ) ;
          zout.putNextEntry( new ZipEntry( file.getAbsolutePath() ) ) ;
          int b ;
          while( ( b = in.read() ) != -1 ){
            zout.write( b ) ;
          }
          in.close() ;
        }
      }
      zout.flush() ;
      zout.close() ;
      return true ;
    }catch( Exception e ){
      System.out.println( e.toString() ) ;
      return false ;
    }
  }

解决方案 »

  1.   

    编译报错
    Utils找不到啊
      

  2.   

    unzip :package cn.com.ucap.uform.tools;
    /* 
     * Copyright (c) 2003 ucap All rights reserved. 
     *  
     *
     * This software consists of voluntary contributions made by many
     * individuals on behalf of the UCAP Technology Co.,Ltd. For more
     * information on the UCAP Technology Co.,Ltd.  please see
     * <http://www.ucap.com.cn/>
     *
     *@author yubin (benjamin.yu)
     *@version ver 1.0
     *
     */
     
     
    import java.util.zip.ZipFile;
    import java.util.zip.ZipEntry;
    import java.io.InputStream;
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.BufferedOutputStream;
    import java.io.FileOutputStream;
    import java.util.Enumeration;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.zip.ZipException;public class unzip
    {
      private static ZipFile zf;
      private static final int EOF = -1;  
      
      public unzip( String fName , String sPath)
      {
         
         Enumeration enum;        
            
         try 
         {
          zf = new ZipFile( fName );
            enum = zf.entries();        while( enum.hasMoreElements() ) 
            {
             ZipEntry target = (ZipEntry)enum.nextElement();
                System.out.print( new String((target.getName().getBytes("ISO8859_1")) ,"GB2312") + " ." );
                saveEntry( sPath,target );
                System.out.println( ". unpacked" );             
            }
    }
        catch( FileNotFoundException e )
        {
         System.out.println( "zipfile not found" );
        }
        catch( ZipException e )
        {
             System.out.println( "zip error..." );
        }
        catch( IOException e )
        {
             System.out.println( "IO error..." );
        } 
        
      }
      
      public static void saveEntry( String s, ZipEntry target )
                                       throws ZipException,IOException 
        {
         try 
         {
         //String fn = new String((target.getName().getBytes("ISO8859_1")), "GB2312");
             File file = new File( s+"/" + target.getName() );
                
                if( target.isDirectory() ) 
                {
                    file.mkdirs(); 
                }           
                else 
                {
                         InputStream is = zf.getInputStream( target );
                         BufferedInputStream bis = new BufferedInputStream( is );
                         File dir = new File( file.getParent() );
                         dir.mkdirs();  
                         FileOutputStream fos = new FileOutputStream( file );
                         BufferedOutputStream bos = new BufferedOutputStream( fos );                     int c;
                         while( ( c = bis.read() ) != EOF ) 
                         {
                              bos.write( (byte)c );
                         }
                         bos.close();
                         fos.close();
                 }
             }
             catch( ZipException e )
             {
                 throw e;
             }
             catch( IOException e )
             {
                  throw e;
             }
    }   
    }       
      

  3.   

    我用递归算法实现过,你说不知某个项为目录,你可以把读出的文件列表,每一个建立File对象,用其方法isDirectory就可以判断是否为目录还是文件。
        你会对单一文件压缩和解压缩 ,就应改会对多个文件压缩啊 ,读取正确的路径,直接读入流就行了啊。没什么难的 啊
      

  4.   

    to:zihuilegend(子辉)(愚昧无知)
    我不是不知道isDirectory(),而是那样只能压缩所有文件,但原来的目录结构在解压时无法还原。我不知你是如何解决的。
      

  5.   

    if( Utils.checkNull( filePath )
            || Utils.checkNull( addToPath ) )
          return false ;
    换成if( ( filePath == null && filePath.equals( "" ) )
           || ( addToPath == null && addToPath.equals( "" ) ) )
    运行一下就知道是不是二级目录了
      

  6.   

    换成if( ( filePath == null || filePath.equals( "" ) )
           || ( addToPath == null || addToPath.equals( "" ) ) )
      

  7.   

    to:wukongqiao(NullGFException)
    运行了,看来是所有目录。不过有两个问题
    1.如果有中文目录或文件名,解压时有的会报错,无法建立,有的是乱码。
     2.假设加压文件为test.zip,每次都有一个嵌套的test.zip,就是test.zip中还有一个test.zip,而且内部的zip文件打不开。
      

  8.   

    public class ZipUtil {    private final static String FILE_SEPARATOR =
                System.getProperty("file.separator");
                
        private final static int BUFFER = 2048;    public static void main(String[] args) throws IOException {
            int flag = 1;
            if (flag == 0) {
                ZipUtil.compressFile("E:\\aaa","c:\\temp\\aaaaaaaaaaaaaaaa.zip");
            } else {
                ZipUtil.extractFile("C:\\temp\\pdf.rar","c:\\temp\\abcdefgjklmn");
            }
               
            System.out.println("operation completed");
        }           public static void extractFile(String fileName, String outDir) throws IOException {
            try {
                BufferedOutputStream dest = null;
                BufferedInputStream is = null;
                ZipEntry entry;
                ZipFile zipfile = new ZipFile(fileName);
                Enumeration e = zipfile.entries();
                
                if (outDir == null || outDir.equals("")) {
                    int k = fileName.lastIndexOf(FILE_SEPARATOR);
                    if (k >= 0) {
                        outDir = fileName.substring(0, k);
                    }
                }
                File outFile = new File(outDir);
                outFile.mkdir();
                while (e.hasMoreElements()) {
                    entry = (ZipEntry) e.nextElement();
                    String tempFileName = outDir + FILE_SEPARATOR + entry.getName();
    System.out.println(tempFileName);
                    File tempFile = new File(tempFileName);
                    if (entry.isDirectory()) {
                        tempFile.mkdir();
                        continue;
                    } else {
                        File aa = new File(tempFile.getParent());
                        aa.mkdir();
                    }
                    is = new BufferedInputStream(zipfile.getInputStream(entry));
                    int count;
                    byte data[] = new byte[BUFFER];
                    FileOutputStream fos = new FileOutputStream(tempFile);
                    dest = new BufferedOutputStream(fos, BUFFER);
                    while ((count = is.read(data, 0, BUFFER)) != -1) {
                        dest.write(data, 0, count);
                    }
                    dest.flush();
                    dest.close();
                }
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
    public static void compressFile(String fileName, String zipfile) throws IOException {
            File file = new File(fileName);
            String parentDir = getDir(fileName);
            FileOutputStream dest = new FileOutputStream(zipfile);
            ZipOutputStream out =
                new ZipOutputStream(new BufferedOutputStream(dest));
            if (file.isFile()) {
                gzipFile(out, fileName, parentDir + FILE_SEPARATOR);
            } else {
                zipDir(out, fileName, parentDir);
            }
            out.close();
    }
        
        private static void zipDir(ZipOutputStream out, String dir, String parentDir)
            throws IOException, IllegalArgumentException {
            File d = new File(dir);
            
            if (!d.isDirectory()) {
                gzipFile(out,dir, parentDir);
                return;
            }
            String[] entries = d.list();
            String path = d.getCanonicalPath();
            for (int i = 0; i < entries.length; i++) {
                zipDir(out, path + FILE_SEPARATOR + entries[i], parentDir);
            }
        }
        
        private static void gzipFile(ZipOutputStream out, String fileName, String parentDir) throws IOException {
            BufferedInputStream origin = null;
            byte data[] = new byte[BUFFER];
            FileInputStream inputFile = new FileInputStream(fileName);
            origin = new BufferedInputStream(inputFile, BUFFER);
            ZipEntry entry = new ZipEntry(getPath(fileName, parentDir));
            out.putNextEntry(entry);
            int count;
            while ((count = origin.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
            origin.close();
        }
        
        private static String getDir(String fileName) {
            File temp = new File(fileName);
            return temp.getParent();
        }
        
        private static String getPath(String fileName, String parentPackge) throws IOException {
            if (fileName.indexOf(parentPackge) < 0) {
                System.out.println(fileName);
                System.out.println(parentPackge);
                throw new IOException("パース不正");
            }
            return fileName.substring(parentPackge.length());
        }}
      

  9.   

    to whyxx(我也是新手) :
    哥们啊,你这段程序,我只修改了一个地方:
    if (fileName.indexOf(parentPackge) < 0) 改成
    if (fileName.indexOf(parentPackge.toUpperCase()) < 0) --因为从调试来看,filename
    的驱动器号为大写,而后者为小写,如果不改,始终无法运行,因为总是报错。
    所以我修改了这个地方。
    然而,运行之后,出现了一个无限增长的ZIP文件。开始21M,一会就120M了,faint,我赶紧给STOP了。