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;
   while ((n = gunzip.read(buffer)) >= 0) {
    out.write(buffer, 0, n);
   }
  }finally{
   gunzip.close();
  }
  in.close();
  out.close();
  
  return out.toString("UTF-8");
 }

}

C#的代码:

[csharp]
using System; 
using System.IO; 
using System.IO.Compression; 
using System.Text; 
using System.Windows.Forms; 
using testWebService.ServiceReference1; 
 
namespace testWebService 
{ 
    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
 
        private void btnSayOK_Click(object sender, EventArgs e) 
        { 
 
            string name = txtName.Text; 
            string zipedName = Zip(name); 
 
            TestWebServiceClient client = new TestWebServiceClient(); 
            string result = client.SayHello(zipedName); 
            MessageBox.Show(UnZip(result)); 
        } 
 
        public static string Zip(string value) 
        { 
            byte[] byteArray = Encoding.UTF8.GetBytes(value); 
            byte[] tmpArray; 
 
            using (MemoryStream ms = new MemoryStream()) 
            { 
                using (GZipStream sw = new GZipStream(ms, CompressionMode.Compress)) 
                { 
                    sw.Write(byteArray, 0, byteArray.Length); 
                    sw.Flush(); 
                } 
                tmpArray = ms.ToArray(); 
            } 
            return Convert.ToBase64String(tmpArray); 
        } 
 
 
        public static string UnZip(string value) 
        { 
            byte[] byteArray = Convert.FromBase64String(value); 
            byte[] tmpArray; 
 
            using (MemoryStream msOut = new MemoryStream()) 
            { 
                using (MemoryStream msIn = new MemoryStream(byteArray)) 
                { 
                    using (GZipStream swZip = new GZipStream(msIn, CompressionMode.Decompress)) 
                    { 
                        swZip.CopyTo(msOut); 
                        tmpArray = msOut.ToArray(); 
                    } 
                } 
            } 
            return Encoding.UTF8.GetString(tmpArray); 
        } 
    } 
} 

你可能感兴趣的:(随笔)