1

我正在开发一个使用 LZW 压缩来压缩 WAV 文件的项目。

我找到了这段代码并试图修改它来处理 wav 文件,

但是,有两件事变得一团糟:

  1. 媒体播放器无法再播放压缩音频文件。

  2. 我使用的下面的代码,它完美地适用于文本文件。但在 Wav 文件上失败。

非常感谢您的帮助。(PS 下面的代码来自 https://code.google.com/p/lzwj/source/browse/src/main/java/by/dev/madhead/lzwj/compress/LZW.java?r=c834a12adea60b0a5d9c3fa3a9d7900c26e5df80

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import by.dev.madhead.lzwj.io.Input;
import by.dev.madhead.lzwj.io.Output;
import by.dev.madhead.lzwj.util.ByteArray;

public class LZW {
        public static final int INITIAL_DICT_SIZE = 256; // 0..255
        public static final int DEFAULT_CODEWORD_LENGTH = 12; // Bit

        private int codeWordLength = DEFAULT_CODEWORD_LENGTH;
        private Map<ByteArray, Integer> codeTable;
        private List<ByteArray> decodeTable;

        public int getCodeWordLength() {
                return codeWordLength;
        }

        public void setCodeWordLength(int codeWordLength) {
                // Haha! Expected this method do something useful, didn't you?
                // this.codeWordLength = codeWordLength;
        }

        // Here be dragons!
        public void compress(InputStream in, OutputStream out) throws IOException {
                init();

                int code = INITIAL_DICT_SIZE;

                InputStream bufferedIn = new BufferedInputStream(in);
                Output compressedOutput = new Output(new BufferedOutputStream(out),
                                codeWordLength);

                int firstByte = bufferedIn.read();
                ByteArray w = new ByteArray((byte) firstByte);
                int K;

                while ((K = bufferedIn.read()) != -1) {
                        ByteArray wK = new ByteArray(w).append((byte) K);
                        if (codeTable.containsKey(wK)) {
                                w = wK;
                        } else {
                                compressedOutput.write(codeTable.get(w));
                                if (code < (1 << codeWordLength) - 1) {
                                        codeTable.put(wK, code++);
                                }
                                w = new ByteArray((byte) K);
                        }
                }
                compressedOutput.write(codeTable.get(w));
                compressedOutput.flush();
                compressedOutput.close();
        }

        public void decompress(InputStream in, OutputStream out) throws IOException {
                init();

                Input compressedInput = new Input(new BufferedInputStream(in),
                                codeWordLength);
                OutputStream bufferedOut = new BufferedOutputStream(out);

                int oldCode = compressedInput.read();
                bufferedOut.write(oldCode);
                int character = oldCode;
                int newCode;
                while ((newCode = compressedInput.read()) != -1) {
                        ByteArray string;
                        if (newCode >= decodeTable.size()) {
                                string = new ByteArray(decodeTable.get(oldCode));
                                string.append((byte) character);
                        } else {
                                string = decodeTable.get(newCode);
                        }
                        for (int i = 0; i < string.size(); i++) {
                                bufferedOut.write(string.get(i));
                        }
                        character = string.get(0);
                        decodeTable.add(new ByteArray(decodeTable.get(oldCode))
                                        .append((byte) character));
                        oldCode = newCode;
                }

                bufferedOut.flush();
                bufferedOut.close();
        }

        private void init() {
                codeTable = new HashMap<ByteArray, Integer>();
                decodeTable = new ArrayList<ByteArray>();
                for (int i = 0; i < INITIAL_DICT_SIZE; i++) {
                        codeTable.put(new ByteArray((byte) i), i);
                        decodeTable.add(new ByteArray((byte) i));
                }
        }
}
4

0 回答 0