大家好,用java.util.zip或Ant.jar类库中的zip类import org.apache.tools.zip.*,可以解压不带密码的zip压缩文件,现在需要解压带密码的zip文件(密码已知),以上两个包中没有带设置压缩文件密码的方法,不知道大家有谁做过解压带密码的zip文件?import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;public class UnZip 
{
    static final int BUFFER = 2048;     //设置缓冲流
    public void unZip(String path) 
    {
       File fileComptation = new File(path);       int i =fileComptation.getAbsolutePath().lastIndexOf('.');
       String dirname = new String();
  
       if ( i != -1 )
  dirname = fileComptation.getAbsolutePath().substring(0,i);
       else
  dirname = fileComptation.getAbsolutePath();
  
       File newdir = new File(dirname);
       newdir.mkdir();
       try 
       {   
  FileInputStream fis = new FileInputStream(path);   //获得输入流
  ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
  ZipEntry file = zis.getNextEntry();
  byte[] c = new byte[1024];
  int slen; 
  while (file != null)
  {
    i = make8859toGB(file.getName()).replace('/','\\').lastIndexOf('\\');
    if ( i != -1 )
             {
                 File dirs = new File(dirname+File.separator+make8859toGB(file.getName()).replace('/','\\').substring(0,i));
        dirs.mkdirs();
        dirs = null;
    }
         
    System.out.print("Extract "+make8859toGB(file.getName()).replace('/','\\')+" ........  ");
    if (file.isDirectory())
    {
        File dirs = new File(make8859toGB(file.getName()).replace('/','\\'));
     dirs.mkdir();
     dirs = null;
    }
    else
    { 
        FileOutputStream out = new FileOutputStream(dirname+File.separator+make8859toGB(file.getName()).replace('/','\\'));
       while((slen = zis.read(c,0,c.length)) != -1)
       out.write(c,0,slen);
       out.close();
    }
    System.out.print("O.K.\n");
    file = zis.getNextEntry();
 }
        }
        catch (Exception e) 
        {
    e.printStackTrace();
        }
    }    public static void main(String[] args)
    {
        UnZip uz=new UnZip();
        uz.unZip("c:\\eSeExcelFromJDBC.zip") ;//zip压缩文件路径
    }    public static String make8859toGB(String str)
   {
        try
        {
             String str8859 = new String(str.getBytes("8859_1"),"GB2312");
             return str8859;
         }catch(UnsupportedEncodingException ioe)
         {
             return str;
         }
     }
}以上是解压不带密码的zip压缩文件代码,如果zip文件有密码(密码固定,假设为123456),该怎么做?请高人指点,谢谢!

解决方案 »

  1.   

    java.util.zip或Ant.jar类库中没有说明文档吗??明天看看
      

  2.   

    麻烦~,zip包没有提供支持。估计得自己写了
      

  3.   

    如果是windows的系统,装个winrar。用它来解压,呵呵。        String pw="12345";
            String cmd="C:\\Program Files\\WinRAR\\winrar X -p"+pw+" f:\\temp\\temp1.zip d:\\temp";
            try{
                Runtime.getRuntime().exec(cmd);
            }catch(Exception ex){
                ex.printStackTrace();
            }
      

  4.   

    没空找下载地址了
    我邮箱 theforever # 163.com  你发给我 我明天下来看看如果确信里面的确没有这功能(比如开发包文档声明了),那就算了