程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> java和C#的壓縮和解壓縮

java和C#的壓縮和解壓縮

編輯: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); 
        } 
    } 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved