java解压rar.zip
来源:互联网 发布:阿里云服务器如何分区 编辑:程序博客网 时间:2024/06/04 18:09
/**
* 解压zip格式压缩包
* 对应的是ant.jar
*/
private static void unzip(String sourceZip,String destDir) throws Exception{
try{
Project p = new Project();
Expand e = new Expand();
e.setProject(p);
e.setSrc(new File(sourceZip));
e.setOverwrite(false);
e.setDest(new File(destDir));
/*
ant下的zip工具默认压缩编码为UTF-8编码,
而winRAR软件压缩是用的windows默认的GBK或者GB2312编码
所以解压缩时要制定编码格式
*/
e.setEncoding("gbk");
e.execute();
}catch(Exception e){
throw e;
}
}
/**
* 解压rar格式压缩包。
* 对应的是java-unrar-0.3.jar,但是java-unrar-0.3.jar又会用到commons-logging-1.1.1.jar
*/
private static void unrar(String sourceRar,String destDir) throws Exception{
Archive a = null;
FileOutputStream fos = null;
try{
a = new Archive(new File(sourceRar));
FileHeader fh = a.nextFileHeader();
while(fh!=null){
if(!fh.isDirectory()){
//1 根据不同的操作系统拿到相应的 destDirName 和 destFileName
String compressFileName = fh.getFileNameString().trim();
String destFileName = "";
String destDirName = "";
//非windows系统
if(File.separator.equals("/")){
destFileName = destDir + compressFileName.replaceAll("\\\\", "/");
destDirName = destFileName.substring(0, destFileName.lastIndexOf("/"));
//windows系统
}else{
destFileName = destDir + compressFileName.replaceAll("/", "\\\\");
destDirName = destFileName.substring(0, destFileName.lastIndexOf("\\"));
}
//2创建文件夹
File dir = new File(destDirName);
if(!dir.exists()||!dir.isDirectory()){
dir.mkdirs();
}
//3解压缩文件
fos = new FileOutputStream(new File(destFileName));
a.extractFile(fh, fos);
fos.close();
fos = null;
}
fh = a.nextFileHeader();
}
a.close();
a = null;
}catch(Exception e){
throw e;
}finally{
if(fos!=null){
try{fos.close();fos=null;}catch(Exception e){e.printStackTrace();}
}
if(a!=null){
try{a.close();a=null;}catch(Exception e){e.printStackTrace();}
}
}
}
/**
* 解压缩
*/
public static void deCompress(String sourceFile,String destDir) throws Exception{
//保证文件夹路径最后是"/"或者"\"
char lastChar = destDir.charAt(destDir.length()-1);
if(lastChar!='/'&&lastChar!='\\'){
destDir += File.separator;
}
//根据类型,进行相应的解压缩
String type = sourceFile.substring(sourceFile.lastIndexOf(".")+1);
if(type.equals("zip")){
DeCompressUtil.unzip(sourceFile, destDir);
}else if(type.equals("rar")){
DeCompressUtil.unrar(sourceFile, destDir);
}else{
throw new Exception("只支持zip和rar格式的压缩包!");
}
}
public static void main(String[] args)
{
try {
// DeCompressUtil.deCompress("F:\\AttachDir.rar","F:\\a第三方s");
// List<File> fileList=new ArrayList<File>();
// DeCompressUtil.readFile("F:\\a第三方s",fileList);
// System.out.println(fileList.size());
String path="C:\\Documents and Settings\\Administrator\\workspaces\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\MSISA2.1.4\\ntkoOffice\\ntkofile\\1370764956046";
DeCompressUtil.delFolder(path);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 删除文件夹
* param folderPath 文件夹完整绝对路径
*/
public static void delFolder(String folderPath) {
try {
delAllFile(folderPath); //删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); //删除空文件夹
} catch (Exception e) {
e.printStackTrace();
}
}
//删除指定文件夹下所有文件
//param path 文件夹完整绝对路径
public static boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
delFolder(path + "/" + tempList[i]);//再删除空文件夹
flag = true;
}
}
return flag;
}
/**
* 读取指定文件夹下的所有文件
* @param filepath
* @param fileList
* @throws FileNotFoundException
* @throws IOException
*/
public static void readFile(String filepath,List<File> fileList)
throws FileNotFoundException, IOException {
try {
File file = new File(filepath);
if (!file.isDirectory()) {
//System.out.println("path=" + file.getPath());
fileList.add(file);
} else if (file.isDirectory()) {
File[] filelist = file.listFiles();
for (int i = 0; i < filelist.length; i++) {
File readfile = filelist[i];
if (!readfile.isDirectory()) {
//System.out.println("path=" + readfile.getPath());
fileList.add(readfile);
} else if (readfile.isDirectory()) {
readFile(readfile.getPath(),fileList);
}
}
}
} catch (FileNotFoundException e) {
System.out.println("readfile() Exception:" + e.getMessage());
}
}
- java解压rar.zip
- java解压zip与rar
- java解压rar以及zip
- Java解压ZIP、RAR文件
- Java 解压rar 解压缩zip
- java 解压zip、rar文件
- java之解压zip,rar
- JAVA 解压ZIP RAR文件
- Java解压zip和rar文件
- java zip和rar压缩文件的解压
- Java 解压zip和rar文件
- java解压zip,rar的代码
- java解压zip或者rar压缩文件
- java解压zip、rar(多级文件)
- 解压rar,打成zip
- zip rar的解压
- 解压zip rar文件
- php 解压zip/rar
- 银行加密算法之——数据打解包
- cocos2d-x CCRenderTexture
- 黑马程序员_字符串的处理(一)
- STM32 + RT Thread OS 学习笔记[一]
- cocos2d-x 获取屏幕当前方向和系统语言
- java解压rar.zip
- cocos2d-x cocosBuilder
- cocos2d-x 物理引擎box2d
- 大学生活随想
- django 数据排序
- 一个女大学生总结的大学生活
- 大学最后一个学期,我们在干什么?
- 35岁前应做好的十件事
- 玫瑰花的含义