zoukankan      html  css  js  c++  java
  • 使用赫夫曼编码压缩解压文件(三)及注意事项

     /**
         *使用灾难级IO方案进行压缩
         * @param srcFile 希望的压缩的文件全路径
         * @param dstFile 压缩文件的保存路径
         */
        public static void zipFile(String srcFile,String dstFile)
        {
            //创建输出流
            FileOutputStream os=null;
            //创建输出流
            FileInputStream is=null;
            ObjectOutputStream oos=null;
            try {
                //创建文件的输入流
                is=new FileInputStream(srcFile);
                //创建一个和源文件大小一样的byte数组
                //is.available()返回当前文件的大小
                byte[] b=new byte[is.available()];
                //读取文件
                is.read(b);
                //使用赫夫曼编码进行编码:1 获取到文件对应的编码
                //直接对源文件进行压缩
                byte[] bytes=huffmanZip(b);
                //创建文件的输出流,存放压缩文件
                os=new FileOutputStream(dstFile);
                //创建一个和文件输出流关联的ObjectOutputStream
                //把经过赫夫曼编码后的字节数组写入压缩文件
                oos=new ObjectOutputStream(os);
                oos.writeObject(bytes);//这里我们以对象流的方式写入赫夫曼编码,是为了以后我们恢复源文件时使用
                //注意一定要把赫夫曼编码写入压缩文件
                oos.writeObject(huffmanCodes);
            } catch (FileNotFoundException e) {
                System.out.println(e.getMessage());
            } catch (IOException e) {
               System.out.println(e.getMessage());
            } finally {
                if (is!=null)
                {
                    try {
                        is.close();
                    } catch (IOException e) {
                        System.out.println(e.getMessage());
                    }
                }
                if (os!=null)
                {
                    try {
                        os.close();
                    } catch (IOException e) {
                        System.out.println(e.getMessage());
                    }
                }
                if (oos!=null)
                {
                    try {
                        oos.close();
                    } catch (IOException e) {
                        System.out.println(e.getMessage());
                    }
                }
            }
        }
        //编写一个方法,完成对压缩文件的解压
    
        /**
         *
         * @param zipFile 准备解压的文件
         * @param dstFile 将文件解压到那个路径
         */
        public static  void unZip(String zipFile,String dstFile)
        {
            //创建文件输入流
            InputStream is=null;
            //定义一个对象输入流
            ObjectInputStream ois=null;
            //定义文件的输出流
            OutputStream os=null;
            try{
                //创建文件输入流
                is=new FileInputStream(zipFile);
                //创建一个和is关联的对象输入流
                ois=new ObjectInputStream(is);
                //读取byte数组 huffmanBytes
                byte[] bytes= (byte[]) ois.readObject();
                //读取赫夫曼编码表
                Map<Byte,String> codes=(Map<Byte,String>)ois.readObject();
                //解码
                byte[] bytes1=decode(codes,bytes);
                //将byte数组写入到目标文件
                os=new FileOutputStream(dstFile);
                //写数据到文件中
                os.write(bytes1);
                os.flush();
            }catch (Exception e)
            {
                System.out.println(e.getMessage());
            }finally {
                    if (is!=null)
                    {
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                if (ois!=null)
                {
                    try {
                        ois.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (os!=null)
                {
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    此代码续上文,关于赫夫曼编码代码请参考前2章

    赫夫曼编码压缩文件注意事项

    1 如果文件本身就是经过压缩处理的,那么使用赫夫曼编码再压缩效率也不会有明显变化,比如视频文件,.pptx等等文件

    2 赫夫曼编码是按字节来处理的,因此可以处理所有的文件(二进制文件,文本文件)

    3 如果一个文件中的内容,重复的数据不多,压缩效果也不会很明显

  • 相关阅读:
    启动Mysql后找不到服务或出现找不到指定文件
    WEB-MVC模式图示
    Java中Map集合的遍历方式
    sun.misc.BASE64Encoder找不到jar包的解决方法
    Tomcat常用的网站发布方式
    Sql Server查询行号
    Mysql下载安装问题
    【数学】环逆序
    【搜索】【the first editoral】OpenJudge 我是最快的马
    The First Blog
  • 原文地址:https://www.cnblogs.com/qyx66/p/12109638.html
Copyright © 2011-2022 走看看