如题,我想对字符串进行压缩,但是压缩后打印出来的字符长度却变长了,不知道是什么原因?
代码如下:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class TestP {
public static void main(String[] args) {
String str1 = "多资料都或多或少的包含一些多余的数据";
byte[] s=compress(str1);
System.out.println("s.length===="+s.length);
}
public static final byte[] compress(String str) {
if(str == null)
return null;
byte[] compressed;
ByteArrayOutputStream out = null;
ZipOutputStream zout = null;
try {
out = new ByteArrayOutputStream();
zout = new ZipOutputStream(out);
zout.putNextEntry(new ZipEntry("0"));
zout.write(str.getBytes());
zout.closeEntry();
compressed = out.toByteArray();
} catch(IOException e) {
compressed = null;
} finally {
if(zout != null) {
try{zout.close();} catch(IOException e){}
}
if(out != null) {
try{out.close();} catch(IOException e){}
}
}
return compressed;
}
}我输入的字符串占38个字节,但压缩后却打印出来字节长度是88
打印的如下:s.length====88
代码如下:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class TestP {
public static void main(String[] args) {
String str1 = "多资料都或多或少的包含一些多余的数据";
byte[] s=compress(str1);
System.out.println("s.length===="+s.length);
}
public static final byte[] compress(String str) {
if(str == null)
return null;
byte[] compressed;
ByteArrayOutputStream out = null;
ZipOutputStream zout = null;
try {
out = new ByteArrayOutputStream();
zout = new ZipOutputStream(out);
zout.putNextEntry(new ZipEntry("0"));
zout.write(str.getBytes());
zout.closeEntry();
compressed = out.toByteArray();
} catch(IOException e) {
compressed = null;
} finally {
if(zout != null) {
try{zout.close();} catch(IOException e){}
}
if(out != null) {
try{out.close();} catch(IOException e){}
}
}
return compressed;
}
}我输入的字符串占38个字节,但压缩后却打印出来字节长度是88
打印的如下:s.length====88
10.if(str == null)
11.return null;
12.
13.byte[] compressed;
14.ByteArrayOutputStream out = null;
15.ZipOutputStream zout = null;
16.
17.try {
18.out = new ByteArrayOutputStream();
19.zout = new ZipOutputStream(out);
20.zout.putNextEntry(new ZipEntry("0"));
21.zout.write(str.getBytes());
22.zout.closeEntry();
23.compressed = out.toByteArray();
24.} catch(IOException e) {
25.compressed = null;
26.} finally {
27.if(zout != null) {
28.try{zout.close();} catch(IOException e){}
29.}
30.if(out != null) {
31.try{out.close();} catch(IOException e){}
32.}
33.}
34.
35.return compressed;
36.}
37.
38./**
39.* 将压缩后的 byte[] 数据解压缩
40.*
41.* @param compressed 压缩后的 byte[] 数据
42.* @return 解压后的字符串
43.*/
44.public static final String decompress(byte[] compressed) {
45.if(compressed == null)
46.return null;
47.
48.ByteArrayOutputStream out = null;
49.ByteArrayInputStream in = null;
50.ZipInputStream zin = null;
51.String decompressed;
52.try {
53.out = new ByteArrayOutputStream();
54.in = new ByteArrayInputStream(compressed);
55.zin = new ZipInputStream(in);
56.ZipEntry entry = zin.getNextEntry();
57.byte[] buffer = new byte[1024];
58.int offset = -1;
59.while((offset = zin.read(buffer)) != -1) {
60.out.write(buffer, 0, offset);
61.}
62.decompressed = out.toString();
63.} catch (IOException e) {
64.decompressed = null;
65.} finally {
66.if(zin != null) {
67.try {zin.close();} catch(IOException e) {}
68.}
69.if(in != null) {
70.try {in.close();} catch(IOException e) {}
71.}
72.if(out != null) {
73.try {out.close();} catch(IOException e) {}
74.}
75.}
76.
77.return decompressed;
78.}这里需要特别注意的是,如果你想把压缩后的byte[]保存到字符串中,不能直接使用new String(byte)或者byte.toString(),因为这样转换之后容量是增加的。同样的道理,如果是字符串的话,也不能直接使用new String().getBytes()获取byte[]传入到decompress中进行解压缩。
如果保存压缩后的二进制,可以使用new sun.misc.BASE64Encoder().encodeBuffer(byte[] b)将其转换为字符串。同样解压缩的时候首先使用new BASE64Decoder().decodeBuffer 方法将字符串转换为字节,然后解压就可以了。