当前位置:首页 » 文件管理 » java解压文件

java解压文件

发布时间: 2022-01-09 00:23:59

java中怎么用cmd命令解压zip文件

对于zip文件,java有自带类库java.util.zip;可是要想解压rar文件只能靠第三方类库,我试过两个:com.github.junrar和de.innosystec.unrar,前者解压时可能会出现crcError,后者pom配置时报错;利用cmd命令调用winRAR进行解压,无疑方便快捷很多。

调用cmd命令

public static boolean exe(String cmd) {
Runtime runtime = Runtime.getRuntime(); try {
Process p = runtime.exec(cmd);
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(),"GBK"));

String line = reader.readLine(); while(line!=null) {
logger.info(line);
line = reader.readLine();
}
reader.close(); if(p.waitFor()!=0) { return false;
}
} catch (IOException e) { // TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) { // TODO Auto-generated catch block
e.printStackTrace();
} return true;
}

首先利用runtime.exec()执行指令,得到process,从process.getInputStream()中获取回显字符并打印,打印回显时可能会出现中文乱码,这个和操作系统编码有关,我这里是GBK编码,所以在new inputstreamReader时加入了编码参数”GBK“

命令行字符串

如果需要调用cmd命令,如cd等,可写”cmd c cd 目录”。对于直接调用exe执行,则可以写成”exe文件绝对路径 参数”,在命令行字符串中,含有空格的路径或者字符串应该再加上引号,即””exe文件绝对路径” ”参数”“

winRAR调用

我这里安装目录是C:/Program Files/WinRAR,将D:1.rar 解压到D:,则写成””C:/Program Files/WinRAR/unRar.exe” x -y D:/1.rar D:/”,x代表绝对路径解压,-y表示全部确定;压缩的命令如下:“”C:/Program Files/WinRAR/rar.exe” a -ep1 D:2.rar D:源目录”,a表示添加文件到压缩文件,-ep1表示排除基本目录,如D:winrar ar这个目录,如果没有-ep1那么压缩包中会出现winrar目录路径,而加了之后就只将当前目录打包,只有rar目录

Ⅱ 如何通过java,不进行解压zip/rar文件操作,就把压缩文件中的文件名给读取出来求可行的思路!谢谢!

可以不解压,zip包里的一个对象就是一个ZipEntry
找到你想要的那个ZipEntry,用文流写出来就可以了。

Ⅲ java 解压文件

给你找了一个 你参考一下吧:
package com.da.unzip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Unzip {

public static void main(String[] args) throws Exception {

Unzip unzip = new Unzip();
String zippath = "C:\\unzip\\";// /解压到的目标文件路径
String zipDir = "C:\\data\\";// 要解压的压缩文件的存放路径

File file = new File(zipDir);
List list = unzip.getSubFiles(file);
for (Object obj : list) {
String realname = ((File)obj).getName();
System.out.println(realname);
int end = realname.lastIndexOf(".");
System.out.println("要解压缩的文件名.........."+zipDir+realname);
System.out.println("解压到的目录" +zippath+realname.substring(0, end));
unzip.testReadZip(zippath,zipDir+realname);
}

}

/*
* 解压缩功能. 将zippath目录文件解压到unzipPath目录下. @throws Exception
*/
public void ReadZip(String zippath, String unzipPath) throws Exception {

ZipFile zfile = new ZipFile(unzipPath);// 生成一个zip文件对象

System.out.println(zfile.getName());// 获取要解压的zip的文件名全路径

Enumeration zList = zfile.entries();// 返回枚举对象

ZipEntry ze = null;// 用于表示 ZIP 文件条目

byte[] buf = new byte[1024];// 声明字节数组
/**
* 循环获取zip文件中的每一个文件
*/
while (zList.hasMoreElements()) {
// 从ZipFile中得到一个ZipEntry
ze = (ZipEntry) zList.nextElement();
if (ze.isDirectory())// 如果为目录条目,则返回 true,执行下列语句
{
System.out.println("Dir: " + ze.getName() + " skipped..");
continue;
}
int begin = zfile.getName().lastIndexOf("\\") + 1;
int end = zfile.getName().lastIndexOf(".");
String zipRealName = zfile.getName().substring(begin, end);
System.out.println("解压缩开始Extracting:"+ze.getName()+"\t"+ze.getSize()+"\t"+ze.getCompressedSize());
// 以ZipEntry为参数得到一个InputStream,并写到OutputStream中,并加上缓冲
OutputStream os = new BufferedOutputStream(
new FileOutputStream(getRealFileName(zippath + "\\"
+ zipRealName, ze.getName())));
InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
String fileName = getRealFileName(zippath, ze.getName()).getName();
System.out.println("解压出的文件名称:" + fileName);
int readLen = 0;
while ((readLen = is.read(buf, 0, 1024)) != -1) {
os.write(buf, 0, readLen);
}
is.close();
os.close();
// System.out.println("解压缩结束Extracted: "+ze.getName());
}
zfile.close();
}

/**
* 给定根目录,返回一个相对路径所对应的实际文件名.
*
* @param zippath
* 指定根目录
* @param absFileName
* 相对路径名,来自于ZipEntry中的name
* @return java.io.File 实际的文件
*/
private File getRealFileName(String zippath, String absFileName) {

String[] dirs = absFileName.split("/", absFileName.length());

File ret = new File(zippath);// 创建文件对象

if (dirs.length > 1) {
for (int i = 0; i < dirs.length - 1; i++) {
ret = new File(ret, dirs[i]);

}
}

if (!ret.exists()) {// 检测文件是否存在
ret.mkdirs();// 创建此抽象路径名指定的目录
}
ret = new File(ret, dirs[dirs.length - 1]);// 根据 ret 抽象路径名和 child
// 路径名字符串创建一个新 File 实例

return ret;
}

}

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

1.代码如下:
[java] view plain
<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.length<1){
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;i<sourceFiles.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压缩和解压缩:
http://blog.csdn.net/gaowen_han/article/details/7163737/

Ⅵ JAVA怎么把zip文件解压到指定位置

刚好我在项目中用到了,送给你,希望你能用上。

/**
* 解压,处理下载的zip工具包文件
*
* @param directory
* 要解压到的目录
* @param zip
* 工具包文件
*
* @throws Exception
* 操作失败时抛出异常
*/
public static void unzipFile(String directory, File zip) throws Exception
{
try
{
ZipInputStream zis = new ZipInputStream(new FileInputStream(zip));
ZipEntry ze = zis.getNextEntry();
File parent = new File(directory);
if (!parent.exists() && !parent.mkdirs())
{
throw new Exception("创建解压目录 \"" + parent.getAbsolutePath() + "\" 失败");
}
while (ze != null)
{
String name = ze.getName();
File child = new File(parent, name);
FileOutputStream output = new FileOutputStream(child);
byte[] buffer = new byte[10240];
int bytesRead = 0;
while ((bytesRead = zis.read(buffer)) > 0)
{
output.write(buffer, 0, bytesRead);
}
output.flush();
output.close();
ze = zis.getNextEntry();
}
zis.close();
}
catch (IOException e)
{
}
}

Ⅶ java zip解压

如果把out.close()写在注释处,那么意味着while循环中创建的out输出流对象没有关闭,要知道,如果这个流没有关闭,那么该流缓冲区中的数据不会被刷新到实际目标文件中。
因此只有最后一个文件有内容(因为out被关闭时指向最后一个文件)。

Ⅷ java压缩文件怎么解压

后缀名为.jar的文件不要解压,直接在java中运行就好了

Ⅸ 怎样用java快速实现zip文件的压缩解压缩

packagezip;
importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.util.Enumeration;
importjava.util.zip.CRC32;
importjava.util.zip.CheckedOutputStream;
importjava.util.zip.ZipEntry;
importjava.util.zip.ZipFile;
importjava.util.zip.ZipOutputStream;
importorg.apache.commons.lang3.StringUtils;
publicclassZipUtil{

/**
*递归压缩文件夹
*@paramsrcRootDir压缩文件夹根目录的子路径
*@paramfile当前递归压缩的文件或目录对象
*@paramzos压缩文件存储对象
*@throwsException
*/
privatestaticvoidzip(StringsrcRootDir,Filefile,ZipOutputStreamzos)throwsException
{
if(file==null)
{
return;
}

//如果是文件,则直接压缩该文件
if(file.isFile())
{
intcount,bufferLen=1024;
bytedata[]=newbyte[bufferLen];

//获取文件相对于压缩文件夹根目录的子路径
StringsubPath=file.getAbsolutePath();
intindex=subPath.indexOf(srcRootDir);
if(index!=-1)
{
subPath=subPath.substring(srcRootDir.length()+File.separator.length());
}
ZipEntryentry=newZipEntry(subPath);
zos.putNextEntry(entry);
BufferedInputStreambis=newBufferedInputStream(newFileInputStream(file));
while((count=bis.read(data,0,bufferLen))!=-1)
{
zos.write(data,0,count);
}
bis.close();
zos.closeEntry();
}
//如果是目录,则压缩整个目录
else
{
//压缩目录中的文件或子目录
File[]childFileList=file.listFiles();
for(intn=0;n<childFileList.length;n++)
{
childFileList[n].getAbsolutePath().indexOf(file.getAbsolutePath());
zip(srcRootDir,childFileList[n],zos);
}
}
}

/**
*对文件或文件目录进行压缩
*@paramsrcPath要压缩的源文件路径。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
*@paramzipPath压缩文件保存的路径。注意:zipPath不能是srcPath路径下的子文件夹
*@paramzipFileName压缩文件名
*@throwsException
*/
publicstaticvoidzip(StringsrcPath,StringzipPath,StringzipFileName)throwsException
{
if(StringUtils.isEmpty(srcPath)||StringUtils.isEmpty(zipPath)||StringUtils.isEmpty(zipFileName))
{
thrownewParameterException(ICommonResultCode.PARAMETER_IS_NULL);
}
CheckedOutputStreamcos=null;
ZipOutputStreamzos=null;
try
{
FilesrcFile=newFile(srcPath);

//判断压缩文件保存的路径是否为源文件路径的子文件夹,如果是,则抛出异常(防止无限递归压缩的发生)
if(srcFile.isDirectory()&&zipPath.indexOf(srcPath)!=-1)
{
thrownewParameterException(ICommonResultCode.INVALID_PARAMETER,".");
}

//判断压缩文件保存的路径是否存在,如果不存在,则创建目录
FilezipDir=newFile(zipPath);
if(!zipDir.exists()||!zipDir.isDirectory())
{
zipDir.mkdirs();
}

//创建压缩文件保存的文件对象
StringzipFilePath=zipPath+File.separator+zipFileName;
FilezipFile=newFile(zipFilePath);
if(zipFile.exists())
{
//检测文件是否允许删除,如果不允许删除,将会抛出SecurityException
=newSecurityManager();
securityManager.checkDelete(zipFilePath);
//删除已存在的目标文件
zipFile.delete();
}

cos=newCheckedOutputStream(newFileOutputStream(zipFile),newCRC32());
zos=newZipOutputStream(cos);

//如果只是压缩一个文件,则需要截取该文件的父目录
StringsrcRootDir=srcPath;
if(srcFile.isFile())
{
intindex=srcPath.lastIndexOf(File.separator);
if(index!=-1)
{
srcRootDir=srcPath.substring(0,index);
}
}
//调用递归压缩方法进行目录或文件压缩
zip(srcRootDir,srcFile,zos);
zos.flush();
}
catch(Exceptione)
{
throwe;
}
finally
{
try
{
if(zos!=null)
{
zos.close();
}
}
catch(Exceptione)
{
e.printStackTrace();
}
}
}

/**
*解压缩zip包
*@paramzipFilePathzip文件的全路径
*@paramunzipFilePath解压后的文件保存的路径
*@paramincludeZipFileName解压后的文件保存的路径是否包含压缩文件的文件名。true-包含;false-不包含
*/
@SuppressWarnings("unchecked")
publicstaticvoinzip(StringzipFilePath,StringunzipFilePath,booleanincludeZipFileName)throwsException
{
if(StringUtils.isEmpty(zipFilePath)||StringUtils.isEmpty(unzipFilePath))
{
thrownewParameterException(ICommonResultCode.PARAMETER_IS_NULL);
}
FilezipFile=newFile(zipFilePath);
//如果解压后的文件保存路径包含压缩文件的文件名,则追加该文件名到解压路径
if(includeZipFileName)
{
StringfileName=zipFile.getName();
if(StringUtils.isNotEmpty(fileName))
{
fileName=fileName.substring(0,fileName.lastIndexOf("."));
}
unzipFilePath=unzipFilePath+File.separator+fileName;
}
//创建解压缩文件保存的路径
FileunzipFileDir=newFile(unzipFilePath);
if(!unzipFileDir.exists()||!unzipFileDir.isDirectory())
{
unzipFileDir.mkdirs();
}

//开始解压
ZipEntryentry=null;
StringentryFilePath=null,entryDirPath=null;
FileentryFile=null,entryDir=null;
intindex=0,count=0,bufferSize=1024;
byte[]buffer=newbyte[bufferSize];
BufferedInputStreambis=null;
BufferedOutputStreambos=null;
ZipFilezip=newZipFile(zipFile);
Enumeration<ZipEntry>entries=(Enumeration<ZipEntry>)zip.entries();
//循环对压缩包里的每一个文件进行解压
while(entries.hasMoreElements())
{
entry=entries.nextElement();
//构建压缩包中一个文件解压后保存的文件全路径
entryFilePath=unzipFilePath+File.separator+entry.getName();
//构建解压后保存的文件夹路径
index=entryFilePath.lastIndexOf(File.separator);
if(index!=-1)
{
entryDirPath=entryFilePath.substring(0,index);
}
else
{
entryDirPath="";
}
entryDir=newFile(entryDirPath);
//如果文件夹路径不存在,则创建文件夹
if(!entryDir.exists()||!entryDir.isDirectory())
{
entryDir.mkdirs();
}

//创建解压文件
entryFile=newFile(entryFilePath);
if(entryFile.exists())
{
//检测文件是否允许删除,如果不允许删除,将会抛出SecurityException
=newSecurityManager();
securityManager.checkDelete(entryFilePath);
//删除已存在的目标文件
entryFile.delete();
}

//写入文件
bos=newBufferedOutputStream(newFileOutputStream(entryFile));
bis=newBufferedInputStream(zip.getInputStream(entry));
while((count=bis.read(buffer,0,bufferSize))!=-1)
{
bos.write(buffer,0,count);
}
bos.flush();
bos.close();
}
}

publicstaticvoidmain(String[]args)
{
StringzipPath="d:\ziptest\zipPath";
Stringdir="d:\ziptest\rawfiles";
StringzipFileName="test.zip";
try
{
zip(dir,zipPath,zipFileName);
}
catch(Exceptione)
{
e.printStackTrace();
}

StringzipFilePath="D:\ziptest\zipPath\test.zip";
StringunzipFilePath="D:\ziptest\zipPath";
try
{
unzip(zipFilePath,unzipFilePath,true);
}
catch(Exceptione)
{
e.printStackTrace();
}
}
}

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

热点内容
ta栅格算法 发布:2024-05-07 07:03:23 浏览:802
符号源码 发布:2024-05-07 06:26:09 浏览:707
玩hypixel服务器ip地址要什么版本 发布:2024-05-07 06:22:50 浏览:62
代码为什么要编译 发布:2024-05-07 06:22:48 浏览:495
java面试复习 发布:2024-05-07 06:01:15 浏览:658
suftp 发布:2024-05-07 06:00:40 浏览:880
编程的tr 发布:2024-05-07 05:37:25 浏览:423
苹果4s的数据怎么备份到安卓上 发布:2024-05-07 05:37:15 浏览:819
安卓怎么注册电邮 发布:2024-05-07 05:23:49 浏览:715
怎么看清被涂鸦的内容安卓手机 发布:2024-05-07 05:16:52 浏览:703