Java實(shí)現(xiàn)文件的歸檔和解檔
本文實(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)容長(zhǎng)度 int len = (int)f.length(); //計(jì)算總長(zhǎng)度 文件名長(zhǎng)度 + 文件名內(nèi)容 + 文件內(nèi)容長(zhǎng)度 + 文件內(nèi)容 int total = 4 + fnameBytes.length + 4 + len ; //初始化總數(shù)組 byte[] bytes = new byte[total]; //1.寫(xiě)入文件名長(zhǎng)度 byte[] fnameLenArr = Util.int2Bytes(fnameBytes.length); System.arraycopy(fnameLenArr, 0, bytes, 0, 4); //2.寫(xiě)入文件名本身 System.arraycopy(fnameBytes, 0, bytes, 4, fnameBytes.length); //3.寫(xiě)入文件內(nèi)容長(zhǎng)度 byte[] fcontentLenArr = Util.int2Bytes(len); System.arraycopy(fcontentLenArr, 0, bytes, 4 + fnameBytes.length, 4); //4.寫(xiě)入文件內(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(); } } /** * 從流中讀取下一個(gè)文件 */ public static FileBean readNextFile(FileInputStream fis) throws Exception{ // byte[] bytes4 = new byte[4]; //讀取四個(gè)字節(jié) int res = fis.read(bytes4); if(res == -1){ return null ; } //文件名長(zhǎng)度 int fnameLen = Util.bytes2Int(bytes4); //文件名數(shù)組 byte[] fileNameBytes = new byte[fnameLen]; fis.read(fileNameBytes); //得到文件名 String fname = new String(fileNameBytes); //再讀取4個(gè)字節(jié),作為文件內(nèi)容的長(zhǎng)度 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 ; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解spring-cloud與netflixEureka整合(注冊(cè)中心)
這篇文章主要介紹了詳解spring-cloud與netflixEureka整合(注冊(cè)中心),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02maven項(xiàng)目在實(shí)踐中的構(gòu)建管理之路的方法
這篇文章主要介紹了maven項(xiàng)目在實(shí)踐中的構(gòu)建管理之路的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05Java同步鎖synchronized用法的最全總結(jié)
這篇文章主要介紹了Java同步鎖synchronized用法的最全總結(jié),需要的朋友可以參考下,文章詳細(xì)講解了Java同步鎖Synchronized的使用方法和需要注意的點(diǎn),希望對(duì)你有所幫助2023-03-03Springboot FeignClient調(diào)用Method has too m
本文主要介紹了Springboot FeignClient微服務(wù)間調(diào)用Method has too many Body parameters 解決,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12java對(duì)象轉(zhuǎn)換String類型的三種方法
在很多情況下我們都需要將一個(gè)對(duì)象轉(zhuǎn)換為String類型。一般來(lái)說(shuō)有三種方法可以實(shí)現(xiàn):Object.toString()、(String)Object、String.valueOf(Object)。下面對(duì)這三種方法一一分析2013-11-11java?Springboot對(duì)接開(kāi)發(fā)微信支付詳細(xì)流程
最近要做一個(gè)微信小程序,需要微信支付,所以研究了下怎么在java上集成微信支付功能,下面這篇文章主要給大家介紹了關(guān)于java?Springboot對(duì)接開(kāi)發(fā)微信支付的相關(guān)資料,需要的朋友可以參考下2024-08-08Java?Collection接口中的常用方法總結(jié)
這篇文章將大概用代碼案例簡(jiǎn)單總結(jié)一下?Collection?接口中的一些方法,我們會(huì)以他的實(shí)現(xiàn)類?Arraylist?為例創(chuàng)建對(duì)象??煲黄饋?lái)看看吧2022-12-12