Java and C # compression and decompression

Source: Internet
Author: User
Tags uncompress

Due to project requirements, the project needs to be compressed in C #, then decompressed in java, or reversed, compressed in Java, decompressed in C #. The following code has been tested and verified.

Key Technical Points and experiences:

The compression results are Base64 encoded to facilitate printing and debugging on Java or C #, and transfer between different applications (such as webservice ), the disadvantage is that it is about 35% larger than the size before transcoding.
The character string uses UTF-8 encoding to obtain the byte array to ensure that both ends are universal. If the application requires encoding, both ends can also be changed to other encoding methods.
From the perspective of Java and C # code, there are minor differences between the two, but the two are basically the same in terms of thinking.
Another memo: in Java, the Stream class should be closed in time, otherwise the output results will be incomplete, even if flush is called
C #'s using is really easy to use. Java's similar language features are only supported in 1.7...
Java: Creates a simple WebService using Session Bean, provides a simple call to SayHello, and then creates a winform application in C #, adds service references, and references Java's webservice WSDL.

The Code is as follows:

Java code:

[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 ("extract:" + 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 ("extract:" + 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 # code:

[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 );
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.