
正文
java解压rar代码 java解压rar压缩包
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
如何用java或者批处理命令 实现解压一个rar或zip文件 到选择的目录下 并生成桌面快捷方式
/**
* 解压课件包中的某个课件
* @param zipFilePath 课件包路径
* @param outDirPath 输出目录路径
* @return 解压出来文件的绝对路径,失败返回null
* @throws IOException
*/
public String decompressAllFileToDir(String zipFilePath, String outDirPath) throws IOException {
ZipFile zip = new ZipFile(zipFilePath, "GBK");
@SuppressWarnings("unchecked")
EnumerationZipEntry entries = zip.getEntries();
if (entries == null) {
return null;
}
java.io.File file = new java.io.File(outDirPath);
String path = file.getAbsolutePath();
file.mkdirs();
if (file.isFile()) {
return null;
}
while(entries.hasMoreElements())
{
ZipEntry entry = entries.nextElement();
String nameInZip = entry.getName().substring(entry.getName().lastIndexOf("/")+1);
InputStream is = zip.getInputStream(entry);
java.io.File tempFile = new java.io.File(nameInZip);
FileOutputStream fos = new FileOutputStream(new StringBuilder(path).append("/").append(tempFile.getName()).toString());
byte[] bytes = new byte[MAX_BUFFER];
int size = 0;
while ((size = is.read(bytes)) != -1) {
fos.write(bytes, 0, size);
}
}
return zipFilePath;
}
相关问答
Q1: 在LINUX下 用JAVA如何解压rar文件
楼主试试这个代码~~
package decompress;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Expand;
import de.innosystec.unrar.Archive;
import de.innosystec.unrar.rarfile.FileHeader;
public class DeCompressUtil {
/**
* 解压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格式的压缩包!");
}
}
}
Q2: 如何用java代码调用解压工具去解压.exe文件
再 windows下通过 cmd命令执行解压缩没问题,但是通过 java代码去执行不能解压是为什么?我在开始运行中输入命令: cmd/ c rar. exe x- y d:\\ auto. rar d:\\----上面命令可以解压成功,但是通过下面 java代码不能实现解压缩功能,请指点。主要代码: java. lang. Runtime. getRuntime(). exec(" cmd/ c rar. exe x- y d:\\ auto. rar d:\\");
再 windows下通过 cmd命令执行解压缩没问题,但是通过 java代码去执行不能解压是为什么?我在开始运行中输入命令: cmd/ c rar. exe x- y d:\\ auto. rar d:\\----上面命令可以解压成功,但是通过下面 java代码不能实现解压缩功能,请指点。主要代码: java. lang. Runtime. getRuntime(). exec(" cmd/ c rar. exe x- y d:\\ auto. rar d:\\");
Q3: java 解压rar文件
Java 解压rar文件需要用到apache的commons-compress-1.0.jar,这个类的使用如下:
package cn.myapps.util.pdf;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
public class UnzipFile {
public static void main(String[] args) {
try {
UnzipFile.unZip(new File("D:/你我他.zip"), "D:/upzip/");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void unZip(File zip, String root) throws Exception {
try {
ZipFile zipFile = new ZipFile(zip, "GBK");
Enumeration e = zipFile.getEntries();
byte ch[] = new byte[256];
while (e.hasMoreElements()) {
ZipArchiveEntry zipEntry = (ZipArchiveEntry) e.nextElement();
String temp = zipEntry.getName();
System.out.println("unziping " + zipEntry.getName());
File zfile = new File(root + temp);
File fpath = new File(zfile.getParentFile().getPath());
if (zipEntry.isDirectory()) {
if (!zfile.exists())
zfile.mkdirs();
} else {
if (!fpath.exists())
fpath.mkdirs();
FileOutputStream fouts = new FileOutputStream(zfile);
InputStream in = zipFile.getInputStream(zipEntry);
int i;
while ((i = in.read(ch)) != -1)
fouts.write(ch, 0, i);
fouts.close();
in.close();
}
}
} catch (Exception e) {
System.err.println("Exception from ZipUtil - unZip() : "
+ e.getMessage());
e.printStackTrace(System.err);
throw e;
}
}
}
java解压rar代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java解压rar压缩包、java解压rar代码的信息别忘了在本站进行查找喔。





