Windows 8 Store Apps Learning 70 Other: file compression and decompression

Source: Internet
Author: User
Tags empty tostring xmlns

Re-imagine Windows 8 store Apps (70)-Other: File compression and decompression, Windows Store-related operations, app and web, several Core applications, page lifecycle and program lifecycle

Author: WEBABCD

Introduced

Re-imagine the other Windows 8 Store Apps

File compression and decompression

Actions related to Windows store

App and web

The application of several Core

The life cycle of the page and the lifecycle of the program

Example

1. Demonstrates how to compress and decompress files

Feature/compression.xaml

<page x:class= "XamlDemo.Feature.Compression" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "xmlns:local=" using:XamlDemo.Feature "xmlns:d=" http:// schemas.microsoft.com/expression/blend/2008 "xmlns:mc=" http://schemas.openxmlformats.org/markup-compatibility/ 2006 "mc:ignorable=" D "> <grid background=" Transparent "> <stackpanel margin=" 0 0 0 "
                
            ; <textblock name= "lblmsg" fontsize= "14.667"/> <button name= "btnxpress" content= "Xpress" margin= "0 0 0" click= "Btnxpress_click"/> <button name= "Btnxpresshuff" content= "Xpresshuff" margin= "0 0 0 "click=" Btnxpresshuff_click "/> <button name=" Btnmszip "content=" Mszip "margin=" 0 0 0 "C lick= "Btnmszip_click"/> <button name= "btnlzms" content= "lzms" margin= "0 0 0" click= "btnlzms_cl IcK "/> </StackPanel> </Grid> </Page> 

Feature/compression.xaml.cs

* * Demonstrates how to compress and decompress files * Note: For very small data compression may be larger than before compression, has been compressed algorithm files such as JPG mp3 mp4 and other compression effect is not obvious also may be larger than before * * using System;
Using Windows.storage;
Using Windows.Storage.Compression;
Using Windows.Storage.Pickers;
Using Windows.Storage.Streams;
Using Windows.UI.Xaml;
Using Windows.UI.Xaml.Controls;
    
Using Xamldemo.common;
        Namespace Xamldemo.feature {public sealed partial class Compression:page {public Compression () {this.
        InitializeComponent ();
            } private void Btnxpress_click (object sender, RoutedEventArgs e) {//XPRESS algorithm
        Compressiondemo (compressalgorithm.xpress);  } private void Btnxpresshuff_click (object sender, RoutedEventArgs e) {//With Huffman-encoded XPRESS
        Algorithm Compressiondemo (Compressalgorithm.xpresshuff);
            } private void Btnmszip_click (object sender, RoutedEventArgs e) {//Mszip algorithm Compressiondemo (Compressalgorithm.mszip); } private void Btnlzms_click (object sender, RoutedEventArgs e) {//LZMS algorithm C
        Ompressiondemo (COMPRESSALGORITHM.LZMS);
                Private async void Compressiondemo (Compressalgorithm algorithm) {try { if (!
    
                Helper.ensureunsnapped ()) return;
                Select a file to be compressed var picker = new Fileopenpicker (); Picker.
                Filetypefilter.add ("*"); var originalfile = await picker.
                Picksinglefileasync ();
    
                if (originalfile = null) return;
                Lblmsg.text = "Selected file:" + originalfile.name;
    
    
    
                Lblmsg.text + = Environment.NewLine; var compressedfilename = Originalfile.name + ". Compressed"; Note: In order to have access to read/write. Compressed files, you need to add a "File type association" declaration in Package.appxmanifest, and do the related configuration var compressedfile = AwaIt KnownFolders.DocumentsLibrary.CreateFileAsync (Compressedfilename, Creationcollisionoption.generateuniquename)
                ;
                Lblmsg.text + = "created a new file to save the compressed file:" + compressedfile.name;
    
                Lblmsg.text + = Environment.NewLine; using (var originalinput = await Originalfile.openreadasync ())//Open the original file using (var compressedoutput = Awa It Compressedfile.openasync (fileaccessmode.readwrite))//Open the compressed data target file (currently an empty file) using (var compressor =
                    New Compressor (Compressedoutput.getoutputstreamat (0), algorithm, 0))//instantiation Compressor { var bytescompressed = await Randomaccessstream.copyasync (originalinput, compressor);
                    The target file that writes the raw data to the compressed data lblmsg.text + = "has written the original file's data to:" + compressedfile.name;
    
                    Lblmsg.text + = Environment.NewLine; var finished = await compressor. Finishasync (); Compress the data in the specified file Lblmsg.text = "Data in this filehas been compressed: "+ compressedfile.name;
                    Lblmsg.text + = Environment.NewLine;
                    Lblmsg.text + + "compressed before size:" + bytescompressed.tostring () + "-Compressed after Size:" + compressedOutput.Size.ToString ();
                Lblmsg.text + = Environment.NewLine; var decompressedfilename = Originalfile.name + ". decompressed"; Note: In order to have access to read/write. decompressed files, you need to add a "File type association" declaration in Package.appxmanifest, and do the related configuration var decompressedfile = Awa It KnownFolders.DocumentsLibrary.CreateFileAsync (Decompressedfilename,
                Creationcollisionoption.generateuniquename);
                Lblmsg.text + = "created a new file to save the uncompressed file:" + decompressedfile.name;
    
                Lblmsg.text + = Environment.NewLine; using (var compressedinput = await Compressedfile.opensequentialreadasync ())//Open compressed file using (Var decom
    Pressedoutput = await Decompressedfile.openasync (fileaccessmode.readwrite))//Open target file for uncompressed data (currently an empty file)            using (var decompressor = new Decompressor (compressedinput))//instantiation Compressor { var bytesdecompressed = await Randomaccessstream.copyasync (decompressor, decompressedoutput);
                    Extract the data, and save the extracted data to the target file Lblmsg.text + = "File decompression complete:" + decompressedfile.name;
                    Lblmsg.text + = Environment.NewLine;
                    Lblmsg.text + = "Uncompressed size:" + bytesdecompressed.tostring ();
                Lblmsg.text + = Environment.NewLine; } catch (Exception ex) {lblmsg.text = ex.
            ToString (); }
        }
    }
}

2. Demo of actions related to Windows store

Feature/storedemo.xaml

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.