Java中怎么實(shí)現(xiàn)文件歸檔和解檔,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)專注于企業(yè)成都營(yíng)銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、敘州網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5、商城系統(tǒng)網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為敘州等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
文件的歸檔
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.寫入文件名長(zhǎng)度 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)容長(zhǎng)度 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(); } } /** * 從流中讀取下一個(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 ; }}
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
網(wǎng)頁題目:Java中怎么實(shí)現(xiàn)文件歸檔和解檔
文章網(wǎng)址:http://sd-ha.com/article46/jgsheg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、品牌網(wǎng)站設(shè)計(jì)、企業(yè)建站、網(wǎng)站內(nèi)鏈、網(wǎng)站策劃、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)