mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-20 21:33:10 -07:00
DeflateUtils
This commit is contained in:
parent
f23786ce5e
commit
e36faaa0b5
4 changed files with 52 additions and 19 deletions
|
@ -165,6 +165,7 @@
|
||||||
<Compile Include="Converters\RadixToStringConverter.cs" />
|
<Compile Include="Converters\RadixToStringConverter.cs" />
|
||||||
<Compile Include="Converters\VisibilityNegationConverter.cs" />
|
<Compile Include="Converters\VisibilityNegationConverter.cs" />
|
||||||
<Compile Include="Selectors\NavViewMenuItemTemplateSelector.cs" />
|
<Compile Include="Selectors\NavViewMenuItemTemplateSelector.cs" />
|
||||||
|
<Compile Include="Utils\DeflateUtils.cs" />
|
||||||
<Compile Include="Utils\ResourceVirtualKey.cs" />
|
<Compile Include="Utils\ResourceVirtualKey.cs" />
|
||||||
<Compile Include="Utils\ResourceString.cs" />
|
<Compile Include="Utils\ResourceString.cs" />
|
||||||
<Compile Include="Utils\JsonUtils.cs" />
|
<Compile Include="Utils\JsonUtils.cs" />
|
||||||
|
|
|
@ -15,7 +15,11 @@ namespace CalculatorApp
|
||||||
{
|
{
|
||||||
result = null;
|
result = null;
|
||||||
var protoArgs = args as IProtocolActivatedEventArgs;
|
var protoArgs = args as IProtocolActivatedEventArgs;
|
||||||
if (protoArgs == null || protoArgs.Uri == null)
|
if (protoArgs == null ||
|
||||||
|
protoArgs.Uri == null ||
|
||||||
|
protoArgs.Uri.Segments == null ||
|
||||||
|
protoArgs.Uri.Segments.Length < 2 ||
|
||||||
|
protoArgs.Uri.Segments[0] != "snapshot/")
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
44
src/Calculator/Utils/DeflateUtils.cs
Normal file
44
src/Calculator/Utils/DeflateUtils.cs
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.IO.Compression;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace CalculatorApp
|
||||||
|
{
|
||||||
|
internal static class DeflateUtils
|
||||||
|
{
|
||||||
|
public static byte[] Compress(string text)
|
||||||
|
{
|
||||||
|
var data = Encoding.UTF8.GetBytes(text);
|
||||||
|
using (var compressed = new MemoryStream())
|
||||||
|
{
|
||||||
|
using (var deflater = new DeflateStream(compressed, CompressionLevel.Optimal))
|
||||||
|
{
|
||||||
|
deflater.Write(data, 0, data.Length);
|
||||||
|
}
|
||||||
|
return compressed.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool TryDecompress(byte[] data, out string text)
|
||||||
|
{
|
||||||
|
text = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var srcStream = new MemoryStream(data))
|
||||||
|
using (var deflateStream = new DeflateStream(srcStream, CompressionMode.Decompress))
|
||||||
|
using (var resultStream = new MemoryStream())
|
||||||
|
{
|
||||||
|
deflateStream.CopyTo(resultStream);
|
||||||
|
byte[] decompressed = resultStream.ToArray();
|
||||||
|
text = Encoding.UTF8.GetString(decompressed);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using Windows.ApplicationModel.UserActivities;
|
using Windows.ApplicationModel.UserActivities;
|
||||||
|
@ -23,10 +24,6 @@ using CalculatorApp.ViewModel.Common;
|
||||||
using CalculatorApp.ViewModel.Common.Automation;
|
using CalculatorApp.ViewModel.Common.Automation;
|
||||||
|
|
||||||
using wuxc = Windows.UI.Xaml.Controls;
|
using wuxc = Windows.UI.Xaml.Controls;
|
||||||
using System.Text.Json;
|
|
||||||
using System.IO.Compression;
|
|
||||||
using System.IO;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace CalculatorApp
|
namespace CalculatorApp
|
||||||
{
|
{
|
||||||
|
@ -78,7 +75,7 @@ namespace CalculatorApp
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var json = JsonSerializer.Serialize(new ApplicationSnapshotAlias { Value = Model.Snapshot });
|
var json = JsonSerializer.Serialize(new ApplicationSnapshotAlias { Value = Model.Snapshot });
|
||||||
embeddedData = Convert.ToBase64String(Compress(json));
|
embeddedData = Convert.ToBase64String(DeflateUtils.Compress(json));
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
|
@ -720,19 +717,6 @@ namespace CalculatorApp
|
||||||
await dialog.ShowAsync();
|
await dialog.ShowAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] Compress(string text)
|
|
||||||
{
|
|
||||||
var data = Encoding.UTF8.GetBytes(text);
|
|
||||||
using (var compressed = new MemoryStream())
|
|
||||||
{
|
|
||||||
using (var deflater = new DeflateStream(compressed, CompressionLevel.Optimal))
|
|
||||||
{
|
|
||||||
deflater.Write(data, 0, data.Length);
|
|
||||||
}
|
|
||||||
return compressed.ToArray();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Calculator m_calculator;
|
private Calculator m_calculator;
|
||||||
private GraphingCalculator m_graphingCalculator;
|
private GraphingCalculator m_graphingCalculator;
|
||||||
private UnitConverter m_converter;
|
private UnitConverter m_converter;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue