网站建设资讯

NEWS

网站建设资讯

java代码解压rar java代码解压到项目路径

如何用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:\\");

java解压RAR文件的问题

进程冲突吧?

两个命令一起执行的应该。。两个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;

}

在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格式的压缩包!");

}

}

}

java中怎么解压rar文件 到指定文件目录中

1.代码如下:

[java] view plain copy

span style="font-size:18px;background-color: rgb(204, 204, 204);"package cn.gov.csrc.base.util;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

/**

* 将文件夹下面的文件

* 打包成zip压缩文件

*

* @author admin

*

*/

public final class FileToZip {

private FileToZip(){}

/**

* 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下

* @param sourceFilePath :待压缩的文件路径

* @param zipFilePath :压缩后存放路径

* @param fileName :压缩后文件的名称

* @return

*/

public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName){

boolean flag = false;

File sourceFile = new File(sourceFilePath);

FileInputStream fis = null;

BufferedInputStream bis = null;

FileOutputStream fos = null;

ZipOutputStream zos = null;

if(sourceFile.exists() == false){

System.out.println("待压缩的文件目录:"+sourceFilePath+"不存在.");

}else{

try {

File zipFile = new File(zipFilePath + "/" + fileName +".zip");

if(zipFile.exists()){

System.out.println(zipFilePath + "目录下存在名字为:" + fileName +".zip" +"打包文件.");

}else{

File[] sourceFiles = sourceFile.listFiles();

if(null == sourceFiles || sourceFiles.length1){

System.out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");

}else{

fos = new FileOutputStream(zipFile);

zos = new ZipOutputStream(new BufferedOutputStream(fos));

byte[] bufs = new byte[1024*10];

for(int i=0;isourceFiles.length;i++){

//创建ZIP实体,并添加进压缩包

ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());

zos.putNextEntry(zipEntry);

//读取待压缩的文件并写进压缩包里

fis = new FileInputStream(sourceFiles[i]);

bis = new BufferedInputStream(fis, 1024*10);

int read = 0;

while((read=bis.read(bufs, 0, 1024*10)) != -1){

zos.write(bufs,0,read);

}

}

flag = true;

}

}

} catch (FileNotFoundException e) {

e.printStackTrace();

throw new RuntimeException(e);

} catch (IOException e) {

e.printStackTrace();

throw new RuntimeException(e);

} finally{

//关闭流

try {

if(null != bis) bis.close();

if(null != zos) zos.close();

} catch (IOException e) {

e.printStackTrace();

throw new RuntimeException(e);

}

}

}

return flag;

}

public static void main(String[] args){

String sourceFilePath = "D:\\TestFile";

String zipFilePath = "D:\\tmp";

String fileName = "12700153file";

boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName);

if(flag){

System.out.println("文件打包成功!");

}else{

System.out.println("文件打包失败!");

}

}

}

/span

2.结果如下:

文件打包成功!

3.到D:/tmp下查看,你会发现生成了一个zip压缩包.


文章标题:java代码解压rar java代码解压到项目路径
文章网址:http://cdweb.net/article/hjgpci.html