题目3.编写一个压缩软件

内容:

编写一个压缩软件,选择两种压缩算法(自选),将用户提交的文件实现压缩并提示用户压缩率并提示用户按压缩率高的算法压缩;该软件还可通过文件格式识别文件是否是本软件压缩并按压缩时的算法解压。

教学模式:

理解并分析题目需求,查找资料,设计数据结构和算法,选择最优算法和编程工具。

知识点:

使用多种存储结构,使用两种压缩算法,算法评估

能力:

能够掌握多种存储结构,自学多种压缩算法并选择合理的结构存储数据,选择适当的工具编写程序,掌握算法的评估,编写程序实现题目要求。

介绍:

使用哈夫曼压缩和lzw压缩方法,具体算法请自行百度,此处不做详细介绍。源码请在文末github地址处下载。

截图效果

部分代码

package function;import java.io.*;
import java.util.*;public class LZWCompression {/*** Define a HashMap and other variables that will be used in the program*/public HashMap<String, Integer> table = new HashMap<String, Integer>();// public TreeMap<String,Integer> table = new TreeMap<String,Integer>();private String[] Array_char;private int count;/** Default Constructor */public LZWCompression() {}public void LZW_Compress(String input, String output) throws IOException {/** Initialize the variables */Array_char = new String[4096];for (int i = 0; i < 256; i++) {table.put(Character.toString((char) i), i);Array_char[i] = Character.toString((char) i);}count = 256;/** Pointer to input and output file */DataInputStream read = new DataInputStream(new BufferedInputStream(new FileInputStream(input)));DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(output)));/** Local Variables */byte input_byte;String temp = "";byte[] buffer = new byte[3];boolean onleft = true;try {/** Read the First Character from input file into the String */input_byte = read.readByte();int i = new Byte(input_byte).intValue();if (i < 0) {i += 256;}char c = (char) i;temp = "" + c;/** Read Character by Character */while (true) {input_byte = read.readByte();i = new Byte(input_byte).intValue();if (i < 0) {i += 256;}c = (char) i;if (table.containsKey(temp + c)) {temp = temp + c;} else {String s12 = to12bit(table.get(temp));/*** Store the 12 bits into an array and then write it to the* output file*/if (onleft) {buffer[0] = (byte) Integer.parseInt(s12.substring(0, 8), 2);buffer[1] = (byte) Integer.parseInt(s12.substring(8, 12) + "0000", 2);} else {buffer[1] += (byte) Integer.parseInt(s12.substring(0, 4), 2);buffer[2] = (byte) Integer.parseInt(s12.substring(4, 12), 2);for (int b = 0; b < buffer.length; b++) {out.writeByte(buffer[b]);buffer[b] = 0;}}onleft = !onleft;if (count < 4096) {table.put(temp + c, count++);}temp = "" + c;}}} catch (EOFException e) {String temp_12 = to12bit(table.get(temp));if (onleft) {buffer[0] = (byte) Integer.parseInt(temp_12.substring(0, 8), 2);buffer[1] = (byte) Integer.parseInt(temp_12.substring(8, 12)+ "0000", 2);out.writeByte(buffer[0]);out.writeByte(buffer[1]);} else {buffer[1] += (byte) Integer.parseInt(temp_12.substring(0, 4), 2);buffer[2] = (byte) Integer.parseInt(temp_12.substring(4, 12), 2);for (int b = 0; b < buffer.length; b++) {out.writeByte(buffer[b]);buffer[b] = 0;}}read.close();out.close();}}/** Convert 8 bit to 12 bit */public String to12bit(int i) {String temp = Integer.toBinaryString(i);while (temp.length() < 12) {temp = "0" + temp;}return temp;}public int getvalue(byte b1, byte b2, boolean onleft) {String temp1 = Integer.toBinaryString(b1);String temp2 = Integer.toBinaryString(b2);while (temp1.length() < 8) {temp1 = "0" + temp1;}if (temp1.length() == 32) {temp1 = temp1.substring(24, 32);}while (temp2.length() < 8) {temp2 = "0" + temp2;}if (temp2.length() == 32) {temp2 = temp2.substring(24, 32);}/** On left being true */if (onleft) {return Integer.parseInt(temp1 + temp2.substring(0, 4), 2);} else {return Integer.parseInt(temp1.substring(4, 8) + temp2, 2);}}public void LZW_Decompress(String input, String output) throws IOException {/** Initialize the variables */Array_char = new String[4096];for (int i = 0; i < 256; i++) {table.put(Character.toString((char) i), i);Array_char[i] = Character.toString((char) i);}count = 256;/** Stream pointer to input and output file path */DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(input)));DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(output)));int currword, priorword;byte[] buffer = new byte[3];boolean onleft = true;try {/*** Get the first word in code and output its corresponding character*/buffer[0] = in.readByte();buffer[1] = in.readByte();priorword = getvalue(buffer[0], buffer[1], onleft);onleft = !onleft;out.writeBytes(Array_char[priorword]);/*** Read every 3 bytes and generate a corresponding characters - 2* character*/while (true) {if (onleft) {buffer[0] = in.readByte();buffer[1] = in.readByte();currword = getvalue(buffer[0], buffer[1], onleft);} else {buffer[2] = in.readByte();currword = getvalue(buffer[1], buffer[2], onleft);}onleft = !onleft;if (currword >= count) {if (count < 4096)Array_char[count] = Array_char[priorword]+ Array_char[priorword].charAt(0);count++;out.writeBytes(Array_char[priorword]+ Array_char[priorword].charAt(0));} else {if (count < 4096)Array_char[count] = Array_char[priorword]+ Array_char[currword].charAt(0);count++;out.writeBytes(Array_char[currword]);}priorword = currword;}} catch (EOFException e) {in.close();out.close();}}
}

源码下载:

完整项目下载:点此获取
或许有帮到你的话可以点个Star么~

编写一个压缩软件(Java实现版本)相关推荐

  1. java 程序输出 赵_编写一个完整的JAVA的程序

    编写一个完整的JAVA的程序 关注:84  答案:1  mip版 解决时间 2021-02-05 08:43 提问者妳螚鬧俄螚笑 2021-02-05 02:59 1,接口Person Show()方 ...

  2. Java制作一个盒子程序_编写一个简单的Java程序,模拟计算器的功能。

    提问:编写一个简单的Java程序,模拟计算器的功能. 网友回答: 程序参考: import java.awt.*; import java.awt.event.ActionEvent; import ...

  3. 教你如何删除流氓软件(以一个压缩软件为例)

    1.序 电脑上流氓软件如何删除是一个让人比较头疼的事.而这些流氓软件是如何安装到我们电脑上的?似乎我们也没有主动安装过它们.其实,很多情况下这些流氓软件都是在我们不知名的情况下被安装的,比如笔者在一个 ...

  4. 如何使用eclipse编写一个简单的java程序

    用eclipse编写一个简单的java程序,输出hello world 在使用eclipse编写程序之前先保证jdk安装的完成,才能正确运行程序 1.打开eclipse,选择工作空间,工作空间就是你新 ...

  5. 编写一个java_Java入门篇(一)——如何编写一个简单的Java程序

    最近准备花费很长一段时间写一些关于Java的从入门到进阶再到项目开发的教程,希望对初学Java的朋友们有所帮助,更快的融入Java的学习之中. 主要内容包括JavaSE.JavaEE的基础知识以及如何 ...

  6. Java入门篇(一)——如何编写一个简单的Java程序

    最近准备花费很长一段时间写一些关于Java的从入门到进阶再到项目开发的教程,希望对初学Java的朋友们有所帮助,更快的融入Java的学习之中. 主要内容包括JavaSE.JavaEE的基础知识以及如何 ...

  7. ava入门篇——如何编写一个简单的Java程序

    最近准备花费很长一段时间写一些关于Java的从入门到进阶再到项目开发的教程,希望对初学Java的朋友们有所帮助,更快的融入Java的学习之中. 主要内容包括JavaSE.JavaEE的基础知识以及如何 ...

  8. java实现表达式求值_如何编写一个高效的Java表达式求值程序

    虽然,这个题目是有一点夺人眼球,但我真实这么做了(关是以否信任基准测试效果,这是其他一个话题). 所以,上周我一贯在找一个小型.适用的竞赛争辩数学表达式的类库.有功夫我在stackoverflow上看 ...

  9. 用java编写输出欢迎光临_编写一个完整的Java applet程序,程序功能为:在屏幕上输出“欢迎光临Java世界!”的字符串信息。...

    Applet程序开发主要步骤如下: 1)选用EDIT或Windows Notepad等工具作为编辑器建立Java Applet源程序. 2)把Applet的源程序转换为字节码文件. 3)编制使用cla ...

  10. java 定时调度_怎么编写一个定时调度java程序

    目前有两种流行Spring定时器配置:Java的Timer类和OpenSymphony的Quartz. 1.Java Timer定时 首先继承java.util.TimerTask类实现run方法 i ...

最新文章

  1. flipdim--按指定维数翻转矩阵
  2. 吴恩达机器学习(第一章)——初识机器学习
  3. Mybatis的selectKey使用
  4. PyTorch中的torch.nn.Parameter() 详解
  5. linux进程管理 ps,Linux - 进程管理,ps与top
  6. 报错, liquibase.exception.ValidationFailedException: Validation Failed
  7. nginx 平滑升级
  8. c++注释快捷键_JAVA编程中你一定要掌握的“快捷键”
  9. 切实把握大数据时代的新机遇新变革
  10. 运维工程师的生存法则
  11. 定义和使用结构体变量
  12. 这些 iOS 面试基础题目,你都深入了解吗?
  13. 基于ObjectArx进行cad二次开发总结
  14. QT集成Windows手写输入法
  15. 74HC595在【8x8LED点阵】中的运用
  16. 鹏业安装算量软件运行环境_安装算量软件电脑配置
  17. 喜迎进博会 欢聚环球港 共享消费城——2021环球商业年会暨城市综合体论坛圆满举行
  18. windows时间同步脚本
  19. JAVA入门-024(int的正负数)
  20. 监督学习(supervised learning)与非监督学习(unsupervised learning)

热门文章

  1. 当HR压你价,说你只值7K,你该怎么回答?
  2. C语言实现图片找茬,[创意心得]大家来找茬(C语言)
  3. 前后端分离单页面应用(SPA)项目示例(Vue+ElementUI+Axios+Django+MySql)
  4. php域名查询,域名查询 PHP 代码
  5. windows7内部版本7601副本不是正版的解决方案
  6. android手机连不上wifi密码,修改wifi密码后手机连不上_修改wifi密码后手机不能上网-192路由网...
  7. 2022年一级建造师《工程经济》模拟卷有解析
  8. python中的ln函数_python中的对数log函数表示及用法
  9. 第四次实验(全连MGRE、星型拓扑、OSPF通私有网段)
  10. 激活剂、天梯与火石:从ASC 19解读产学结合的关键密码