javarar,JAVArar怎麼運行

本文目錄一覽:

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可以將文件壓縮成rar格式的嗎

可以,壓縮只是一種算法,什麼語言都可以,比如某種格式的文件中1001010(二進制)代表漢子的”中”字,那麼壓縮算法就是在編碼不衝突的情況下可以改變編碼長度,比如壓縮之後中字變成1010,這樣就節省空間了,這是我隨便舉的例子,具體的對應算法可以網上查

java怎麼獲得rar和zip文件的壓縮率

/**

* 計算ZIP文件的解壓大小

*

* @param inputStream 輸入流

* 註:輸入流由調用方負責關閉

* @return 解壓大小

*/

public static long getUncompressedSize(InputStream inputStream) throws IOException {

ZipInputStream zipInputStream = new ZipInputStream(inputStream, Charset.forName(“GBK”));

byte[] buffer = new byte[8*1024*1024];

long uncompressedSize = 0;

ZipEntry zipEntry;

while ((zipEntry = zipInputStream.getNextEntry()) != null) {

long size = zipEntry.getSize();

// ZipEntry的size可能為-1,表示未知

if (size == -1) {

int len;

while ((len = zipInputStream.read(buffer, 0, buffer.length)) != -1) {

uncompressedSize += len;

}

} else {

uncompressedSize += size;

}

zipInputStream.closeEntry();

}

return uncompressedSize;

}

目前只找到這個,zip的,rar的沒找到

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

直接通過工具類進行解壓或者壓縮文件即可。

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.Closeable;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.Enumeration;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

/**

*

* @author gdb

*/

public class ZipUtilAll {

public static final int DEFAULT_BUFSIZE = 1024 * 16;

/**

* 解壓Zip文件

*

* @param srcZipFile

* @param destDir

* @throws IOException

*/

public static void unZip(File srcZipFile, String destDir) throws IOException

{

ZipFile zipFile = new ZipFile(srcZipFile);

unZip(zipFile, destDir);

}

/**

* 解壓Zip文件

*

* @param srcZipFile

* @param destDir

* @throws IOException

*/

public static void unZip(String srcZipFile, String destDir) throws IOException

{

ZipFile zipFile = new ZipFile(srcZipFile);

unZip(zipFile, destDir);

}

/**

* 解壓Zip文件

*

* @param zipFile

* @param destDir

* @throws IOException

*/

public static void unZip(ZipFile zipFile, String destDir) throws IOException

{

Enumeration? extends ZipEntry entryEnum = zipFile.entries();

ZipEntry entry = null;

while (entryEnum.hasMoreElements()) {

entry = entryEnum.nextElement();

File destFile = new File(destDir + entry.getName());

if (entry.isDirectory()) {

destFile.mkdirs();

}

else {

destFile.getParentFile().mkdirs();

InputStream eis = zipFile.getInputStream(entry);

System.out.println(eis.read());

write(eis, destFile);

}

}

}

/**

* 將輸入流中的數據寫到指定文件

*

* @param inputStream

* @param destFile

*/

public static void write(InputStream inputStream, File destFile) throws IOException

{

BufferedInputStream bufIs = null;

BufferedOutputStream bufOs = null;

try {

bufIs = new BufferedInputStream(inputStream);

bufOs = new BufferedOutputStream(new FileOutputStream(destFile));

byte[] buf = new byte[DEFAULT_BUFSIZE];

int len = 0;

while ((len = bufIs.read(buf, 0, buf.length)) 0) {

bufOs.write(buf, 0, len);

}

} catch (IOException ex) {

throw ex;

} finally {

close(bufOs, bufIs);

}

}

/**

* 安全關閉多個流

*

* @param streams

*/

public static void close(Closeable… streams)

{

try {

for (Closeable s : streams) {

if (s != null)

s.close();

}

} catch (IOException ioe) {

ioe.printStackTrace(System.err);

}

}

/**

* @param args

* @throws java.lang.Exception

*/

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

{

// unZip(new File(ZipDemo.class.getResource(“D:/123/HKRT-B2B.zip”).toURI()), “D:/123/”);

unZip(“D:/123/123.zip”, “D:/123/”);

// new File();

}

}

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

java中有zip包,可以使用

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

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/278259.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-19 13:21
下一篇 2024-12-19 13:21

相關推薦

  • Javarar解壓操作詳解

    一、什麼是Javarar? Javarar是一個Java語言實現的對RAR文件進行解壓操作的開源庫,可以在不同的平台上進行使用。 二、Javarar解壓的代碼實現 Javarar解…

    編程 2024-11-24

發表回復

登錄後才能評論