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);
}
}