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

java解压rar

发布时间: 2022-01-11 15:34:35

1. 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压缩包.

2. java 解压rar文件

Java 解压rar文件需要用到apache的commons-compress-1.0.jar,这个类的使用如下:

packagecn.myapps.util.pdf;

importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.InputStream;
importjava.util.Enumeration;

importorg.apache.commons.compress.archivers.zip.ZipArchiveEntry;
importorg.apache.commons.compress.archivers.zip.ZipFile;

publicclassUnzipFile{

publicstaticvoidmain(String[]args){

try{
UnzipFile.unZip(newFile("D:/你我他.zip"),"D:/upzip/");
}catch(Exceptione){
e.printStackTrace();
}
}

publicstaticvoinZip(Filezip,Stringroot)throwsException{
try{
ZipFilezipFile=newZipFile(zip,"GBK");
Enumeratione=zipFile.getEntries();
bytech[]=newbyte[256];
while(e.hasMoreElements()){
ZipArchiveEntryzipEntry=(ZipArchiveEntry)e.nextElement();
Stringtemp=zipEntry.getName();
System.out.println("unziping"+zipEntry.getName());
Filezfile=newFile(root+temp);
Filefpath=newFile(zfile.getParentFile().getPath());

if(zipEntry.isDirectory()){
if(!zfile.exists())
zfile.mkdirs();
}else{
if(!fpath.exists())
fpath.mkdirs();
FileOutputStreamfouts=newFileOutputStream(zfile);
InputStreamin=zipFile.getInputStream(zipEntry);
inti;
while((i=in.read(ch))!=-1)
fouts.write(ch,0,i);
fouts.close();
in.close();
}
}

}catch(Exceptione){
System.err.println("ExceptionfromZipUtil->unZip():"
+e.getMessage());
e.printStackTrace(System.err);
throwe;
}
}
}

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

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

4. java压缩文件怎么解压

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

5. 求:解压.rar格式的压缩包的java代码

rar格式的 是收费的格式

以前我也曾去找解压rar 的文档,后来发现rar 的格式处理起来蛮麻烦的,而且后来一直在linux 平台上工作,接触的都是 zip 或者 tar这种格式

建议你使用zip 格式,很简单,winrar 一样可以将文件打成zip格式

这种格式 可以使用jdk 自带的 zip工具类 或者 ant 的工具类

6. JAVA 怎么打开RAR压缩文件

用 org.apache.tools.zip 来做,可以看看api很容易的其实不需要给代码,衣用就明

7. 在LINUX下 用JAVA如何解压rar文件

下载地址:http://www.rarsoft.com/download.htm(目前最新为RAR 3.71 for Linux)

以最新的为准。

对于Window下的常见压缩文件.zip和.rar,Linux也有相应的方法来解压它们:

1:对于.zip

linux下提供了zip和unzip程序,zip是压缩程序,unzip是解压程序。它们的参数选项很多,这里只做简单介绍,举例说明一下其用法:

# zip all.zip *.jpg(这条命令是将所有.jpg的文件压缩成一个zip包)

# unzip all.zip(这条命令是将all.zip中的所有文件解压出来)

2:对于.rar

要在linux下处理.rar文件,需要安装RAR for Linux,可以从网上下载,但要记住,RAR for Linux不是免费的;可从http://www.rarsoft.com/download.htm下载RAR 3。60 for Linux ,然后安装其安装操作如下:

# tar -xzpvf rarlinux-3.2.0.tar.gz
# cd rar
# make

这样就安装好了,安装后就有了rar和unrar这两个程序,rar是压缩程序,unrar是解压程序。它们的参数选项很多,举例说明一下其用法

# rar a all *.jpg

这条命令是将所有.jpg的文件压缩成一个rar包,名为all.rar,该程序会将.rar 扩展名将自动附加到包名后。

# unrar e all.rar

这条命令是将all.rar中的所有文件解压出来。

linux下的文件名引用的时候空格要加转义 比如

test file.rar 实际引用的时候就是 test\ file.rar

至于怎么解决 我就不再多说了哈

8. 怎么把rar解压缩文件解压成java解压缩文件

把后缀名.rar改成.jad就能用了

9. java解压zip文件

import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;

/**
* 获得zip文件里的所有文件
* @author Administrator
*
*/
public class ZipFile {

public ZipFile() throws IOException
{
java.util.zip.ZipFile zf = new java.util.zip.ZipFile("E:/Java/Project.zip");
Enumeration e = zf.entries();
while(e.hasMoreElements())
{
ZipEntry ze = (ZipEntry) e.nextElement();
if(!ze.isDirectory())
System.out.println(new String(ze.getName().getBytes("ISO-8859-1"), "GB2312"));
}
}
public static void main(String[] args) {
try {
new ZipFile();
} catch (IOException e) {
e.printStackTrace();
}
}

}

10. Java手机如何解压rar压缩包

手机型号能说下不 可以明确的告诉你 有这种软件 NOKIA的塞班系统可以安装的

热点内容
云服务器免费领 发布:2024-09-29 00:11:23 浏览:325
向数据库中添加的表 发布:2024-09-29 00:10:00 浏览:624
a梦源码站 发布:2024-09-29 00:00:49 浏览:265
mac颜色文件夹 发布:2024-09-28 23:54:14 浏览:817
signsql 发布:2024-09-28 23:46:50 浏览:917
梅林远程访问路由器 发布:2024-09-28 22:52:56 浏览:464
怎么让电脑不存储临时文件 发布:2024-09-28 22:44:59 浏览:657
php中替换字符串 发布:2024-09-28 22:01:48 浏览:582
sql数据库的使用 发布:2024-09-28 22:01:41 浏览:628
你们的wifi密码多少 发布:2024-09-28 21:53:07 浏览:377