C # xml compression and decompression (use the built-in compression and decompression) (source code sharing)

Source: Internet
Author: User
I searched a lot of questions about XML compression and decompression on the Internet. Many solutions use open source or other components to compress and decompress XML, however, we cannot find the simplest implementation method of the system.
In fact, the principle is very simple. Convert XML into a string and compress the string. Decompression is the reverse process.

The functions are not complex. Let's not talk about them here.CodeNow: 

Using System;
Using System. text;
Using System. IO;
Using System. Io. compression;

Namespace Trying to be lazy. commonds
{
///   <Summary>
/// Use the default stream compression method to compress and save it as a file,
/// Convention 1: The first eight bytes of the file are stored in the long type, and the length of the byte before string compression is stored.
///   </Summary>
Public Class Zipfilehelper
{
Public Static Long Writestring (String Filename, String Data)
{
Long Lresult = 0 ;
Byte [] BS = encoding. utf8.getbytes (data );
Memorystream MS = New Memorystream ();
// Create a file stream
Filestream FS = New Filestream (filename, filemode. Create );
Try
{
Deflatestream compressedzipstream = Null ;
Try
{
Compressedzipstream = New Deflatestream (MS, compressionmode. Compress, True );
Compressedzipstream. Write (BS, 0 , BS. Length );// Compress the data in BS in binary format and write it to Ms.
Lresult = Ms. length;
}
Finally
{
If ( Null ! = Compressedzipstream)
Compressedzipstream. Close ();
}
Byte [] BL = bitconverter. getbytes (( Long ) BS. Length ); // Save the uncompressed Data Length for restoration.
FS. Write (BL, 0 , BL. Length );
Ms. writeto (FS );
FS. Flush ();
}
Finally
{
FS. Close ();
Ms. Close ();
}
Return Lresult;
}
///   <Summary>
/// Decompress the package and return the byte array.
///   </Summary>
///   <Param name = "FILENAME"> </param>
///   <Returns> </returns>
Public Static Byte [] Loadbytes ( String Filename)
{
// Bytes of the source data length
Byte [] BS;
Long Nsize; // Actual value after BS Conversion
// Result
Byte [] Decompressbuffer;
Filestream FS = New Filestream (filename, filemode. Open );
Try
{
// Length of bytes read from the source data
Byte [] BL = New Byte [ 8 ];
FS. Read (BL, 0 , 8 );
Nsize = bitconverter. toint64 (BL, 0 );
// Write the binary data of Characters in the file stream to the BS array.
BS = New Byte [Fs. Length- 8 ];
FS. Read (BS, 0 ,( Int ) Fs. Length- 8 );
}
Finally
{
FS. Close ();
}

// BS data is written to the MS stream.
Memorystream MS = New Memorystream ();
Deflatestream decompressedzipstream = Null ;
Try
{
Ms. Write (BS, 0 , BS. Length );
Ms. Position = 0 ;
// Extract
Decompressedzipstream = New Deflatestream (MS, compressionmode. Decompress );
Decompressedzipstream. Flush ();
// Decompressed data
Decompressbuffer = New Byte [Nsize];
Decompressedzipstream. Read (decompressbuffer, 0 , Decompressbuffer. Length );
}
Finally
{
If ( Null ! = Decompressedzipstream)
Decompressedzipstream. Close ();
If ( Null ! = MS)
Ms. Close ();
}
Return Decompressbuffer;
}
}

}

 

Original Works are from hard work, please describe them for reprintingArticleSource:Http://blog.csdn.net/kfarvidOrHttp://www.cnblogs.com/kfarvid/ 

 

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.