重新想象 Windows 8 Store Apps (70) - 其它: 檔案壓縮和解壓縮, 與 Windows 商店相關的操作, app 與 web, 幾個 Core 的應用, 頁面的生命週期和程式的生命週期
作者:webabcd
介紹
重新想象 Windows 8 Store Apps 之 其它
檔案壓縮和解壓縮
與 Windows 商店相關的操作
app 與 web
幾個 Core 的應用
頁面的生命週期和程式的生命週期
樣本
1、示範如何壓縮和解壓縮檔案
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="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> <Button Name="btnXpress" Content="Xpress" Margin="0 10 0 0" Click="btnXpress_Click" /> <Button Name="btnXpressHuff" Content="XpressHuff" Margin="0 10 0 0" Click="btnXpressHuff_Click" /> <Button Name="btnMszip" Content="Mszip" Margin="0 10 0 0" Click="btnMszip_Click" /> <Button Name="btnLzms" Content="Lzms" Margin="0 10 0 0" Click="btnLzms_Click" /> </StackPanel> </Grid></Page>
Feature/Compression.xaml.cs
/* * 示範如何壓縮和解壓縮檔案 * * 註:對於非常小的資料壓縮後可能比壓縮前還要大,已經經過壓縮演算法的檔案如 jpg mp3 mp4 等再壓縮效果不明顯也可能比之前還大 */ 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 演算法 CompressionDemo(CompressAlgorithm.Xpress); } private void btnXpressHuff_Click(object sender, RoutedEventArgs e) { // 具有霍夫曼編碼的 XPRESS 演算法 CompressionDemo(CompressAlgorithm.XpressHuff); } private void btnMszip_Click(object sender, RoutedEventArgs e) { // Mszip 演算法 CompressionDemo(CompressAlgorithm.Mszip); } private void btnLzms_Click(object sender, RoutedEventArgs e) { // Lzms 演算法 CompressionDemo(CompressAlgorithm.Lzms); } private async void CompressionDemo(CompressAlgorithm algorithm) { try { if (!Helper.EnsureUnsnapped()) return; // 選擇一個準備壓縮的檔案 var picker = new FileOpenPicker(); picker.FileTypeFilter.Add("*"); var originalFile = await picker.PickSingleFileAsync(); if (originalFile == null) return; lblMsg.Text = "選中了檔案:" + originalFile.Name; lblMsg.Text += Environment.NewLine; var compressedFilename = originalFile.Name + ".compressed"; // 注意:為了有讀寫 .compressed 檔案的許可權,需要在 Package.appxmanifest 中新增一個“檔案類型關聯”聲明,並做相關配置 var compressedFile = await KnownFolders.DocumentsLibrary.CreateFileAsync(compressedFilename, CreationCollisionOption.GenerateUniqueName); lblMsg.Text += "建立了一個新檔案,用於儲存壓縮後的檔案:" + compressedFile.Name; lblMsg.Text += Environment.NewLine; using (var originalInput = await originalFile.OpenReadAsync()) // 開啟原始檔案 using (var compressedOutput = await compressedFile.OpenAsync(FileAccessMode.ReadWrite)) // 開啟壓縮後的資料的目標檔案(目前是一個空檔案) using (var compressor = new Compressor(compressedOutput.GetOutputStreamAt(0), algorithm, 0)) // 執行個體化 Compressor { var bytesCompressed = await RandomAccessStream.CopyAsync(originalInput, compressor); // 將未經處理資料寫入到壓縮後的資料的目標檔案 lblMsg.Text += "已將原始檔案的資料寫入到:" + compressedFile.Name; lblMsg.Text += Environment.NewLine; var finished = await compressor.FinishAsync(); // 壓縮指定檔案中的資料 lblMsg.Text += "此檔案中的資料已被壓縮:" + compressedFile.Name; lblMsg.Text += Environment.NewLine; lblMsg.Text += "壓縮前大小:" + bytesCompressed.ToString() + " - 壓縮後大小:" + compressedOutput.Size.ToString(); lblMsg.Text += Environment.NewLine; } var decompressedFilename = originalFile.Name + ".decompressed"; // 注意:為了有讀寫 .decompressed 檔案的許可權,需要在 Package.appxmanifest 中新增一個“檔案類型關聯”聲明,並做相關配置 var decompressedFile = await KnownFolders.DocumentsLibrary.CreateFileAsync(decompressedFilename, CreationCollisionOption.GenerateUniqueName); lblMsg.Text += "建立了一個新檔案,用於儲存解壓縮後的檔案:" + decompressedFile.Name; lblMsg.Text += Environment.NewLine; using (var compressedInput = await compressedFile.OpenSequentialReadAsync()) // 開啟經過壓縮的檔案 using (var decompressedOutput = await decompressedFile.OpenAsync(FileAccessMode.ReadWrite)) // 開啟解壓縮後的資料的目標檔案(目前是一個空檔案) using (var decompressor = new Decompressor(compressedInput)) // 執行個體化 Compressor { var bytesDecompressed = await RandomAccessStream.CopyAsync(decompressor, decompressedOutput); // 解壓縮資料,並將解壓縮後的資料儲存到目標檔案 lblMsg.Text += "檔案解壓縮完成:" + decompressedFile.Name; lblMsg.Text += Environment.NewLine; lblMsg.Text += "解壓縮後的大小:" + bytesDecompressed.ToString(); lblMsg.Text += Environment.NewLine; } } catch (Exception ex) { lblMsg.Text = ex.ToString(); } } }}
2、示範與 Windows 商店相關的操作
Feature/StoreDemo.xaml