package com.qmth.jkserver;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.qmth.jkserver.constant.SystemConstant;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import com.qmth.jkserver.core.exception.JkServerException;

/**
 * 通用题库 优特答案数据包解码操作
 * @author tianjun
 *
 */
public class FileDecodeTestByChunk {
	
	private static final File ROOT = new File("E:\\RESOURCE_RUNNING\\output");
	
	public static void decode(File file) throws IOException{
		byte[] startBytes = new byte[23];	//zip头部块
		byte[] endBytes = new byte[23];		//zip尾部块
		byte[] loop = new byte[41];			
		RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
		randomAccessFile.seek(136);			//跳过头部的136字节
		int len = 0;
		long total = file.length()-54-136;
		//每次读取41字节 计算需要读取多少次，以及最后一次需要读取多少字节
		int count = new Double(Math.floor(total / 41)).intValue();	//读取次数
		int left = new Double(total % 41).intValue();	//最后一次读取多少字节
		File chunksDir = new File(file.getParentFile(),"chunk");
		if(!chunksDir.exists()){
			chunksDir.mkdirs();
		}
		for (int i=0;i<count;i++) {
			len = randomAccessFile.read(loop);
			if(len>0 && len == 41){
				File chunkFile = new File(chunksDir, i+".chunk");
				OutputStream os = new BufferedOutputStream(new FileOutputStream(chunkFile));
				os.write(loop);
				IOUtils.closeQuietly(os);
				if(chunkFile.length() > 41){
					throw new JkServerException("加密文件结构错误");
				}
			}
			else{
				throw new JkServerException("加密文件结构错误");
			}
		}
		byte[] leftBytes = new byte[left];
		len = randomAccessFile.read(leftBytes);
		if(len!=left){
			throw new JkServerException("加密文件结构错误,尾部字节读取错误");
		}
		else{
			File chunkFile = new File(chunksDir, count+".chunk");
			OutputStream os = new BufferedOutputStream(new FileOutputStream(chunkFile));
			os.write(leftBytes);
			IOUtils.closeQuietly(os);
			if(chunkFile.length() > 41){
				throw new JkServerException("加密文件结构错误");
			}
		}
		len = randomAccessFile.read(startBytes);
		if(len!=23){
			throw new JkServerException("文件结构错误,头部23字节读取失败");
		}
		len = randomAccessFile.read(endBytes);
		if(len!=23){
			throw new JkServerException("文件结构错误,尾部23字节读取失败");
		}
		//输出到文件里面
		File ansFile = new File(ROOT,"answer_"+new SimpleDateFormat("yyyy-MM-dd-HHmmss").format(new Date()) + SystemConstant.ZIP_PREFIX);
		if(!ansFile.exists()){
			ansFile.createNewFile();
		}
		BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(ansFile));
		os.write(startBytes);//写入头部23字节
		//倒序写入
		for (int i=count;i>=0;i--) {
			File chunkFile = new File(chunksDir, i+".chunk");
			if(chunkFile.exists() && chunkFile.length()<=41){
				byte[] c = new byte[41];
				InputStream is = new FileInputStream(chunkFile);
				len = is.read(c);
				os.write(c,0,len);
				IOUtils.closeQuietly(is);
			}
			else{
				throw new JkServerException("文件结构错误");
			}
		}
		os.write(endBytes);
		os.flush();
		IOUtils.closeQuietly(os);
		if(ansFile.length() != file.length()-144){
			throw new JkServerException("文件解码失败");
		}
		for (File f : chunksDir.listFiles()) {
			System.out.println(f.getAbsolutePath());
			if(f.exists()){
				FileUtils.forceDelete(f);
			}
		}
	}
	
	//复制数组a中的字节
	protected static byte[] cloneByteArray(byte[] a){
		byte[] result = new byte[a.length];
		for (int i=0;i<result.length;i++) {
			result[i] = a[i];
		}
		return result;
	}
	
	public static void main(String[] args) {
		try {
			decode(new File("E:\\RESOURCE_RUNNING\\test\\420101_42010101_20171026145351.qm"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
