群里一位朋友的解压RAR的代码,留着以备参考
来源:互联网 发布:linux sh 定义变量 编辑:程序博客网 时间:2024/05/21 18:38
慕容火月提供的部分代码
Archive rar = new Archive(new File("d://2345.rar"));
FileHeader fh = rar.nextFileHeader();
while(fh != null){
System.out.println("fh.name="+fh.getFileNameString());
File out = new File("d://",fh.getFileNameString());
FileOutputStream os = new FileOutputStream(out);
rar.extractFile(fh, os);
os.close();
fh=rar.nextFileHeader();
}
以下是j2ee北京提供的方法,需要用到java-urar-0.3.jar这个jar包
源码如下:
/**
* 解压rar格式的压缩文件到指定目录下
* @param rarFileName 压缩文件
* @param extPlace 解压目录
* @param repeatFile 重复路径
* @throws Exception
* @author Every
*/
public synchronized void unrar(String rarFileName, String extPlace,List repeatFile) throws Exception{
File f = new File(rarFileName);
FileOutputStream os=null;
Archive a=null;
try {
a = new Archive(f);
} catch (RarException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
extPlace=(extPlace==null||extPlace.trim().equals(""))?f.getParent()+File.separator+f.getName().replace(".", "#").split("#")[0]:extPlace;
// System.out.println(extPlace);
File ext=new File(extPlace);
if(!ext.exists()){
ext.mkdirs();
}
if(a!=null){
a.getMainHeader().print();
FileHeader fh = a.nextFileHeader();
while(fh!=null){
try {
File out = new File(extPlace+File.separator+fh.getFileNameString().trim());
if (out.exists()&&repeatFile!=null) {
File rFile=new File(extPlace+File.separator+"tempFile");
if(rFile.exists())rFile.mkdirs();
repeatFile.add(fh.getFileNameString().trim());
out=new File(extPlace+File.separator+"tempFile"+File.separator+fh.getFileNameString().trim());
os = new FileOutputStream(out);
}else{
os = new FileOutputStream(out);
}
a.extractFile(fh, os);
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RarException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fh=a.nextFileHeader();
}
}
}
/**
* 解压rar格式的压缩文件到当前文件所在目录下
* @param rarFileName 压缩文件
* @param repeatFile 重复路径
* @throws Exception
* @author Every
*/
public synchronized void unrar(String rarFileName,List repeatFile) throws Exception{
unrar(rarFileName,null,repeatFile);
}
/**
* 解压rar格式的压缩文件到当前文件所在目录下
* @param rarFileName 压缩文件
* @param extPlace 解压目录
* @throws Exception
* @author Every
*/
public synchronized void unrar(String rarFileName,String extPlace) throws Exception{
unrar(rarFileName,extPlace,null);
}
/**
* 解压rar格式的压缩文件到当前文件所在目录下
* @param rarFileName 压缩文件
* @param delete 是否删除源文件
* @throws Exception
* @author Every
*/
public synchronized void unrar(String rarFileName) throws Exception{
unrar(rarFileName,null,null);
}
/**
* 执行实例
* @param args
*/
public static void main(String[] args) throws Exception {
Decompression decompression=new Decompression();
// decompression.unzipFile("c:/test.zip");
// decompression.unzip("c:/test.zip","c:/test22");
// decompression.zip("c:/test22", "c:/test222.zip");
decompression.unrar("c:/tests.rar",null,new ArrayList());
}
懒的排格式了= =|| 用到的时候再说
- 群里一位朋友的解压RAR的代码,留着以备参考
- 解压RAR格式的压缩包代码
- java解压zip,rar的代码
- zip rar的解压
- 一位朋友的到访
- 一位素不相识的朋友做的代码雨屏保程序
- 一位华为朋友的来信
- DiskPerf代码分析(供学习驱动开发的朋友参考)
- 批量解压相同密码的RAR文件
- Linux实现对rar压缩文件的解压
- Ubuntu下解压rar文件的方法
- linux下解压rar文件的方法
- java zip和rar压缩文件的解压
- ubuntu下rar解压的解决办法
- Ubuntu下解压rar文件的方法
- Java解压带密码的Rar压缩文件
- Ubuntu下解压rar文件的方法
- Ubuntu下解压rar文件的方法
- 平台崩溃之operator new异常(十五)-2010-6-10
- 再谈Windows Mobile上的模拟按键操作
- Ubuntu 命令技巧
- dos命令大全
- CRichEditCtrl加载 rtf 文件和字符串的方法
- 群里一位朋友的解压RAR的代码,留着以备参考
- Windows Mobile读取SIM卡联系人
- 利用Visual C#开发一个媒体播放器
- 2010年6月7日(新公司入职,第十周星期五)
- 如何在windows7 Home家庭版中创建Administrator管理员账户
- linux网站
- 详解spring事务属性
- 认识GPS卫星导航定位系统
- Shortcuts for the Edit menu