[请教]字符串压缩的问题:出错java.util.zip.ZipException: missing entry name
package gov.nblt.taxcore.servletservice.compress;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.io.ByteArrayInputStream;
import java.util.zip.ZipInputStream;
public class CompressTest {
public CompressTest() {
}
public static void main(String[] args)
throws IOException {
String a = "compressString";
byte[] abyte = a.getBytes();
byte[] testbyte1 = null;
byte[] testbyte2 = null;
testbyte1 = zip(abyte);
//处理1
testbyte2 = unzip(testbyte1);
//处理2
//byte[]<->String<->byte[]
//String b = new String(testbyte1);
//byte[] testbyte3 = b.getBytes();
//testbyte2 = unzip(testbyte3);
System.out.println(new String(testbyte2));
}
public static byte[] unzip(byte[] zipBytes)
throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(zipBytes);
String entryName = new String("servletservice");
ZipInputStream zis = new ZipInputStream(bais);
zis.getNextEntry();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
final int BUFSIZ = 4096;
byte inbuf[] = new byte[BUFSIZ];
int n;
while ( (n = zis.read(inbuf, 0, BUFSIZ)) != -1) {
baos.write(inbuf, 0, n);
}
byte[] data = baos.toByteArray();
zis.close();
return data;
}
public static byte[] zip(byte[] data)
throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipEntry ze = new ZipEntry("servletservice");
ZipOutputStream zos = new ZipOutputStream(baos);
zos.putNextEntry(ze);
zos.write(data, 0, data.length);
zos.close();
byte[] zipBytes = baos.toByteArray();
return zipBytes;
}
}
-------------------------------------------------------
按“处理1” ,执行是ok的
现在想经过byte[]<->String<->byte[]的转换处理后,在进行解压缩
按“处理2”,执行的时候就会出错
很奇怪,为什么经过byte[]<->String<->byte[]的转换处理就不行了呢?
-------------------------------------------------------
其实是解决这样的问题:
客户端上传参数字符串(经过压缩的,zip压缩)到服务器端,服务器接受参数(自然首先是要解压),上面这个test就在模拟这个
我想问大家是怎么样处理解压/压缩字符串的问题,是不是我上面的处理方法有问题还是有更好的处理方法,谢谢大家