java-文件编码

package code;


import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

public class Operation {
    private static List<Reconstitution> sort;
    private static byte[] buffer;

    public static void main(String[] args) throws Exception {
        String inputPath = "C:\\Users\\Administrator\\Desktop\\InputTest.jpg";
        Operation o = new Operation();
        o.prepar(inputPath);
        Start();

    }

    private void prepar(String inputPath) throws Exception {

        //建立缓冲区
        Path file = Paths.get(inputPath);
        byte[] buffer = Files.readAllBytes(file);

        //统计
        Map<Byte, Integer> count = new HashMap<>();
        for (byte individually : buffer) {
            Integer head = count.get(individually); //有没有
            if (head == null) {
                count.put(individually, 1);//第一次统计
            } else {
                count.put(individually, head + 1);
            }
        }

        //排序
        List<Reconstitution> sort = new ArrayList<>();
        for (Map.Entry<Byte, Integer> apart : count.entrySet()) {//允许对一对数据操作
            Reconstitution reconstitution = new Reconstitution(apart.getKey(), apart.getValue());
            sort.add(reconstitution);
        }
        Collections.sort(sort);

        //种树
        while (sort.size() > 1) {
            //次数
            Reconstitution thisWay = sort.get(0);
            Reconstitution thatWay = sort.get(1);
            int enumeration = thisWay.enumeration + thatWay.enumeration;

            //重构为倒Y
            Reconstitution upper = new Reconstitution(null, enumeration);
            upper.thisWay = thisWay;
            upper.thatWay = thatWay;

            //剪去首部,后面向前补,在尾部添加。逐层向上
            sort.remove(thisWay);
            sort.remove(thatWay);
            sort.add(upper);//我有一颗完整的树,劈开来拣到底的打印

        }
        this.sort = sort;
        this.buffer = buffer;
    }

    public static void Start() throws Exception {
        //获取路径
        Reconstitution root = sort.get(0);
        Encode patchwork = new Encode();
        Map<Byte, String> route = patchwork.patchwork(root);
        save(route);


        //转译
        StringBuilder ciphertext = new StringBuilder();//编码串
        for (byte tmpBuffer :
                buffer) {
            ciphertext.append(route.get(tmpBuffer));
        }
        FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\CodeTest2.txt",true);
        String st = new String(ciphertext);
        byte[] bt = st.getBytes();
        fileOutputStream.write(bt);
        fileOutputStream.flush();
        fileOutputStream.close();

    }

    private static void save(Map<Byte, String> route) throws  Exception {
//        保存路径
        for (Map.Entry<Byte, String> routesave:
             route.entrySet()) {
            FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\PathTest.txt",true);
            String str = "路径信息[byte="+routesave.getKey()+"];[path="+routesave.getValue()+")\n";
            byte[] bytes = str.getBytes();

            fileOutputStream.write(bytes);
            fileOutputStream.flush();
            fileOutputStream.close();
        }
    }

}

package code;

public class Reconstitution implements Comparable<Reconstitution> {
    Byte data; //文件内容
    int enumeration;//字符出现次数
    Reconstitution thisWay;//树状
    Reconstitution thatWay;

    public Reconstitution(Byte data, int enumeration){//对外放数据入口
        this.data = data;
        this.enumeration = enumeration;
    }


    @Override
    public int compareTo(Reconstitution o) {  //树状分层
        return this.enumeration - o.enumeration;
    }

    @Override
    public String toString() {//重构数据结构
        return "Nodedata["+"data=" + data + ",enumeration="+ enumeration+"]";
    }


    //树
    public void preOrder(){
        System.out.println(this);//回调函数打印
        if(this.thisWay != null){
            this.thisWay.preOrder();
        }
        if(this.thatWay != null){
            this.thatWay.preOrder();
        }
    }
    public static void preOrder(Reconstitution root){//从头看
        if (root != null){
            root.preOrder();
        }else{
            System.out.println("没有");
        }
    }
}
package code;


import java.util.HashMap;
import java.util.Map;


public class Encode {
    static Map<Byte, String> patchwork = new HashMap<>();//字节,路径
    static StringBuilder stringBuilder = new StringBuilder();//路径拼接

    //运树
    public static Map<Byte, String> patchwork(Reconstitution guidepost) {
        if (guidepost == null) {
            return null;
        }
        patchwork(guidepost.thisWay, "0", stringBuilder);
        patchwork(guidepost.thatWay, "1", stringBuilder);
        return patchwork;
    }

    //砍树
    public static void patchwork(Reconstitution guidepost, String code, StringBuilder stringBuilder) {
        StringBuilder dak = new StringBuilder(stringBuilder);
        dak.append(code);

        if (guidepost.data == null) {
            patchwork(guidepost.thisWay, "0", dak);
            patchwork(guidepost.thatWay, "1", dak);
        } else {//到底部了
            patchwork.put(guidepost.data, dak.toString());
        }
    }
}

posted @ 2024-11-10 02:43  基础狗  阅读(2)  评论(0编辑  收藏  举报