`
jiuyuehe
  • 浏览: 181348 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

java 根据特定数据结构 打自定义包------解包篇

阅读更多
自定义的数据结构在解包的过程中用到,没有这个数据结构的话,解包就无从谈起了!
好了,直接上代码!酷毙的程序员一切尽在代码中!
public static  void testjie(String src,String newPath) throws Exception{
		File file =new File(src);
		//byte[] by = new byte[6];
		FileInputStream fis = new FileInputStream(file);
		int len =4 ;
		String head = readFirst(fis,len);
		//文件个数
		int fileNum = readNext(fis, len, 1);
		len++;
		
		List<String> pathList = new ArrayList<String>();
		List<Integer> inlist = new ArrayList<Integer>();
		
		for (int i = 0; i < fileNum; i++) {
			//路径长度
			int path_Len = readNext(fis, len, 1);
			len++;
			//文件大小
			int file_size = readNext(fis, len, 4);
			len++;
			inlist.add(file_size);
			len= len+path_Len;
			
			String path = readOffStr(fis,len,path_Len);
			pathList.add(path.trim());
		}
		
		System.out.println("len:"+len);
		System.out.println(pathList.toString());
		System.out.println(inlist.toString());
		
		List<String> newPathList = change2Disk( newPath, pathList);
		
		System.out.println("new path:"+newPathList.toString());
		
		for (int j = 0; j <newPathList.size(); j++) {
			File targetFile = new File(newPathList.get(j).replace("\\", "/"));
			if(!targetFile.exists()){
				System.out.println(targetFile.getPath());
				String dir = targetFile.getParent();
				System.out.println(dir);
				File dirfile = new File(dir);
				dirfile.mkdirs();
				targetFile.createNewFile();
			}
			OutputStream os = new FileOutputStream(targetFile);
			
			byte[] buf = new byte[1024];
			//xuyao du de 
			int need = inlist.get(j);
			int readed = 0;
			while (readed <need )
			{
				if(need - readed <buf.length )
				{
					int ret = fis.read(buf, 0, need - readed > buf.length ? buf.length :need - readed);
					os.write(buf);
					System.out.println(new String(buf));
					if(ret <0 )
					{
						
					}else
					{
						readed += ret;
					}
					
				}
				else{
					int ret = fis.read(buf, 0, need - readed > buf.length ? buf.length :need - readed);
					os.write(buf);
					System.out.println(new String(buf));
					if(ret <0 )
					{
						
					}else
					{
						readed += ret;
					}
				}
			}
			
			os.close();
			
		}
		
		fis.close();
	}


这里最重要的莫过于心里把握清楚,每一个信息占多少个自己。同时对java操作文件要掌握清楚,这个可以总结一下,FileInputStream 读文件的时候是有一个文件指针的,随着你的byte[] buf 往后面移动!不需要你去管理,知道这一定就清楚了,为什么不需要去标记代码中前面的len 后面写文件的时候不要用了!
/**
	 * 解压至--->newPath
	 * @param newPath
	 * @param pathList
	 * @return
	 */
	public static List<String> change2Disk(String newPath, List<String> pathList) {

		List<String> newPathList = new ArrayList<String>();

		for (int i = 0; i < pathList.size(); i++) {

			String path = pathList.get(i).replace("\\", "/");
			String pathArr[] = path.split(":");
			String realPath = newPath + pathArr[1];
			newPathList.add(realPath);
		}
		return newPathList;
	}

/**
	 * read len   off ->len  
	 * 读一段从偏移位置开始读
	 * @param fis
	 * @param off
	 * @param len
	 * @return buf -> int 
	 * @throws IOException
	 */
	public static int readNext(FileInputStream fis ,int off,int len) throws IOException{
		off++;
		if(len==1){
		byte [] buf = new byte[off+len];
		fis.read(buf,off,len);
		int length = (int)buf[off];
		System.out.println( "this readNext length is :"+length);
		return length;
		}else{
			byte [] buf = new byte[off+len];
			fis.read(buf,off,len);
			byte [] buf2 = new byte[4];
			for(int i=buf.length-4;i<=buf.length;i++){
				buf2[3]=buf[buf.length-1];
				buf2[2]=buf[buf.length-2];
				buf2[1]=buf[buf.length-3];
				buf2[0]=buf[buf.length-4];
			}
			System.out.println( "this readNext ==byte 2 int== length is :"+bytesToInt(buf2));
			return bytesToInt(buf2);
		}
		
	}


基本如此。既然是打包的话当然需要压缩。文件压缩算法采取那种呢!还再思考中,写完后将继续发出来!谢谢!希望看的大神们留下思路!
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics