Reduce data transmission volume: Use zip.

Source: Internet
Author: User
Tags deflater

Previous

Using soap to transmit data over the network results in a slow transmission speed due to a large amount of data.
To solve this problem, you can zip the file before passing it.
Because almost all data is transmitted in the database, the compression space is very large.
Tested, it can be compressed to the original 1/15

Method 1: use Java CompressionAlgorithm. Because the DLL is directly provided by. net.

Reference vjslib. dll
------------------------ Code -----------------------
Using system;
Using java. IO;
Using java.util.zip;
Using COM. Ms. vjsharp. @ struct;
Namespace soapgrid
{
/// <Summary>
/// Zip summary.
/// </Summary>
Public class zip
{

Public static sbyte [] compressstring (string S)
{
Deflater F = new Deflater (Deflater. best_compression );
Sbyte [] DATA = javastructmarshalhelper. converttobytearray (s );
F. setinput (data );
F. Finish ();

Bytearrayoutputstream o = new bytearrayoutputstream (data. Length );
Try
{
Sbyte [] Buf = new sbyte [1024];
While (! F. Finished ())
{
Int got = f. Deflate (BUF );
O. Write (BUF, 0, got );
}
}
Finally
{
O. Close ();
}
Return O. tobytearray ();
}

Public static string decompressstring (sbyte [] S)
{
Inflater F = new Inflater ();
F. setinput (s );

Bytearrayoutputstream o = new bytearrayoutputstream (S. Length );
Try
{
Sbyte [] Buf = new sbyte [1024];
While (! F. Finished ())
{
Int got = f. Inflate (BUF );
O. Write (BUF, 0, got );
}
}
Finally
{
O. Close ();
}
Return javastructmarshalhelper. converttostring (O. tobytearray ());
}

}
}

---------------------------------- End code -------------------------
However, an error may occur when the data volume is large (more than 2 MB.
The reason may be that the data types of Java and C # are not completely uniform. For example, the conversion between byte and sbyte may cause problems.

Method 2: Use sharpziplib. Open source and C # development.
You need to download the DLL of sharpziplib, or download the source code and compile it yourself.
He provides examples (mostly about file operations)

---------------------------------- Code ---------------------
Using system;
Using system. IO;

Using icsharpcode. sharpziplib. Zip. compression;
Using icsharpcode. sharpziplib. Zip. Compression. streams;
Using icsharpcode. sharpziplib. Zip;
Using icsharpcode. sharpziplib. Bzip2;
Namespace soapgrid
{
/// <Summary>
/// Summary of cszip.
/// </Summary>
Public class cszip
{
Public static byte [] compressbytes (byte [] data)
{
Deflater F = new Deflater (Deflater. best_compression );
F. setinput (data );
F. Finish ();

Memorystream o = new memorystream (data. Length );

Try
{
Byte [] Buf = new byte [1, 1024];
While (! F. isfinished)
{
Int got = f. Deflate (BUF );
O. Write (BUF, 0, got );
}
}
Finally
{
O. Close ();
}
Return O. toarray ();
}
Public static byte [] decompressbytes (byte [] data)
{
Inflater F = new Inflater ();
F. setinput (data );

Memorystream o = new memorystream (data. Length );
Try
{
Byte [] Buf = new byte [1, 1024];
While (! F. isfinished)
{
Int got = f. Inflate (BUF );
O. Write (BUF, 0, got );
}
}
Finally
{
O. Close ();
}
Return O. toarray ();
}

}
}
--------------------------------- End code -----------------------
After testing, there is no problem with large data volumes.
The test shows that the compression ratio reaches 22 times.

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.