-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from PandaTechAM/development
gzip
- Loading branch information
Showing
5 changed files
with
195 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System.Text; | ||
|
||
namespace Pandatech.Crypto.Tests; | ||
|
||
public class GZipTests | ||
{ | ||
[Fact] | ||
public void Compress_String_ReturnsCompressedData() | ||
{ | ||
// Arrange | ||
const string input = "Hello, world!"; | ||
var expected = GZip.Compress(Encoding.UTF8.GetBytes(input)); | ||
|
||
// Act | ||
var result = GZip.Compress(input); | ||
|
||
// Assert | ||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact] | ||
public void Decompress_Base64_ReturnsOriginalData() | ||
{ | ||
// Arrange | ||
const string input = "Hello, world!"; | ||
var compressed = GZip.Compress(input); | ||
var compressedBase64 = Convert.ToBase64String(compressed); | ||
|
||
// Act | ||
var result = GZip.Decompress(compressedBase64); | ||
var resultString = Encoding.UTF8.GetString(result); | ||
|
||
// Assert | ||
Assert.Equal(input, resultString); | ||
} | ||
|
||
[Fact] | ||
public void Compress_And_Decompress_Byte_Array_ReturnsOriginalData() | ||
{ | ||
// Arrange | ||
var input = Encoding.UTF8.GetBytes("Hello, world!"); | ||
|
||
// Act | ||
var compressed = GZip.Compress(input); | ||
var decompressed = GZip.Decompress(compressed); | ||
|
||
// Assert | ||
Assert.Equal(input, decompressed); | ||
} | ||
|
||
[Fact] | ||
public void Compress_And_Decompress_Stream_ReturnsOriginalData() | ||
{ | ||
// Arrange | ||
var input = "Hello, world!"u8.ToArray(); | ||
using var inputStream = new MemoryStream(input); | ||
using var compressedStream = new MemoryStream(); | ||
using var decompressedStream = new MemoryStream(); | ||
|
||
// Act | ||
GZip.Compress(inputStream, compressedStream); | ||
compressedStream.Seek(0, SeekOrigin.Begin); | ||
GZip.Decompress(compressedStream, decompressedStream); | ||
var result = decompressedStream.ToArray(); | ||
|
||
// Assert | ||
Assert.Equal(input, result); | ||
} | ||
|
||
[Theory] | ||
[InlineData("")] | ||
[InlineData("Short string")] | ||
[InlineData("The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.")] | ||
public void Compress_Decompress_String_VariousLengths(string input) | ||
{ | ||
// Act | ||
var compressed = GZip.Compress(input); | ||
var decompressed = GZip.Decompress(Convert.ToBase64String(compressed)); | ||
var resultString = Encoding.UTF8.GetString(decompressed); | ||
|
||
// Assert | ||
Assert.Equal(input, resultString); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System.IO.Compression; | ||
using System.Text; | ||
|
||
namespace Pandatech.Crypto; | ||
|
||
public static class GZip | ||
{ | ||
|
||
public static byte[] Compress(string data) | ||
{ | ||
return Compress(Encoding.UTF8.GetBytes(data)); | ||
} | ||
|
||
public static byte[] Compress(byte[] data) | ||
{ | ||
using var compressedStream = new MemoryStream(); | ||
Compress(new MemoryStream(data), compressedStream); | ||
return compressedStream.ToArray(); | ||
} | ||
|
||
public static void Compress(Stream sourceStream, Stream destinationStream) | ||
{ | ||
using var zipStream = new GZipStream(destinationStream, CompressionMode.Compress, leaveOpen: true); | ||
sourceStream.CopyTo(zipStream); | ||
} | ||
|
||
public static byte[] Decompress(string compressedBase64) | ||
{ | ||
var compressedData = Convert.FromBase64String(compressedBase64); | ||
return Decompress(compressedData); | ||
} | ||
|
||
public static byte[] Decompress(byte[] data) | ||
{ | ||
using var decompressedStream = new MemoryStream(); | ||
Decompress(new MemoryStream(data), decompressedStream); | ||
return decompressedStream.ToArray(); | ||
} | ||
|
||
public static void Decompress(Stream sourceStream, Stream destinationStream) | ||
{ | ||
using var zipStream = new GZipStream(sourceStream, CompressionMode.Decompress, leaveOpen: true); | ||
zipStream.CopyTo(destinationStream); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters