java和C#的压缩和解压缩
由于项目需求,需要在C#中压缩,然后在java里解压缩,或者倒过来,在Java里压缩,C#里解压缩,以下代码经测试验证通过。
关键技术点和体会:
压缩的结果采用Base64编码,方便在Java端或者C#端打印出来调试,也方便在不同的应用间传输(如webservice),缺点是比转码前体积变大了约35%
字符串采用UTF-8编码获得byte数组,保证两端通用,如果应用对编码有要求,两端同时改为其他编码方式也可以
从Java和C#的代码看,两者代码上有细微差别,但是思路方面两者基本是一样的
另外一个备忘,Java里边,Stream类要及时close,不然输出的结果是不完整的,即使调用了flush
C#的using真好用,Java的类似语言特性在1.7才支持...
Java,用Session Bean建立了一个简单的WebService,提供一个简单的调用SayHello,然后C#里建立一个winform应用,添加服务引用,引用Java的webservice WSDL。
具体过程不多说,代码如下:
Java代码:
[javascript]
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
@Stateless
@WebService
public class TestWebService {
@WebMethod
public String SayHello(String name) throws Exception {
String t = uncompress(name);
return compress("解压:" + t);
}
@WebMethod(exclude = true)
public static String compress(String str) throws IOException {
if (str == null || str.length() == 0) {
return "";
}
byte[] tArray;
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
try {
gzip.write(str.getBytes("UTF-8"));
gzip.flush();
} finally {
gzip.close();
}
tArray = out.toByteArray();
out.close();
BASE64Encoder tBase64Encoder = new BASE64Encoder();
return tBase64Encoder.encode(tArray);
}
@WebMethod(exclude = true)
public static String uncompress(String str) throws IOException {
if (str == null || str.length() == 0) {
return "";
}
BASE64Decoder tBase64Decoder = new BASE64Decoder();
byte[] t = tBase64Decoder.decodeBuffer(str);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(t);
GZIPInputStream gunzip = new GZIPInputStream(in);
try {
byte[] buffer = new byte[256];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
}finally{
gunzip.close();
}
in.close();
out.close();
return out.toString("UTF-8");
}
}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
@Stateless
@WebService
public class TestWebService {
@WebMethod
public String SayHello(String name) throws Exception {
String t = uncompress(name);
return compress("解压:" + t);
}
@WebMethod(exclude = true)
public static String compress(String str) throws IOException {
if (str == null || str.length() == 0) {
return "";
}
byte[] tArray;
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
try {
gzip.write(str.getBytes("UTF-8"));
gzip.flush();
} finally {
gzip.close();
}
tArray = out.toByteArray();
out.close();
BASE64Encoder tBase64Encoder = new BASE64Encoder();
return tBase64Encoder.encode(tArray);
}
@WebMethod(exclude = true)
public static String uncompress(String str) throws IOException {
if (str == null || str.length() == 0) {
return "";
}
BASE64Decoder tBase64Decoder = new BASE64Decoder();
byte[] t = tBase64Decoder.decodeBuffer(str);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(t);
GZIPInputStream gunzip = new GZIPInputStream(in);
try {
byte[] buffer = new byte[256];
int n;
补充:软件开发 , C# ,