资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java代码解压 java 解压gz

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

创新互联建站主要从事网站设计制作、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务准格尔,10年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

再 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文件 到指定文件目录中

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 以流的形式解压带密码的zip

可以使用 Runtime 直接调用 winRar 的命令行命令来解压缩

注意:

1、winRar命令使用,在dos下输入 unrar 就可以看到全部的命令说明。该命令在winRar的安装目录下

2、winRar命令行命令的路径问题,也就是path。要么加入系统变量path中,要么在winRar的安装目录下执行程序

以下是程序代码,解压 test.rar 到当前目录下,密码123

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class TestRunTime {

public static void main(String[] args) {

Runtime run = Runtime.getRuntime();

try {

Process p = run.exec("unrar e test.rar -p123");//执行解压缩命令

BufferedInputStream in = new BufferedInputStream(p.getInputStream());

BufferedReader inBr = new BufferedReader(new InputStreamReader(in));

String lineStr;

while ((lineStr = inBr.readLine()) != null)

System.out.println(lineStr);

// 检查命令是否执行失败。

if (p.waitFor() != 0) {

if (p.exitValue() == 1)// p.exitValue()==0表示正常结束,1:非正常结束

System.err.println("命令执行失败!");

}

} catch (Exception e) {

e.printStackTrace();

}

}

}


分享题目:java代码解压 java 解压gz
URL标题:http://cdkjz.cn/article/dohddji.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220