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/