Compressing and decompressing data using GZipStream in C #

Source: Internet
Author: User

Requires the first using

Using System.IO;
Using System.IO.Compression;

Compression
private void Compress (stream source, stream dest) {
using (GZipStream ZIPstream = new GZipStream (dest, compressionmode.compress, True)) {
byte[] buf = new byte[1024];
int Len;
while (len = source. Read (buf, 0, buf. Length)) > 0) {
Zipstream.write (buf, 0, Len);
}
}
}

Extract
private void decompress (stream source, stream dest) {
using (GZipStream ZIPstream = new GZipStream (source, compressionmode.decompress, True)) {
byte[] buf = new byte[1024];
int Len;
while (len = Zipstream.read (buf, 0, buf.) Length)) > 0) {
Dest. Write (buf, 0, Len);
}
}
}

Example:

internal void Exportpackage (string filename, string expression, Stream Recordstream, string postscript) {
Short Version = 1;
using (MemoryStream stream = new MemoryStream ()) {
Write version number
Storebase.writevalue (stream, version);
Write to Data entity
byte[] Databytes = Objecthelper.serialize (this. Activeobject);
Storebase.writevalue (stream, databytes);
Write function
Storebase.writevalue (stream, _functiondeclares.count);
foreach (String typeName in _functiondeclares.keys) {
Storebase.writevalue (stream, typeName);
Storebase.writevalue (Stream, _functiondeclares[typename]. Count);

foreach (Funcdescrption func in _functiondeclares[typename]) {
Storebase.writevalue (Stream, Func. FuncName);
Storebase.writevalue (Stream, Func. Nameparams);
Storebase.writevalue (Stream, Func. Descrption);
}
}
Write-expression
Storebase.writevalue (stream, expression);
Write Execution history
byte[] recordbytes = new Byte[recordstream.length];
Recordstream.read (recordbytes, 0, recordbytes.length);
Storebase.writevalue (stream, recordbytes);
Write Debug PostScript
Storebase.writevalue (stream, PostScript);

Compression
Stream. Position = 0;
using (FileStream fs = new FileStream (filename, filemode.create)) {
Compress (stream, FS);
}
}
}

internal void Importpackage (string filename, Loadpackageeventhandler handler) {
Short Version = 1;
using (MemoryStream stream = new MemoryStream ()) {
Decompression
using (FileStream fs = new FileStream (filename, FileMode.Open)) {
Decompress (FS, stream);
}
Stream. Position = 0;

Read version number
Version = Storebase.readvalue (stream, version);
Reading data Entities
byte[] databytes = Storebase.readvalue (stream, new byte[0));
This. Activeobject = Objecthelper.deserialize (databytes);
Read function
_functiondeclares.clear ();
int typecount = Storebase.readvalue (stream, (int) 0);
for (int i = 0; i < Typecount; i++) {
String typeName = Storebase.readvalue (stream, String. Empty);
int funccount = Storebase.readvalue (stream, (int) 0);

String funcName = String. Empty;
String nameparams = String. Empty;
String descrption = String. Empty;
for (int j = 0; J < Funccount; J + +) {
FuncName = Storebase.readvalue (stream, funcName);
Nameparams = Storebase.readvalue (stream, nameparams);
Descrption = Storebase.readvalue (stream, descrption);

This. Addfunctiondeclare (TypeName, FuncName, Nameparams, descrption);
}
}
Reading an expression
string expression = Storebase.readvalue (stream, String. Empty);
Read Execution history
byte[] recordbytes = Storebase.readvalue (stream, new byte[0));
MemoryStream recordstream = new MemoryStream ();
Recordstream.write (recordbytes, 0, recordbytes.length);
recordstream.position = 0;
Read Debug PostScript
String PostScript = Storebase.readvalue (stream, String. Empty);

Handler (expression, Recordstream, PostScript);
}
}

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.