java讀取rar代碼,讀取文件java代碼

如何在java中解壓zip和rar文件

java中有zip包,可以使用

創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設,日土企業(yè)網(wǎng)站建設,日土品牌網(wǎng)站建設,網(wǎng)站定制,日土網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,日土網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

public void getZipFiles(String zipFile, String destFolder) throws IOException {

BufferedOutputStream dest = null;

ZipInputStream zis = new ZipInputStream(

new BufferedInputStream(

new FileInputStream(zipFile)));

ZipEntry entry;

while (( entry = zis.getNextEntry() ) != null) {

System.out.println( "Extracting: " + entry.getName() );

int count;

byte data[] = new byte[BUFFER];

if (entry.isDirectory()) {

new File( destFolder + "/" + entry.getName() ).mkdirs();

continue;

} else {

int di = entry.getName().lastIndexOf( '/' );

if (di != -1) {

new File( destFolder + "/" + entry.getName()

.substring( 0, di ) ).mkdirs();

}

}

FileOutputStream fos = new FileOutputStream( destFolder + "/"

+ entry.getName() );

dest = new BufferedOutputStream( fos );

while (( count = zis.read( data ) ) != -1)

dest.write( data, 0, count );

dest.flush();

dest.close();

}

}

rar的只能用第三方api,比如junrar

java讀取壓縮文件并壓縮

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.List;

import java.util.zip.ZipEntry;

import java.util.zip.ZipInputStream;

import java.util.zip.ZipOutputStream;

public class JZip {

public static int iCompressLevel;//壓縮比 取值范圍為0~9

public static boolean bOverWrite;//是否覆蓋同名文件 取值范圍為True和False

@SuppressWarnings("unchecked")

private static ArrayList AllFiles = new ArrayList();

public static String sErrorMessage;

private String zipFilePath;

public ListFile srcMap;

public JZip () {

iCompressLevel = 9;

// bOverWrite=true;

}

public JZip(String zipFilePath) throws FileNotFoundException, IOException {

this.zipFilePath = zipFilePath;

}

@SuppressWarnings("unchecked")

public static ArrayList extract (String sZipPathFile , String sDestPath) {

ArrayList AllFileName = new ArrayList();

try {

//先指定壓縮檔的位置和檔名,建立FileInputStream對象

FileInputStream fins = new FileInputStream(sZipPathFile);

//將fins傳入ZipInputStream中

ZipInputStream zins = new ZipInputStream(fins);

ZipEntry ze = null;

byte ch[] = new byte[256];

while ((ze = zins.getNextEntry()) != null) {

File zfile = new File(sDestPath + ze.getName());

File fpath = new File(zfile.getParentFile().getPath());

if (ze.isDirectory()) {

if (!zfile.exists())

zfile.mkdirs();

zins.closeEntry();

} else {

if (!fpath.exists())

fpath.mkdirs();

FileOutputStream fouts = new FileOutputStream(zfile);

int i;

AllFileName.add(zfile.getAbsolutePath());

while ((i = zins.read(ch)) != -1)

fouts.write(ch, 0, i);

zins.closeEntry();

fouts.close();

}

}

fins.close();

zins.close();

sErrorMessage = "OK";

} catch (Exception e) {

System.err.println("Extract error:" + e.getMessage());

sErrorMessage = e.getMessage();

}

AllFiles.clear();

return AllFileName;

}

@SuppressWarnings({ "unchecked", "static-access" })

public static void compress (String sPathFile , boolean bIsPath , String sZipPathFile) {

try {

String sPath;

//先指定壓縮檔的位置及檔名,建立一個FileOutputStream

FileOutputStream fos = new FileOutputStream(sZipPathFile);

//建立ZipOutputStream并將fos傳入

ZipOutputStream zos = new ZipOutputStream(fos);

//設置壓縮比

zos.setLevel(iCompressLevel);

if (bIsPath == true) {

searchFiles(sPathFile);

sPath = sPathFile;

} else {

File myfile = new File(sPathFile);

sPath = sPathFile.substring(0, sPathFile.lastIndexOf(myfile.separator) + 1);

AllFiles.add(myfile);

}

Object[] myobject = AllFiles.toArray();

ZipEntry ze = null;

//每個檔案要壓縮,都要透過ZipEntry來處理

FileInputStream fis = null;

BufferedReader in = null;

//byte[] ch = new byte[256];

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

File myfile = (File) myobject[i];

if (myfile.isFile()) {

in = new BufferedReader(new InputStreamReader(new FileInputStream(myfile.getPath()),"iso8859-1"));

//以檔案的名字當Entry,也可以自己再加上額外的路徑

//例如 ze=new ZipEntry("test\\"+myfiles[i].getName());

//如此壓縮檔內的每個檔案都會加test這個路徑

ze = new ZipEntry(myfile.getPath().substring((sPath).length()));

//將ZipEntry透過ZipOutputStream的putNextEntry的方式送進去處理

fis = new FileInputStream(myfile);

zos.putNextEntry(ze);

int len = 0;

//開始將原始檔案讀進ZipOutputStream

while ((len = in.read()) != -1) {

zos.write(len);

}

fis.close();

zos.closeEntry();

}

}

zos.close();

fos.close();

AllFiles.clear();

sErrorMessage = "OK";

} catch (Exception e) {

System.err.println("Compress error:" + e.getMessage());

sErrorMessage = e.getMessage();

}

}

/*

這是一個遞歸過程,功能是檢索出所有的文件名稱

dirstr:目錄名稱

*/

@SuppressWarnings("unchecked")

private static void searchFiles (String dirstr) {

File tempdir = new File(dirstr);

if (tempdir.exists()) {

if (tempdir.isDirectory()) {

File[] tempfiles = tempdir.listFiles();

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

if (tempfiles[i].isDirectory())

searchFiles(tempfiles[i].getPath());

else {

AllFiles.add(tempfiles[i]);

}

}

} else {

AllFiles.add(tempdir);

}

}

}

public String getZipFilePath() {

return zipFilePath;

}

public void setZipFilePath(String zipFilePath) {

this.zipFilePath = zipFilePath;

}

/**

* 解析zip文件得到文件名

* @return

* @throws FileNotFoundException

* @throws IOException

*/

public boolean parserZip() throws FileNotFoundException, IOException {

FileInputStream fis = new FileInputStream(zipFilePath);

ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));

ZipEntry entry;

try {

srcMap = new ArrayListFile();

while ((entry = zis.getNextEntry()) != null) {

File file = new File(zipFilePath + File.separator + entry.getName());

srcMap.add(file);

}

zis.close();

fis.close();

return true;

} catch (IOException e) {

return false;

}

}

/**

*

* @param zipFileName 待解壓縮的ZIP文件

* @param extPlace 解壓后的文件夾

*/

public static void extZipFileList(String zipFileName, String extPlace) {

try {

ZipInputStream in = new ZipInputStream(new FileInputStream(

zipFileName));

File files = new File(extPlace);

files.mkdirs();

ZipEntry entry = null;

while ((entry = in.getNextEntry()) != null) {

String entryName = entry.getName();

if (entry.isDirectory()) {

File file = new File(files + entryName);

file.mkdirs();

System.out.println("創(chuàng)建文件夾:" + entryName);

} else {

OutputStream os = new FileOutputStream(files+File.separator + entryName);

// Transfer bytes from the ZIP file to the output file

byte[] buf = new byte[1024];

int len;

while ((len = in.read(buf)) 0) {

os.write(buf, 0, len);

}

os.close();

in.closeEntry();

System.out.println("解壓文件:" + entryName);

}

}

} catch (IOException e) {

}

}

@SuppressWarnings("static-access")

public static void main(String args[]){

}

}

java 調用rar命令 對rar文件進行加減密,提供思路即可!!

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.util.Enumeration;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

public class Test {

Enumeration enu = null;// 創(chuàng)建一個枚舉型引用

ZipFile zf = null; // 創(chuàng)建一個Zip文件引用

ZipEntry ze = null;// 創(chuàng)建一個進行入Zip文件的引用

BufferedInputStream bis = null; // 創(chuàng)建一個字節(jié)型輸入流

BufferedOutputStream bos = null;// 創(chuàng)建一個字節(jié)型輸出流

public void zipFiles(){

try {

File f = new File("d:\\line.zip");

zf = new ZipFile(f);

enu = zf.entries();

while (enu.hasMoreElements()) {

ze = (ZipEntry) enu.nextElement();

if (ze.isDirectory()) {

new File("d:\\" + ze.getName()).mkdirs();

continue;

}

int count;

byte[] buffer = new byte[2048];

int bu = 2048;

bis = new BufferedInputStream(zf.getInputStream(ze));

FileOutputStream fos = new FileOutputStream("d:\\"

+ ze.getName());

bos = new BufferedOutputStream(fos, bu);

while ((count = bis.read()) != -1) {

bos.write(buffer, 0, count);

}

bos.flush();

bos.close();

bis.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

new Test().zipFiles();

}

}

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++){

//創(chuàng)建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下查看,你會發(fā)現(xiàn)生成了一個zip壓縮包.

java怎么讀取Zip和RAR里面的文件?。?/h2>

java.util.zip.*這個包下有ZipInputStream和ZipOutputStream這兩個類,用于ZIP的讀入解壓,和生成ZIP文件的打包。

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.Enumeration;

import java.util.List;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

import java.util.zip.ZipOutputStream;

import java.util.zip.GZIPInputStream;

import java.io.DataInputStream;

/**

* Description: 此類用于...

*

* @author wunaigang(2005-6-21)

* @version 1.0.0

*/

public class ZipManager {

/**

* zip壓縮功能測試. 將d:\\temp\\zipout目錄下的所有文件連同子目錄壓縮到d:\\temp\\out.zip.

*

* @param baseDir 所要壓縮的目錄名(包含絕對路徑)

* @param objFileName 壓縮后的文件名

* @throws Exception

*/

public void createZip(String baseDir, String objFileName) throws Exception {

File folderObject = new File(baseDir);

if (folderObject.exists()){

List fileList = getSubFiles(new File(baseDir));

//壓縮文件名

ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(objFileName));

ZipEntry ze = null;

byte[] buf = new byte[1024];

int readLen = 0;

for (int i = 0; i fileList.size(); i++) {

File f = (File) fileList.get(i);

System.out.println("Adding: " + f.getPath() + f.getName());

//創(chuàng)建一個ZipEntry,并設置Name和其它的一些屬性

ze = new ZipEntry(getAbsFileName(baseDir, f));

ze.setSize(f.length());

ze.setTime(f.lastModified());

//將ZipEntry加到zos中,再寫入實際的文件內容

zos.putNextEntry(ze);

InputStream is = new BufferedInputStream(new FileInputStream(f));

while ((readLen = is.read(buf, 0, 1024)) != -1) {

zos.write(buf, 0, readLen);

}

is.close();

System.out.println("done...");

}

zos.close();

}else{

throw new Exception("this folder isnot exist!");

}

}

/**

* zip壓縮功能測試. 將指定文件壓縮后存到一壓縮文件中

*

* @param baseDir 所要壓縮的文件名

* @param objFileName 壓縮后的文件名

* @return 壓縮后文件的大小

* @throws Exception

*/

public long createFileToZip(String zipFilename,String sourceFileName) throws Exception {

File sourceFile = new File(sourceFileName);

byte[] buf = new byte[1024];

//壓縮文件名

File objFile = new File(zipFilename);

ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(objFile));

ZipEntry ze = null;

//創(chuàng)建一個ZipEntry,并設置Name和其它的一些屬性

ze = new ZipEntry(sourceFile.getName());

ze.setSize(sourceFile.length());

ze.setTime(sourceFile.lastModified());

//將ZipEntry加到zos中,再寫入實際的文件內容

zos.putNextEntry(ze);

InputStream is = new BufferedInputStream(new FileInputStream(sourceFile));

int readLen = -1;

while ((readLen = is.read(buf, 0, 1024)) != -1) {

zos.write(buf, 0, readLen);

}

is.close();

zos.close();

return objFile.length();

}

/**

* zip壓縮功能測試. 將指定文件壓縮后存到一壓縮文件中

*

* @param baseDir 所要壓縮的文件名

* @param objFileName 壓縮后的文件名

* @return 壓縮后文件的大小

* @throws Exception

*/

public long createFileToZip(File sourceFile,File zipFile)throws IOException {

byte[] buf = new byte[1024];

//壓縮文件名

File objFile = zipFile;

ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(objFile));

ZipEntry ze = null;

//創(chuàng)建一個ZipEntry,并設置Name和其它的一些屬性

ze = new ZipEntry(sourceFile.getName());

ze.setSize(sourceFile.length());

ze.setTime(sourceFile.lastModified());

//將ZipEntry加到zos中,再寫入實際的文件內容

zos.putNextEntry(ze);

InputStream is = new BufferedInputStream(new FileInputStream(sourceFile));

int readLen = -1;

while ((readLen = is.read(buf, 0, 1024)) != -1) {

zos.write(buf, 0, readLen);

}

is.close();

zos.close();

return objFile.length();

}

/**

* 測試解壓縮功能. 將d:\\download\\test.zip連同子目錄解壓到d:\\temp\\zipout目錄下.

*

* @throws Exception

*/

public void releaseZipToFile(String sourceZip, String outFileName)

throws IOException{

ZipFile zfile=new ZipFile(sourceZip);

System.out.println(zfile.getName());

Enumeration zList=zfile.entries();

ZipEntry ze=null;

byte[] buf=new byte[1024];

while(zList.hasMoreElements()){

//從ZipFile中得到一個ZipEntry

ze=(ZipEntry)zList.nextElement();

if(ze.isDirectory()){

continue;

}

//以ZipEntry為參數(shù)得到一個InputStream,并寫到OutputStream中

OutputStream os=new BufferedOutputStream(new FileOutputStream(getRealFileName(outFileName, ze.getName())));

InputStream is=new BufferedInputStream(zfile.getInputStream(ze));

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 baseDir

* File 指定的目錄

* @return 包含java.io.File的List

*/

private List getSubFiles(File baseDir) {

List ret = new ArrayList();

//File base=new File(baseDir);

File[] tmp = baseDir.listFiles();

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

if (tmp[i].isFile()) {

ret.add(tmp[i]);

}

if (tmp[i].isDirectory()) {

ret.addAll(getSubFiles(tmp[i]));

}

}

return ret;

}

/**

* 給定根目錄,返回一個相對路徑所對應的實際文件名.

*

* @param baseDir

* 指定根目錄

* @param absFileName

* 相對路徑名,來自于ZipEntry中的name

* @return java.io.File 實際的文件

*/

private File getRealFileName(String baseDir, String absFileName) {

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

//System.out.println(dirs.length);

File ret = new File(baseDir);

//System.out.println(ret);

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]);

return ret;

}

/**

* 給定根目錄,返回另一個文件名的相對路徑,用于zip文件中的路徑.

*

* @param baseDir

* java.lang.String 根目錄

* @param realFileName

* java.io.File 實際的文件名

* @return 相對文件名

*/

private String getAbsFileName(String baseDir, File realFileName) {

File real = realFileName;

File base = new File(baseDir);

String ret = real.getName();

while (true) {

real = real.getParentFile();

if (real == null)

break;

if (real.equals(base))

break;

else {

ret = real.getName() + "/" + ret;

}

}

System.out.println("TTTTT" + ret);

return ret;

}

public void testReadZip() throws Exception{

String baseDir="d:\\temp\\zipout";

ZipFile zfile=new ZipFile("d:\\download\\src.zip");

System.out.println(zfile.getName());

Enumeration zList=zfile.entries();

ZipEntry ze=null;

byte[] buf=new byte[1024];

while(zList.hasMoreElements()){

//從ZipFile中得到一個ZipEntry

ze=(ZipEntry)zList.nextElement();

if(ze.isDirectory()){

continue;

}

//以ZipEntry為參數(shù)得到一個InputStream,并寫到OutputStream中

OutputStream os=new BufferedOutputStream(new FileOutputStream(getRealFileName(baseDir, ze.getName())));

InputStream is=new BufferedInputStream(zfile.getInputStream(ze));

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();

}

public static void main(String args[]){

ZipManager manager = new ZipManager();

try {

//manager.releaseZipToFile("c:\\test.zip","c:\\test");

manager.testReadZip();

}

catch (Exception e) {}

System.out.println("over");

}

}

如何用java讀取客戶端上傳的rar文件

B/S結構的環(huán)境中?那最好不要這樣設計,,,,使用applet是可以做到,但要求權限放得很低,并且目前的瀏覽器很麻煩配置。

可以考慮post到服務器端處理

新聞名稱:java讀取rar代碼,讀取文件java代碼
當前網(wǎng)址:http://muchs.cn/article42/hcjehc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站設計公司網(wǎng)站內鏈、品牌網(wǎng)站設計、品牌網(wǎng)站建設服務器托管

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設