package com.jeeplus.modules.filePreview;

import org.apache.commons.codec.binary.Base64;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class FileSplit {
    /**
     * 分割
     * @param fileName 文件路径
     * @param Number 分块文件个数
     * @throws Exception
     */
    public void splitByNumber(String fileName,int Number) throws Exception{
        File oldFile=new File(fileName);
        BufferedInputStream in=new BufferedInputStream(new FileInputStream(oldFile));
        String file=encode(in);
        int length=file.length();
        System.out.println("字符串长度："+length);
        int size=length/Number;
        int start=0,end=size;
        BufferedOutputStream out=null;
        File newFile=null;
        String str_temp=null;
        for(int i=0;i<Number-1;i++){
            str_temp=i+" "+oldFile.getName()+"\n";
            str_temp+=file.substring(start,end);
            newFile=new File("F:\\fenge\\"+randNumber()+".file");
            out=new BufferedOutputStream(new FileOutputStream(newFile));
            out.write(str_temp.getBytes());
            out.close();
            start+=size;
            end+=size;
        }
        str_temp=Number-1+" "+oldFile.getName()+"\n";
        str_temp+=file.substring(start);
        newFile=new File("F:\\fenge\\"+randNumber()+".file");
        out=new BufferedOutputStream(new FileOutputStream(newFile));
        out.write(str_temp.getBytes());
        out.close();
        return;
    }

    /**
     * 文件合并
     * @param path
     * @throws Exception
     */
    public void mergeByName(String path) throws Exception{
        File file=new File(path);
        File list[]=file.listFiles();
        Map<String,String> map=new HashMap<String, String>();
        String newFileName=null;
        for(File f:list){
            BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(f)));
            String str_head=reader.readLine();
            String id=str_head.substring(0,str_head.indexOf(" "));
            newFileName=str_head.substring(str_head.indexOf(" ")+1);
            map.put(id,f.getAbsolutePath());
            reader.close();
        }

        StringBuffer stringBuffer=new StringBuffer();
        for(int i=0;i<list.length;i++){
            File f=new File(map.get(String.valueOf(i)));
            BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(f)));
            reader.readLine();
            String temp=null;
            while ((temp=reader.readLine())!=null){
                stringBuffer.append(temp);
            }
            reader.close();
        }
        BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream("F:\\hebing\\"+newFileName));
        out.write(decode(stringBuffer.toString()));
        out.close();
    }

    /**
     * 编码
     * @param in
     * @return
     * @throws IOException
     */
    public String encode(InputStream in) throws IOException{
        byte[] data = new byte[in.available()];
        in.read(data);
        return Base64.encodeBase64String(data);
    }

    /**
     * 解码
     * @param base64Str
     * @return
     * @throws IOException
     */
    public byte[] decode(String base64Str)throws IOException{
        return Base64.decodeBase64(base64Str);
    }

    /**
     * 随机数
     * @return
     */
    public String randNumber(){
        double number=Math.random();
        String str= String.valueOf(number);
        str=str.replace(".","");
        return str;
    }
    public static void main(String[] args){
        try {
            //分块
//            new FileSplit().splitByNumber("F:\\aa.pdf",10);
            //合并
            new FileSplit().mergeByName("F:\\fenge\\");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
