我正在尝试实现 LZW 压缩和解压缩技术。我的程序将任何文件作为 InputStream 并将其读入字节数组。然后它对其应用压缩算法,并在字符串变量中返回编码字节。
然后我应用返回原始数字的解压缩算法。
现在为了检索原始文件,我必须将此字符串的内容传输到字节数组中,然后将此数组写入输出流。
将解压缩字符串的内容复制到字节数组是我的问题所在。
File file = new File("aaaa.png");
byte[] data = new byte[(int) file.length()];
try{
FileInputStream in = new FileInputStream(file);
in.read(data);
in.close();
for(int i= 0; i< data.length;i++){
System.out.print("original = " + data[i]);
}
String ax = Arrays.toString(data);
List<Integer> compressed = compress(ax);
System.out.println("compressed = " + compressed);
String decompressed= new String(decompress(compressed));
System.out.println("decompressed = " + decompressed);
// Copy string contents into byte array arr[]
File file2 = new file("aaaa.png");
FileOutputStream out = new FileOutputStream(file2);
out.write(arr);
out.close();
}
catch(Exception e){
System.out.println("Error!");
e.printStackTrace();
}
到目前为止,我的代码的输出看起来与此有关-
原始 = -119807871131026100001373726882000170001686000 .....
压缩 = [91, 45, 49, 49, 57, 44, 32, 56, 48, 261, 55, 56, 265, 49, 261, 49, 51, 270, 264, 32, 50, 54, 273, 261 , 274, 280, 270, 272, 32, 55, 283, 55, 50, 261, 54, 267, 262, ...]
解压 = [-119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 17, 0, 0, 0, 16, 8, 6, 0, 0, 0, -16, 49, -108, 95, 0, 0, 0, 1, 115, 82, 71, 66, 0, -82, -50, 28, -23 , 0, 0, 0, 4, 103, 65, 77, 65, 0, 0, -79, -113, 11, -4, 97, 5, 0, 0, 0, 32, 99, 72, ,. .....]
请帮我弄清楚如何将字符串的内容复制到字节数组中。谢谢!