Java怎么解压带密码的ZIP文件(急用,给高分)

chentao1983 2006-08-08 04:41:40
大家好,用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),该怎么做?请高人指点,谢谢!
...全文
706 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
低调的小青蛙 2006-08-09
  • 打赏
  • 举报
回复
实在对不住, java.util.zip 不支持带密码的,你只能寻求第三方支持
TinyJimmy 2006-08-09
  • 打赏
  • 举报
回复
这个不好解决,如果压缩和解压过程你都能控制,不如加密数据文件好了
日总是我哥 2006-08-09
  • 打赏
  • 举报
回复
接分~

CSDN论坛浏览器:浏览、发帖、回复、结贴自动平均给分,下载地址:http://CoolSlob.ys168.com
hbwhwang 2006-08-09
  • 打赏
  • 举报
回复
如果是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();
}
masse 2006-08-09
  • 打赏
  • 举报
回复
http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=38&t=002359


http://www.jguru.com/faq/view.jsp?EID=234256
masse 2006-08-09
  • 打赏
  • 举报
回复
Runtime.getRuntime().exec(winzip ....)

java的api是不支持带密码的解压和压缩的,
或者看看有没有别的开源代码做过类似的事情。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧