Prepare to design WebService for the company store to transfer data. to compress and improve performance, collect the following articles:

Source: Internet
Author: User
Tags crc32

Http://www.microsoft.com/taiwan/msdn/columns/adonet/AdoNet_20041231.htm
Question about ADO. Net 1.x dataset serialization

Http://support.microsoft.com /? Kbid = 829740
Improve dataset serialization and remote processing performance

Use sharpziplib for compression and decompression [original]

Because of work requirements, you need to use C # To write a program for compression and decompression. There are three methods to search from the Internet: 1. Call WinRAR's interface functions for implementation, the disadvantage is that WinRAR must be installed, so it is not universal. 2. According to msdn (use the zip class in the J # class library to compress files and data) 3. Use sharpziplib for compression and decompression. This is a software developed by a company that can be encapsulated in commercial use.. Net class. Http://www.icsharpcode.net/introduce the icsharpcode. sharpziplib. dll file into the. NET program from the zookeeper package. When writing this class, I referred to the class written by a csdn user. His class can only compress the files under the binder, but cannot compress the files under the subdirectories AND THEIR subdirectories, only packages without subdirectories can be decompressed. So I improved it so that it can compress sub-directories and their files and decompress the compressed packages with sub-directories and their files. The following is the program code and its comments.

/// The following zipclass is the compression class.
Using system;
Using system. IO;

Using icsharpcode. sharpziplib. checksums;
Using icsharpcode. sharpziplib. Zip;
Using icsharpcode. sharpziplib. gzip;

Namespace webpos
{
Public class zipclass
{
Public String cutstr = "";

# I have not studied this function of region.
Public void zipfile (string filetozip, string zipedfile, int compressionlevel, int blocksize)
{
// If the file is not found, an error is returned.
If (! System. Io. file. exists (filetozip ))
{
Throw new system. Io. filenotfoundexception ("the specified file" + filetozip + "cocould not be found. zipping aborderd ";
}

System. Io. filestream streamtozip = new system. Io. filestream (filetozip, system. Io. filemode. Open, system. Io. fileaccess. Read );
System. Io. filestream zipfile = system. Io. file. Create (zipedfile );
Zipoutputstream zipstream = new zipoutputstream (zipfile );
Zipentry = new zipentry ("zippedfile ";
Zipstream. putnextentry (zipentry );
Zipstream. setlevel (compressionlevel );
Byte [] buffer = new byte [blocksize];
System. int32 size = streamtozip. Read (buffer, 0, buffer. Length );
Zipstream. Write (buffer, 0, size );
Try
{
While (size <streamtozip. length)
{
Int sizeread = streamtozip. Read (buffer, 0, buffer. Length );
Zipstream. Write (buffer, 0, sizeread );
Size + = sizeread;
}
}
Catch (system. Exception ex)
{
Throw ex;
}
Zipstream. Finish ();
Zipstream. Close ();
Streamtozip. Close ();
}
# Endregion

// Get all directoryinfo
Private void direct (directoryinfo Di, ref zipoutputstream S, CRC32 CRC)
{
// Directoryinfo di = new directoryinfo (filenames );
Directoryinfo [] dirs = Di. getdirectories ("*";

// Traverse all subdirectories under the Directory
Foreach (directoryinfo dirnext in dirs)
{
// Add all files in the directory to the zipoutputstream s compressed stream.
Fileinfo [] A = dirnext. getfiles ();
This. writestream (ref S, A, CRC;

// Recursive call is performed until all directories are traversed.
Direct (dirnext, ref S, CRC );
}
}

Private void writestream (ref zipoutputstream S, fileinfo [] A, CRC32 CRC)
{
Foreach (fileinfo FI IN
{
// String FFN = Fi. fullname;
Filestream FS = Fi. openread ();

Byte [] buffer = new byte [fs. Length];
FS. Read (buffer, 0, buffer. Length );

// Zipentry entry = new zipentry (File); Path. getfilename (file)
String file = Fi. fullname;
File = file. Replace (cutstr ,"";

Zipentry entry = new zipentry (File );

Entry. datetime = datetime. now;

// Set size and the CRC, because the information
// About the size and CRC shocould be stored in the header
// If it is not set it is automatically written in the footer.
// (In this case size = CRC =-1 in the header)
// Some zip programs have problems with zip files that don't store
// The size and CRC in the header.
Entry. size = FS. length;
FS. Close ();

CRC. Reset ();
CRC. Update (buffer );

Entry. CRC = CRC. value;

S. putnextentry (entry );

S. Write (buffer, 0, buffer. Length );
}
}

/// <Summary>
/// Main Function
/// </Summary>
/// <Param name = "ARGs"> ARGs [0] specifies the path of the directory to be compressed.
/// Example: D :\\ Temp \ (note that \ is added after temp, but you can modify it when writing a program)
/// ARGs [1] is the compressed file name and Path
/// Example: D: \ temp.zip
/// </Param>
Public void zipfilemain (string [] ARGs)
{
// String filenames = directory. getfiles (ARGs [0]);

CRC32 CRC = new CRC32 ();
Zipoutputstream S = new zipoutputstream (file. Create (ARGs [1]);

S. setlevel (6); // 0-store only to 9-means best compression

Directoryinfo di = new directoryinfo (ARGs [0]);

Fileinfo [] A = Di. getfiles ();

Cutstr = ARGs [0]. Trim ();
// Compress all files in this directory
Writestream (ref S, A, CRC );
// Compress the subdirectories and their files in this directory
Direct (Di, ref S, CRC );

S. Finish ();
S. Close ();
}
}
}

/// The following is the class for extracting files
Using system;
Using system. text;
Using system. collections;
Using system. IO;
Using system. diagnostics;
Using system. runtime. serialization. formatters. Binary;
Using system. Data;

Using icsharpcode. sharpziplib. Bzip2;
Using icsharpcode. sharpziplib. Zip;
Using icsharpcode. sharpziplib. Zip. compression;
Using icsharpcode. sharpziplib. Zip. Compression. streams;
Using icsharpcode. sharpziplib. gzip;

Namespace webpos
{
Public class unzipclass
{
Public void unzip (string [] ARGs)
{
Zipinputstream S = new zipinputstream (file. openread (ARGs [0]);

Zipentry theentry;
While (theentry = S. getnextentry ())! = NULL)
{
String directoryname = path. getdirectoryname (ARGs [1]);
String filename = path. getfilename (theentry. Name );

// Generate the extract directory
Directory. createdirectory (directoryname );

If (filename! = String. Empty)
{
// If the file size is 0 after the file is deleted, it indicates that the file is empty and therefore does not need to be written in.
If (theentry. compressedsize = 0
Break;
// Extract the file to the specified directory
Directoryname = path. getdirectoryname (ARGs [1] + theentry. Name );
// Create the following directories and subdirectories
Directory. createdirectory (directoryname );

Filestream streamwriter = file. Create (ARGs [1] + theentry. Name );

Int size = 2048;
Byte [] DATA = new byte [2048];
While (true)
{
Size = S. Read (data, 0, Data. Length );
If (size> 0)
{
Streamwriter. Write (data, 0, size );
}
Else
{
Break;
}
}
Streamwriter. Close ();
}
}
S. Close ();
}
}
}

It is the best way to compress dataset when a large amount of data must be transmitted on WebService (which cannot be avoided, this reduces the amount of time used during network transmission.
Next, let's take a test on the dataset compression method that I have mastered. I hope that the experts will have an axe and a reference for those who need it.
Note: The WebService that has been compressed into dataset lacks universality. Please weigh the use.

The test source code (only compressed part) and result are as follows:
Condition: the size of the dataset before compression is 16891323 bytes (about 16.12 MB ).
Environment:. Net formatwork 1.1
System: Windows XP SP1
Hardware: P4 (2.4 GB) + 256ddr

Solution 1: datasetsurrogate + binaryformatter
Public Function datasettobyte (byval dataset as dataset) as byte ()
Dim DSS as new datasetsurrogate (Dataset)
Dim MS as new IO. memorystream
Dim BF as new binaryformatter
BF. serialize (MS, DSS)
Dim reval () as byte = Ms. toarray ()
Ms. Close ()
DSS = nothing
MS = nothing
BF = nothing
Return reval
End Function

Solution 2: datasetsurrogate + binaryformatter + sharpziplib
Public Function datasettobyte (byval dataset as dataset) as byte ()
Dim MS as new IO. memorystream
Dim ZOS as new zipoutputstream (MS)
ZOS. putnextentry (New zipentry (Dataset. datasetname ))
Dim BF as new binaryformatter
Dim DSS as datasetsurrogate = new datasetsurrogate (Dataset)
BF. serialize (ZOS, DSS)
ZOS. closeentry ()
ZOS. Close ()
Dim reval as byte () = Ms. toarray
Ms. Close ()
MS = nothing
ZOS = nothing
BF = nothing
DSS = nothing
Return reval
End Function

Solution 3: zlib
Public Function datasettobyte (byval dataset as dataset) as byte ()
Dim MS as new system. Io. memorystream
Dataset. writexml (MS, xmlwritemode. writeschema)
Dim reval () as byte = Ms. toarray
Ms. Close ()
MS = nothing
Dim zlib as new vbzlib. Compress
Zlib. compressbyte (reval)
Zlib = nothing
Return reval
End Function

Result (after compression ):
Solution 1: 4420881 bytes (compressed by about 73.83%), time consumed: 23200 Ms
Solution 2: 696881 bytes (compressed by about 95.87%), time consumed: 26621 Ms
Solution 3: 422990 bytes (compressed by about 97.50%), time consumed: 680 Ms

It seems that no matter the compression rate or time consumption, solution 3 is the best, but a third-party component is used. The CPU usage of the first two solutions is unacceptable.

Http://www.zlib.net/zlib.html's

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.