Windows 8 Compression

Source: Internet
Author: User
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

Related Article

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.