我无法再在我的UWP应用程序中使用System.IO.Compression库,我需要它来压缩一个太大而未压缩的字符串以适应隔离存储。当我更新我的所有Nuget包时,我开始怀疑。我一直收到以下错误,代码如下,
编辑这似乎只在DEBUG模式下发生。当我在RELEASE模式下编译我的应用程序并部署到设备或本地计算机时,它适用于两者。但是当我尝试调试时,它会崩溃(崩溃发生在后来的代码中,尝试使用解压缩的字符串,因为压缩/解压缩无效地失败 - 在using语句中 - 并且我的应用程序中的其余代码被执行)
消息
无法正确加载基础压缩例程。
栈跟踪
在System.IO.Compression.Deflater.DeflateInit(CompressionLevel compressionLevel,Int32 windowBits,Int32 memLevel, CompressionStrategy策略) System.IO.Compression.Deflater..ctor(CompressionLevel compressionLevel,Int32 windowBits)at System.IO.Compression.DeflateStream.InitializeDeflater(Stream stream, Boolean leaveOpen,Int32 windowBits,CompressionLevel compressionLevel)at System.IO.Compression.DeflateStream..ctor(Stream stream, CompressionMode模式,Boolean leaveOpen,Int32 windowBits)at System.IO.Compression.DeflateStream..ctor(Stream stream, CompressionMode模式,Boolean leaveOpen)at myApp.Utilities.Extensions.Compress(String uncompressedString)at myApp.ExtendedSplash.d__17.MoveNext()
我认为这可能与此有关
Unzipping throws an "The underlying compression routine could not be loaded correctly"
我不知道该怎么办。是否有一个不依赖System.IO的替代Zip库,我可以在UWP中使用? - 我试图安装DotNetZip,但不会安装。
代码
public static class Extensions
{
/// <summary>
/// Compresses a string and returns a deflate compressed, Base64 encoded string.
/// </summary>
/// <param name="uncompressedString">String to compress</param>
public static string Compress(this string uncompressedString)
{
var compressedStream = new MemoryStream();
var uncompressedStream = new MemoryStream(Encoding.UTF8.GetBytes(uncompressedString));
using (var compressorStream = new DeflateStream(compressedStream, CompressionMode.Compress, true))
{
uncompressedStream.CopyTo(compressorStream);
}
return Convert.ToBase64String(compressedStream.ToArray());
}
/// <summary>
/// Decompresses a deflate compressed, Base64 encoded string and returns an uncompressed string.
/// </summary>
/// <param name="compressedString">String to decompress.</param>
public static string Decompress(this string compressedString)
{
var decompressedStream = new MemoryStream();
var compressedStream = new MemoryStream(Convert.FromBase64String(compressedString));
using (var decompressorStream = new DeflateStream(compressedStream, CompressionMode.Decompress))
{
decompressorStream.CopyTo(decompressedStream);
}
return Encoding.UTF8.GetString(decompressedStream.ToArray());
}
}