利用赫夫曼树加密压缩文件

因为鼠标落家里了,鼠标快递明天才到,所以明天再补充(5.23注释)

package copy.huffmantree;

import java.io.*;
import java.util.*;

/**
 * 一.把String字符转成ascii编码然后通过赫夫曼树生成唯一标识符。
 *
 *
 */
public class HuffmanDecode {
    public static void main(String[] args) {
       //srcFile是输入,dstFile是输出。
        String srcFile="E://src2.bmp";
        String dstFile="E://dst2.zip";
        //1、zipFile方法是压缩
        zipFile(srcFile,dstFile);
        System.out.println("压缩文件成功");
        //2、unZipFile是解压
//         unZipFile(dstFile,srcFile);
//        System.out.println("解压成功");
//        String content = "i like like like java do you like a java";
//        byte[] contentBytes = content.getBytes();
//        System.out.println(Arrays.toString(contentBytes));
//        byte[] huffmanCodesBytes = huffmanZip(contentBytes);
//        System.out.println("压缩后的编码"+Arrays.toString(huffmanCodesBytes));
//        //步骤5:解码的过程
//       deCode(huffmanCodes,huffmanCodesBytes);
//        byte[] sourceBytes = deCode(huffmanCodes, huffmanCodesBytes);
//        System.out.println(new String(sourceBytes));
        //getByte把字符变成字节ASCII码
//        System.out.println("1、getByte把字符变成字节ASCII码");
//        System.out.println(Arrays.toString(contentBytes));
//        List<Node1> nodes=getNodes(contentBytes);
//        System.out.println("2、把字节码按照个数转化权值遍历到List的Node结点中");
//        System.out.println(nodes);
//        System.out.println("3、创建赫夫曼树");
//        Node1 huffmanTree = createHuffmanTree(nodes);
//        preOrder(huffmanTree);
//        System.out.println("4、生成赫夫曼树对应的赫夫曼编码");
//        getCodes(huffmanTree,"",huffmanRoute);
//        System.out.println(huffmanCodes);
//        zip(contentBytes,huffmanCodes);


    }
    public static void unZipFile(String zipFile,String dstFile){
        InputStream is=null;
        ObjectInputStream ois=null;
        OutputStream os=null;
        try {
            // 初始化
            is = new FileInputStream(zipFile);
            // 初始化对象输入流 --- 用于读入 赫夫曼表和源文件数组
            ois = new ObjectInputStream(is);
            byte[] huffmanBytes = (byte[])ois.readObject();
            Map<Byte,String> h
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值