归档程序的实现
思路:
任何文件底层都是通过字节流进行传输,所以可以准备一个字节数组存储文件的字节数据,进行拼接
1设计协议
取前四个字节,存储文件名的长度;根据长度准备相应字节的空间存相对应的文件名,同样在取四个字节,存内容长度,根据内容长度,存文件内容。
解档的时候 先取四个字节获得文件名的长度,根据文件名长度n获取4个偏移量后的n*4个字节的数据转换成字符就是文件名
在拿四个字节获取文件长度,同样根据长度获取内容,这样一个文件就解析出来了,重复以上步骤设定好偏移量就可以把文件还原回来。
问题一:int型的文件长度需要被转成byte 数组,解档时要恢复。其转换代码如下:”
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 ; } /** * 字节数组转成int */ public static int bytes2Int(byte[] bytes){ int i0= (bytes[1] & 0xFF); //为什么要与0xFF做&?因为byte型数据一旦移位则自动变为Int型,正数没问题,负数前边会全部补1,会影响结果 int i1 = (bytes[1] & 0xFF) << 8 ; int i2 = (bytes[2] & 0xFF) << 16 ; int i3 = (bytes[3] & 0xFF) << 24 ; return i0 | i1 | i2 | i3 ; } }
&0xFF原理如下
理解了原理就可以来着手编写归档和解档程序了,代码如下
首先定义一个文件的bean类
/** * 文件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; } }
编写归档程序
//归档
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(); //文件名数组 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 ; } }
解档程序如下:
/** * 解档程序 */ 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); } //关闭流 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); } }
文档归档完毕后可以利用zipOutPutStream对归档后的文件进行压缩和解压缩,压缩后的文件就可以进行传输了.
public class TestZip { @Test public void zip() throws Exception { //文件输出流 FileOutputStream fos = new FileOutputStream("d:/arch/xxx.xar"); //压缩流 ZipOutputStream zos = new ZipOutputStream(fos); String[] arr = { "d:/arch/1.jpg", "d:/arch/2.txt", "d:/arch/3.xml" }; for(String s : arr){ addFile(zos , s); } zos.close(); fos.close(); System.out.println("over"); } /** * 循环向zos中添加条目 */ public static void addFile(ZipOutputStream zos , String path) throws Exception{ File f = new File(path); zos.putNextEntry(new ZipEntry(f.getName())); FileInputStream fis = new FileInputStream(f); byte[] bytes = new byte[fis.available()]; fis.read(bytes); fis.close(); zos.write(bytes); zos.closeEntry(); } /** * 解压缩 */ @Test public void unzip() throws Exception{ // FileInputStream fis = new FileInputStream("d:/arch/xxx.zip"); // ZipInputStream zis = new ZipInputStream(fis); // ZipEntry entry = null ; byte[] buf = new byte[1024]; int len = 0 ; while((entry = zis.getNextEntry()) != null){ String name = entry.getName(); FileOutputStream fos = new FileOutputStream("d:/arch/unzip/" + name); while((len = zis.read(buf)) != -1){ fos.write(buf, 0, len); } fos.close(); } zis.close(); fis.close(); } }