天天看點

java歸檔_Java實作檔案的歸檔和解檔

本文執行個體為大家分享了Java實作檔案歸檔和解檔的具體代碼,供大家參考,具體内容如下

檔案的歸檔

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

//

}

public static byte[] addFile(String path) throws Exception{

//檔案

File f = new File(path);

//檔案名

String fname = f.getName();

//檔案名數組

byte[] fnameBytes = fname.getBytes() ;

//檔案内容長度

int len = (int)f.length();

//計算總長度 檔案名長度 + 檔案名内容 + 檔案内容長度 + 檔案内容

int total = 4 + fnameBytes.length + 4 + len ;

//初始化總數組

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.寫入檔案内容長度

byte[] fcontentLenArr = Util.int2Bytes(len);

System.arraycopy(fcontentLenArr, 0, bytes, 4 + fnameBytes.length, 4);

//4.寫入檔案内容

//讀取檔案内容到數組中

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

//得到檔案内容

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 files = new ArrayList();

//

FileInputStream fis = new FileInputStream("d:/arch/x.xar");

FileBean fileBean = null ;

//

while((fileBean = readNextFile(fis)) != null){

files.add(fileBean);

}

//關閉流

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

//讀取四個位元組

int res = fis.read(bytes4);

if(res == -1){

return null ;

}

//檔案名長度

int fnameLen = Util.bytes2Int(bytes4);

//檔案名數組

byte[] fileNameBytes = new byte[fnameLen];

fis.read(fileNameBytes);

//得到檔案名

String fname = new String(fileNameBytes);

//再讀取4個位元組,作為檔案内容的長度

fis.read(bytes4);

int fileContLen = Util.bytes2Int(bytes4);

//讀取檔案内容

byte[] fileContBytes = new byte[fileContLen];

fis.read(fileContBytes);

return new FileBean(fname,fileContBytes);

}

}

package cn.yimen.archiver;

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 {

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 ;

}

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 ;

}

}

以上就是本文的全部内容,希望對大家的學習有所幫助,也希望大家多多支援聚米學院。