高分请教解压缩RAR程序代码?!

skyboy0720 2006-03-21 09:35:06
因为有个需求要用到,以前没做过,
搜索了半天也没找到我要的,想请教老鸟给个DEMO,谢谢了!
...全文
198 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
mayi3 2006-05-23
  • 打赏
  • 举报
回复
高人啊。。
ruanjiantaotao 2006-05-23
  • 打赏
  • 举报
回复
关注
fll_0601 2006-05-23
  • 打赏
  • 举报
回复
我也需要啊,楼主你的OK了吗?
chinabh 2006-05-23
  • 打赏
  • 举报
回复
是专利,没办法的,用winrar吧
little06 2006-05-23
  • 打赏
  • 举报
回复
不知道有没有人知道WinRar的算法
知道了进行重写也可以阿
tiannet 2006-05-23
  • 打赏
  • 举报
回复
java里貌似没提供解压rar文件得相关类
只有jar和zip得
cangwu_lee 2006-05-23
  • 打赏
  • 举报
回复
rar 还是调用 winrar.exe 来实现吧
lndlmy 2006-05-23
  • 打赏
  • 举报
回复
bitou(大鹏一日同风起,扶摇直上九万里) 正解。。不过只能解压JAR和ZIP
欢天xi地 2006-05-23
  • 打赏
  • 举报
回复
这个程序我怎么解压不了啊?
public static void main(String[] arg)throws Exception{
File file = new File("E:\\1.rar");
Unzip.unzip(file,"E\\aa","\\aaa");
}
crazy_he 2006-03-21
  • 打赏
  • 举报
回复
好像和gmail差不多
天外流星 2006-03-21
  • 打赏
  • 举报
回复
学习了.
bitou 2006-03-21
  • 打赏
  • 举报
回复

import java.io.*;
import java.util.zip.*;
import cn.com.ouyang.zip.*;
import cn.com.struts.exception.DefaultException;
import org.apache.log4j.Logger;

public class Unzip {
static Logger log = Logger.getLogger("--解压缩文件......");
/**
* 解压缩文件到directory目录,并且查找指定文件是否存在,如存在返回指定文件名,否则报错
*
* @param f File: zip文件
* @param directory String:解压缩的目录
* @param includedFile String:检查是否存在的文件,包含相对路径
* @return String
* @throws AppException:
* 当指定文件不存在时,抛出 AppException("err.file_not_found");
* 当zip中含有中文字符的文件名或目录名时,抛出AppException("err.zip_existChineseChar")
*/

public static String unzip(File f, String directory
, String includedFile) throws DefaultException {
int filecount = 0;
String fileName = null;
String tmpFileName = null;

boolean fileInclude = false;
if ("".equals(includedFile)) {
includedFile = null;
}
ZipInputStream zis = null;
BufferedOutputStream dest = null;

try {
final int BUFFER = 20480;
FileInputStream fis = new FileInputStream(f);
CheckedInputStream checksum = new CheckedInputStream(fis
, new Adler32());
// zis = new CZipInputStream(new BufferedInputStream(fis),"GBK");
zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
new File(directory).mkdirs();
while ( (entry = zis.getNextEntry()) != null) {

// tmpFileName = new String(entry.getName().getBytes("GBK"));
tmpFileName = new String(entry.getName());
int count;
byte data[] = new byte[BUFFER];
// write the files to the disk
String fn = directory + "/" + tmpFileName;

if (entry.isDirectory()) {
new File(fn).mkdirs();
continue;
} else {
if (includedFile == null
|| tmpFileName.equalsIgnoreCase(includedFile)) {
fileName = tmpFileName;
fileInclude = true;
}
filecount++;
if (!new File(fn).getParentFile().exists()) {
new File(fn).getParentFile().mkdirs();
}
new File(fn).createNewFile();
}

FileOutputStream fos = new FileOutputStream(fn);
dest = new BufferedOutputStream(fos, BUFFER);
while ( (count = zis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}
zis.close();
}

catch (Exception e) {
log.error( e);
throw new DefaultException("解压缩文件出现异常……");
} finally {
try {
zis.close();

} catch (IOException ex) {
log.error( ex);
}
try {
dest.close();
} catch (IOException ex) {
log.error( ex);
}
}
if (!fileInclude) {
throw new DefaultException("指定文件未找到!");
}
return (filecount <= 1) ? tmpFileName : fileName;
}

}

81,092

社区成员

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

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