mirror of
https://github.com/riegera2412/UVtools.git
synced 2026-07-08 17:42:31 +02:00
57f077f444
- **File formats:** - (Add) OSF (Vlare Open File Format) - (Fix) CTB Encrypted: Bottom Retract Height for TSMC was constraining incorrectly with the normal total retract height - (Fix) CWS: Only issue `;<Slice>` command when the exposure is about to happen (#514) - **GCode:** - (Add) Command `CommandWaitSyncDelay` for movement wait sync delay instead of depending on G4 wait command - (Fix) Wrong parsing and set of wait times when using a wait after lift / wait before retract - (Improvement) Auto update: Make sure the download url exists before attempt the download, if not, instead it will prompt for manual download and update - (Improvement) MacOS: Remove `com.apple.quarantine` flag from the auto downloaded files - (Upgrade) .NET from 6.0.7 to 6.0.8 - (Upgrade) AvaloniaUI from 0.10.17 to 0.10.18
131 lines
5.1 KiB
C#
131 lines
5.1 KiB
C#
/*
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE
|
|
* Version 3, 19 November 2007
|
|
* Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
* Everyone is permitted to copy and distribute verbatim copies
|
|
* of this license document, but changing it is not allowed.
|
|
*/
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Runtime.CompilerServices;
|
|
using CommunityToolkit.HighPerformance;
|
|
|
|
namespace UVtools.Core.Extensions;
|
|
|
|
public static class CompressionExtensions
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static int GetGzipUncompressedLength(ReadOnlyMemory<byte> compressedData)
|
|
{
|
|
return BitConverter.ToInt32(compressedData.Slice(compressedData.Length - 4, 4).Span);
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static int GetGzipUncompressedLength(Stream stream)
|
|
{
|
|
Span<byte> uncompressedLength = stackalloc byte[4];
|
|
stream.Position = stream.Length - 4;
|
|
stream.Read(uncompressedLength);
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
return BitConverter.ToInt32(uncompressedLength);
|
|
}
|
|
|
|
public static MemoryStream GZipCompress(Stream inputStream, CompressionLevel compressionLevel, bool leaveStreamOpen = false)
|
|
{
|
|
if (inputStream.Position == inputStream.Length) { inputStream.Seek(0, SeekOrigin.Begin); }
|
|
|
|
var compressedStream = new MemoryStream();
|
|
using (var gzipStream = new GZipStream(compressedStream, compressionLevel, true))
|
|
{
|
|
inputStream.CopyTo(gzipStream);
|
|
}
|
|
if (!leaveStreamOpen) { inputStream.Close(); }
|
|
|
|
compressedStream.Seek(0, SeekOrigin.Begin);
|
|
return compressedStream;
|
|
}
|
|
|
|
public static MemoryStream GZipCompress(byte[] inputStream, CompressionLevel compressionLevel) =>
|
|
GZipCompress(new ReadOnlyMemory<byte>(inputStream).AsStream(), compressionLevel);
|
|
|
|
public static byte[] GZipCompressToBytes(byte[] inputStream, CompressionLevel compressionLevel)
|
|
{
|
|
using var ms = GZipCompress(new ReadOnlyMemory<byte>(inputStream).AsStream(), compressionLevel);
|
|
return ms.ToArray();
|
|
}
|
|
|
|
|
|
public static MemoryStream GZipDecompress(Stream compressedStream, bool leaveStreamOpen = false)
|
|
{
|
|
if (compressedStream.Position == compressedStream.Length) { compressedStream.Seek(0, SeekOrigin.Begin); }
|
|
|
|
var uncompressedStream = new MemoryStream(GetGzipUncompressedLength(compressedStream));
|
|
using var gzipStream = new GZipStream(compressedStream, CompressionMode.Decompress, leaveStreamOpen);
|
|
gzipStream.CopyTo(uncompressedStream);
|
|
|
|
return uncompressedStream;
|
|
}
|
|
|
|
public static ArraySegment<byte> GZipDecompress(ReadOnlyMemory<byte> compressedData)
|
|
{
|
|
using var uncompressedStream = new MemoryStream(GetGzipUncompressedLength(compressedData));
|
|
using (var gzipStream = new GZipStream(compressedData.AsStream(), CompressionMode.Decompress, false))
|
|
{
|
|
gzipStream.CopyTo(uncompressedStream);
|
|
}
|
|
|
|
return uncompressedStream.TryGetBuffer(out var buffer)
|
|
? buffer
|
|
: uncompressedStream.ToArray();
|
|
}
|
|
|
|
public static MemoryStream DeflateCompress(Stream inputStream, CompressionLevel compressionLevel, bool leaveStreamOpen = false)
|
|
{
|
|
if (inputStream.Position == inputStream.Length) { inputStream.Seek(0, SeekOrigin.Begin); }
|
|
|
|
var compressedStream = new MemoryStream();
|
|
using (var gzipStream = new DeflateStream(compressedStream, compressionLevel, true))
|
|
{
|
|
inputStream.CopyTo(gzipStream);
|
|
}
|
|
if (!leaveStreamOpen) { inputStream.Close(); }
|
|
|
|
compressedStream.Seek(0, SeekOrigin.Begin);
|
|
return compressedStream;
|
|
}
|
|
|
|
public static MemoryStream DeflateCompress(byte[] inputStream, CompressionLevel compressionLevel) =>
|
|
DeflateCompress(new ReadOnlyMemory<byte>(inputStream).AsStream(), compressionLevel);
|
|
|
|
public static byte[] DeflateCompressToBytes(byte[] inputStream, CompressionLevel compressionLevel)
|
|
{
|
|
using var ms = DeflateCompress(new ReadOnlyMemory<byte>(inputStream).AsStream(), compressionLevel);
|
|
return ms.ToArray();
|
|
}
|
|
|
|
public static MemoryStream DeflateDecompress(Stream compressedStream, bool leaveStreamOpen = false)
|
|
{
|
|
if (compressedStream.Position == compressedStream.Length) { compressedStream.Seek(0, SeekOrigin.Begin); }
|
|
|
|
var uncompressedStream = new MemoryStream();
|
|
using var gzipStream = new DeflateStream(compressedStream, CompressionMode.Decompress, leaveStreamOpen);
|
|
gzipStream.CopyTo(uncompressedStream);
|
|
|
|
return uncompressedStream;
|
|
}
|
|
|
|
public static ArraySegment<byte> DeflateDecompress(ReadOnlyMemory<byte> compressedData)
|
|
{
|
|
using var uncompressedStream = new MemoryStream();
|
|
using (var gzipStream = new DeflateStream(compressedData.AsStream(), CompressionMode.Decompress, false))
|
|
{
|
|
gzipStream.CopyTo(uncompressedStream);
|
|
}
|
|
|
|
return uncompressedStream.TryGetBuffer(out var buffer)
|
|
? buffer
|
|
: uncompressedStream.ToArray();
|
|
}
|
|
} |