The compression example demonstrates how to read data from a file and write compressed data to a new file, and how to read and write compressed data to a new file. Specifically, this example shows how to compress data from an existing • file and write it into a new file. • Read compressed data from files and compressed data. Function Data (compressalgorithm ){
VaR Testobject = [1, 2, 3, "sampletext", {value: "sampleobject"}];
Try {
//
// Note: Read and Write positions are shared when iinputstream and ioutputstream
// Are queried directly from irandomaccessstream, so we need either rewind stream
// Before reading or get separate streams using getinput/outputstreamat
//
VaR Stream = New Windows. Storage. Streams. inmemoryrandomaccessstream ();
VaR Compressor;
//
// It is safe to pass-through undefined value as compressalgorithm in which case default algorithm (Xpress) will be used.
// For the purposes of this sample both usages are shown.
//
If (Typeof (Compressalgorithm) = "undefined "){
//
// If you don't have any specific algorithm requirements-this is recommended way to initialize compressor object
//
Compressor = New Winjs. compressor (stream. getoutputstreamat (0 ));
} Else {
Compressor = New Winjs. compressor (stream. getoutputstreamat (0), compressalgorithm );
}
VaRDecompressor =NewWinjs. decompressor (stream. getinputstreamat (0 ));
Showprogress ("compressor object created ");
Compressor. compressasync (testobject). Then ( Function (){
Showprogress ("test object COMPRESSED ");
Compressor. Close ();
Return Decompressor. readobjectasync ();
}, Onerror). Then ( Function (Decompressedobject ){
Decompressor. Close ();
Showprogress ("test object decompressed:" + decompressedobject );
If (JSON. stringify (testobject) === JSON. stringify (decompressedobject )){
Showprogress ("test object matches decompressed one ");
} Else {
Onerror ( New Error ("test object doesn' t match decompressed one "));
}
}, Onerror );
// Alternatively:
/*
Showprogress ("compressor object created ");
Compressor. compressasync (testobject). Then (function (){
Showprogress ("test object COMPRESSED ");
Decompressor. readobjectasync (). Then (function (decompressedobject ){
Showprogress ("test object decompressed:" + decompressedobject );
If (JSON. stringify (testobject) = JSON. stringify (decompressedobject )){
Showprogress ("test object matches decompressed one ");
} Else {
Onerror (New error ("test object doesn't match decompressed one "));
}
}, Onerror)
}, Onerror );
*/
}Catch (E ){
Onerror (E );
}
}
Complete example:/files/risk/windows8/compressed sample.rar