Java實(shí)現(xiàn)文件的歸檔和解檔
更新時間:2019年09月26日 09:03:32 投稿:lijiao
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)文件的歸檔和解檔,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了Java實(shí)現(xiàn)文件歸檔和解檔的具體代碼,供大家參考,具體內(nèi)容如下
文件的歸檔
package cn.yimen.archiver;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* 歸檔器
*/
public class Archiver {
public static void main(String[] args) throws Exception {
//
FileOutputStream fos = new FileOutputStream("d:/arch/x.xar");
fos.write(addFile("D:/arch/a.xls"));
fos.write(addFile("D:/arch/b.xml"));
fos.write(addFile("D:/arch/c.jpg"));
fos.close();
//
}
/**
* path : d:/xxx/xxx/a.jpg
* @throws Exception
*/
public static byte[] addFile(String path) throws Exception{
//文件
File f = new File(path);
//文件名
String fname = f.getName();
//文件名數(shù)組
byte[] fnameBytes = fname.getBytes() ;
//文件內(nèi)容長度
int len = (int)f.length();
//計(jì)算總長度 文件名長度 + 文件名內(nèi)容 + 文件內(nèi)容長度 + 文件內(nèi)容
int total = 4 + fnameBytes.length + 4 + len ;
//初始化總數(shù)組
byte[] bytes = new byte[total];
//1.寫入文件名長度
byte[] fnameLenArr = Util.int2Bytes(fnameBytes.length);
System.arraycopy(fnameLenArr, 0, bytes, 0, 4);
//2.寫入文件名本身
System.arraycopy(fnameBytes, 0, bytes, 4, fnameBytes.length);
//3.寫入文件內(nèi)容長度
byte[] fcontentLenArr = Util.int2Bytes(len);
System.arraycopy(fcontentLenArr, 0, bytes, 4 + fnameBytes.length, 4);
//4.寫入文件內(nèi)容
//讀取文件內(nèi)容到數(shù)組中
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(f);
byte[] buf = new byte[1024];
int len0 = 0 ;
while((len0 = fis.read(buf)) != -1){
baos.write(buf, 0, len0);
}
fis.close();
//得到文件內(nèi)容
byte[] fileContentArr = baos.toByteArray();
System.arraycopy(fileContentArr, 0, bytes, 4 + fnameBytes.length + 4, fileContentArr.length);
return bytes ;
}
}
文件的解檔
package cn.yimen.archiver;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* 解檔程序
*/
public class Unarchiver {
public static void main(String[] args) throws Exception {
List<FileBean> files = new ArrayList<FileBean>();
//
FileInputStream fis = new FileInputStream("d:/arch/x.xar");
FileBean fileBean = null ;
//
while((fileBean = readNextFile(fis)) != null){
files.add(fileBean);
}
//關(guān)閉流
fis.close();
FileOutputStream fos = null ;
//
for(FileBean fb : files){
fos = new FileOutputStream("d:/arch/unarch/" + fb.getFileName());
fos.write(fb.getFileContent());
fos.close();
}
}
/**
* 從流中讀取下一個文件
*/
public static FileBean readNextFile(FileInputStream fis) throws Exception{
//
byte[] bytes4 = new byte[4];
//讀取四個字節(jié)
int res = fis.read(bytes4);
if(res == -1){
return null ;
}
//文件名長度
int fnameLen = Util.bytes2Int(bytes4);
//文件名數(shù)組
byte[] fileNameBytes = new byte[fnameLen];
fis.read(fileNameBytes);
//得到文件名
String fname = new String(fileNameBytes);
//再讀取4個字節(jié),作為文件內(nèi)容的長度
fis.read(bytes4);
int fileContLen = Util.bytes2Int(bytes4);
//讀取文件內(nèi)容
byte[] fileContBytes = new byte[fileContLen];
fis.read(fileContBytes);
return new FileBean(fname,fileContBytes);
}
}
package cn.yimen.archiver;
/**
* 文件Bean
*/
public class FileBean {
private String fileName;
private byte[] fileContent;
public FileBean() {
}
public FileBean(String fname, byte[] fileContBytes) {
this.fileName = fname ;
this.fileContent = fileContBytes ;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public byte[] getFileContent() {
return fileContent;
}
public void setFileContent(byte[] fileContent) {
this.fileContent = fileContent;
}
}
工具類
package cn.yimen.archiver;
public class Util {
/**
* 整型轉(zhuǎn)換成字節(jié)數(shù)組
*/
public static byte[] int2Bytes(int i){
byte[] arr = new byte[4] ;
arr[0] = (byte)i ;
arr[1] = (byte)(i >> 8) ;
arr[2] = (byte)(i >> 16) ;
arr[3] = (byte)(i >> 24) ;
return arr ;
}
/**
* 字節(jié)數(shù)組轉(zhuǎn)成int
*/
public static int bytes2Int(byte[] bytes){
int i0= bytes[0];
int i1 = (bytes[1] & 0xFF) << 8 ;
int i2 = (bytes[2] & 0xFF) << 16 ;
int i3 = (bytes[3] & 0xFF) << 24 ;
return i0 | i1 | i2 | i3 ;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
深入淺出理解Java Lambda表達(dá)式之四大核心函數(shù)式的用法與范例
Lambda 表達(dá)式,也可稱為閉包,它是推動 Java 8 發(fā)布的最重要新特性。Lambda 允許把函數(shù)作為一個方法的參數(shù)(函數(shù)作為參數(shù)傳遞進(jìn)方法中)。使用 Lambda 表達(dá)式可以使代碼變的更加簡潔緊湊,今天小編帶你理解Lambda表達(dá)式之四大核心函數(shù)式的用法,感興趣的朋友快來看看吧2021-11-11
更簡單更高效的Mybatis?Plus最新代碼生成器AutoGenerator
這篇文章主要為大家介紹了更簡單更高效的Mybatis?Plus最新代碼生成器AutoGenerator使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
SpringBoot框架實(shí)現(xiàn)支付和轉(zhuǎn)賬功能
在 Spring Boot 框架中實(shí)現(xiàn)支付和轉(zhuǎn)賬功能時,涉及到多個細(xì)節(jié)和注意點(diǎn),這些功能通常需要高度的安全性、穩(wěn)定性和可擴(kuò)展性,本文介紹了實(shí)現(xiàn)支付和轉(zhuǎn)賬功能的一些關(guān)鍵點(diǎn),需要的朋友可以參考下2024-08-08
Spring?Security實(shí)現(xiàn)接口放通的方法詳解
在用Spring?Security項(xiàng)目開發(fā)中,有時候需要放通某一個接口時,我們需要在配置中把接口地址配置上,這樣做有時候顯得麻煩。本文將通過一個注解的方式快速實(shí)現(xiàn)接口放通,感興趣的可以了解一下2022-05-05
SpringBoot集成Liquibase的詳細(xì)步驟
Liquibase 是一個強(qiáng)大的數(shù)據(jù)庫版本控制工具,能夠與 Spring Boot2024-12-12
無縫集成,以簡化數(shù)據(jù)庫遷移和管理的過程,以下是從開始集成到最終運(yùn)行的詳細(xì)步驟,需要的朋友可以參考下
SpringBoot項(xiàng)目中引入本地JAR包配置的幾種方法
SpringBoot有時需要引入本地JAR包以便重用已有的代碼庫或者第三方庫,本文主要介紹了SpringBoot項(xiàng)目中引入本地JAR包配置的幾種方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08

