java如何实现解压缩rar,zip,iso等文件啊?

zhangfun 2005-01-31 03:59:42
我要用java来实现解压缩,zip格式的可以用java自带的ZipFile,
但是rar和iso等格式的文件如何解压啊?有例子吗?
...全文
580 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yzh315 2005-02-17
  • 打赏
  • 举报
回复
咋会是这样子的了
Weilibo 2005-02-17
  • 打赏
  • 举报
回复
关注一下
sqlink 2005-01-31
  • 打赏
  • 举报
回复
压缩不公开,解压是公开的,你可以网上查一下算法
lhj 2005-01-31
  • 打赏
  • 举报
回复
rar的压缩算法是不公开的,不能自由使用的。如果要用要付钱。
iso的不太清楚。
zhangfun 2005-01-31
  • 打赏
  • 举报
回复
我想问的是rar和iso文件如何解压啊?!!
zhengji 2005-01-31
  • 打赏
  • 举报
回复
Gsoling 听到这个问题, 已经“不朽”了!默哀。
vcvj 2005-01-31
  • 打赏
  • 举报
回复

import java.io.*;
import java.util.*;
import java.util.zip.*;

/**
* JarResources: JarResources maps all resources included in a
* Zip or Jar file. Additionaly, it provides a method to extract one
* as a blob.
*/
public final class JarResources {
public boolean debugOn=false;
private Hashtable htSizes=new Hashtable();
private Hashtable htJarContents=new Hashtable();
private String jarFileName;
public JarResources(String jarFileName) {
this.jarFileName=jarFileName;
init();
}

/**
* Extracts a jar resource as a blob.
* @param name a resource name.
*/
public byte[] getResource(String name) {
return (byte[])htJarContents.get(name);
}

/**
* initializes internal hash tables with Jar file resources.
*/
private void init() {
try {
// extracts just sizes only.
ZipFile zf=new ZipFile(jarFileName);
Enumeration e=zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze=(ZipEntry)e.nextElement();
if (debugOn) {
System.out.println(dumpZipEntry(ze));
}
htSizes.put(ze.getName(),new Integer((int)ze.getSize()));
}
zf.close();

// extract resources and put them into the hashtable.
FileInputStream fis=new FileInputStream(jarFileName);
BufferedInputStream bis=new BufferedInputStream(fis);
ZipInputStream zis=new ZipInputStream(bis);

ZipEntry ze=null;
while ((ze=zis.getNextEntry())!=null) {
if (ze.isDirectory()) {
continue;
}
if (debugOn) {
System.out.println(
"ze.getName()="+ze.getName()+","+"getSize()="+ze.getSize()
);
}
int size=(int)ze.getSize();
// -1 means unknown size.
if (size==-1) {
size=((Integer)htSizes.get(ze.getName())).intValue();
}
byte[] b=new byte[(int)size];
int rb=0;
int chunk=0;
while (((int)size - rb) > 0) {
chunk=zis.read(b,rb,(int)size - rb);
if (chunk==-1) {
break;
}
rb+=chunk;
}
// add to internal resource hashtable
htJarContents.put(ze.getName(),b);
if (debugOn) {
System.out.println(
ze.getName()+" rb="+rb+
",size="+size+
",csize="+ze.getCompressedSize()
);
}
}
} catch (NullPointerException e) {
System.out.println("done.");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* Dumps a zip entry into a string.
* @param ze a ZipEntry
*/
private String dumpZipEntry(ZipEntry ze) {
StringBuffer sb=new StringBuffer();
if (ze.isDirectory()) {
sb.append("d ");
} else {
sb.append("f ");
}
if (ze.getMethod()==ZipEntry.STORED) {
sb.append("stored ");
} else {
sb.append("defalted ");
}
sb.append(ze.getName());
sb.append("\t");
sb.append(""+ze.getSize());
if (ze.getMethod()==ZipEntry.DEFLATED) {
sb.append("/"+ze.getCompressedSize());
}
return (sb.toString());
}


public static void main(String[] args) throws IOException {
if (args.length!=2) {
System.err.println(
"usage: java JarResources <jar file name> <resource name>"
);
System.exit(1);
}
JarResources jr=new JarResources(args[0]);
byte[] buff=jr.getResource(args[1]);
if (buff==null) {
System.out.println("Could not find "+args[1]+".");
} else {
System.out.println("Found "+args[1]+ " (length="+buff.length+").");
System.out.println(new String(buff));
}
}

} // End of JarResources class.

62,614

社区成员

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

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