⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compress.java

📁 用java编写的压缩程序
💻 JAVA
字号:
package compress;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Calendar;
import java.io.*;

public class Compress {//extends Thread {
    String text;
    String code;
    String remain;
    String codeFile, textFile;
    CodeNode[] leafs;
    Compressor com;
    char[] codeChar, buffereChar;
    byte buffersize;
    double fileLength, currentLength;//文件长度,现在已压缩大小 用来设置进度条

    // 算法类对象
//    public static void main(String[] args) {
//        // TODO Auto-generated method stub
//        Compress compressor = new Compress();
//        Calendar cal = Calendar.getInstance();
//        System.out.println("Start Time: " + cal.get(Calendar.HOUR_OF_DAY) + ":"
//                           + cal.get(Calendar.MINUTE) + ":" +
//                           cal.get(Calendar.SECOND)
//                           + ":" + cal.get(Calendar.MILLISECOND));
//        compressor.compress("e://test.txt", "e://test.dat");
//        cal = Calendar.getInstance();
//        System.out.println("End Time: " + cal.get(Calendar.HOUR_OF_DAY) + ":"
//                           + cal.get(Calendar.MINUTE) + ":" +
//                           cal.get(Calendar.SECOND)
//                           + ":" + cal.get(Calendar.MILLISECOND));
//    }

    public Compress() {
        this.initialize();
    }

    public Compress(String codeFile, String textFile) {
        this();
        this.codeFile = codeFile;
        this.textFile = textFile;
    }

    public void setFile(String codeFile, String textFile) {
        this.codeFile = codeFile;
        this.textFile = textFile;
    }

    void initialize() {
        text = code = remain = "";
        buffersize = 16;
        buffereChar = new char[buffersize];
        // 算法类初始化
        com = new Compressor();

    }

    private String getCode(CodeNode[] chars, char c) { // 得到字符对应01代码
        int r = 0; // 记录搜索位置
        boolean isFound = false; // 是否已经找到
        CodeNode aNode = null;
        while (r < chars.length && !isFound) {
            if (chars[r].getChar() == c) {
                isFound = true; // 已找到
                aNode = chars[r];
            } else {
                r++;
            }
        }
        return aNode.code;
    }

    char converBitsToChar(String bits) {//将01代码转化为char并返回
        char result;
        int number = 0;
        int count = 0;

        for (int t = 0; t < 16; t++) {//每十六位一转化
            if (bits.charAt(count) == '1') {
                number += Math.pow(2, 15 - t);
            }
            count++;
        }

        result = (char) number;

        return result;
    }

    public String getCode(String str) {
        // 得到字符串对应的01代码
        String code = "";
        for (int s = 0; s < str.length(); s++) {
            code += getCode(leafs, str.charAt(s));
        }

        return code;
    }

    protected void setMap(String str) {
        leafs = com.getNodes(str);
    }

    char[] compressCode(String bits) {

        char[] result;
        result = new char[bits.length() / 16];
        int mode = bits.length() % 16;
        if (mode != 0) {
            this.remain = bits.substring(bits.length() - mode, bits.length());
        }
        // 保存没被处理完的编码
        int number = 0;
        int count = 0;
        for (int i = 0; i < result.length; i++) {
            number = 0;
            for (int t = 0; t < 16; t++) {
                if (bits.charAt(count) == '1') {
                    number += Math.pow(2, 15 - t);
                }
                count++;
            }

            result[i] = (char) number;
        }
        return result;
    }

    public void compress(String sourceFile, String codeFile) {
        File cFile = new File(codeFile);
        if (cFile.exists()) { // 删除重名文件
            cFile.delete();
        }
        this.fileLength = new File(sourceFile).length();
        BufferedReader reader;
        FileOutputStream out;
        this.setMap(sourceFile); // 建立字符映射表
        ObjectOutputStream objectWriter; // 用于写对象:leafs
        DataOutputStream charWriter;
        RandomAccessFile randomWriter;

        try {
            reader = new BufferedReader(new FileReader(sourceFile)); // construct

            out = new FileOutputStream(cFile); // construct out put stream
            objectWriter = new ObjectOutputStream(out); // 写对象
            charWriter = new DataOutputStream(out); // 写字符
            randomWriter = new RandomAccessFile(codeFile, "rw"); // 操作文件写入需要保存的数据;
            for (int i = 0; i < this.leafs.length; i++) {

                objectWriter.writeObject(leafs[i]);

            }

            String temp = "";
            while ((this.text = reader.readLine()) != null) {

                this.currentLength += this.text.length() * 2;
               // System.out.println(currentLength);
                this.text = this.text + "\n"; // 处理转行符
                code = this.getCode(text); // 把"ab"转化为"01"串
                this.code = this.remain + this.code; // 把上一行剩余的编码添加到前面,形成新编码

                codeChar = this.compressCode(this.code); // 把"01"转化为char[]
                int codel = code.length(); // 得到编码的长度
                if (codel % 16 != 0 && codel > 0) {
                    if (codel > 16) { // 如果编码的长度大于16且不能被16整除,那么保存模16剩余的那一部分编码
                        remain = code.substring(codel - codel % 16, codel);
                    } else {
                        remain = code;
                    }
                }
                for (int i = 0; i < codeChar.length; i++) {
                    charWriter.writeChar(codeChar[i]);
                }


            }
            int remainLength = remain.length();
            if (remainLength > 0) { // 处理最后的几个01
                for (int i = 0; i < 16 - remainLength; i++) {
                    remain = "0" + remain; // 前面补0
                }

            }
            charWriter.writeChar(this.converBitsToChar(remain)); // 保存最后一个字符
            charWriter.writeInt(remainLength); // 记录最后一个01串的实际长度
           // System.out.println(remainLength);
            charWriter.writeInt(this.leafs.length); // 记录对象的数目
            reader.close();
            out.close(); // close FileOutputStream

        } catch (IOException io) {
            System.out.println(io.getLocalizedMessage());
        }

        System.out.println("Done!");
    }

    public void run() {
        if (this.codeFile == null || this.textFile == null) {
            System.err.println("Can't get the file name");

        } else {
            this.compress(codeFile, textFile);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -