當前位置:首頁 » 文件管理 » 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 01:40:21 瀏覽:198
晨曦軟體加密狗 發布:2024-09-29 01:40:18 瀏覽:307
浙江大學我的世界伺服器雲伺服器 發布:2024-09-29 01:40:14 瀏覽:63
怎麼存儲鮮姜 發布:2024-09-29 01:39:34 瀏覽:223
安卓手機如何降低白屏點 發布:2024-09-29 01:39:31 瀏覽:341
extjs4多文件上傳 發布:2024-09-29 01:34:32 瀏覽:897
安卓機按鍵怎麼隱藏 發布:2024-09-29 01:34:29 瀏覽:689
加密u盤軟體下載 發布:2024-09-29 01:15:26 瀏覽:87
伺服器啟動後如何掛存儲 發布:2024-09-29 01:01:50 瀏覽:468
全編譯運行 發布:2024-09-29 01:01:28 瀏覽:75