forked from philippelatulippe/ZLIB.NET
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added memory zlib helper and changed library namespace.
- Loading branch information
Showing
18 changed files
with
224 additions
and
19 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
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
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
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,118 @@ | ||
// Copyright (c) 2014-2018, Els_kom org. | ||
// https://github.com/Elskom/ | ||
// All rights reserved. | ||
// license: MIT, see LICENSE for more details. | ||
|
||
namespace Elskom.Generic.Libs | ||
{ | ||
using System.IO; | ||
|
||
/// <summary> | ||
/// Zlib Memory Compression and Decompression Helper Class. | ||
/// </summary> | ||
public static class MemoryZlib | ||
{ | ||
/// <summary> | ||
/// Compresses data using the default compression level. | ||
/// </summary> | ||
/// <param name="inData">The original input data.</param> | ||
/// <param name="outData">The compressed output data.</param> | ||
/// <param name="adler32">The output adler32 of the data.</param> | ||
/// <exception cref="NotPackableException">Thrown when the stream Errors in any way.</exception> | ||
public static void CompressData(byte[] inData, out byte[] outData, out int adler32) | ||
=> CompressData(inData, out outData, ZlibConst.ZDEFAULTCOMPRESSION, out adler32); | ||
|
||
/// <summary> | ||
/// Compresses data using the default compression level. | ||
/// </summary> | ||
/// <param name="inData">The original input data.</param> | ||
/// <param name="outData">The compressed output data.</param> | ||
/// <exception cref="NotPackableException">Thrown when the stream Errors in any way.</exception> | ||
public static void CompressData(byte[] inData, out byte[] outData) | ||
=> CompressData(inData, out outData, ZlibConst.ZDEFAULTCOMPRESSION); | ||
|
||
/// <summary> | ||
/// Compresses data using an specific compression level. | ||
/// </summary> | ||
/// <param name="inData">The original input data.</param> | ||
/// <param name="outData">The compressed output data.</param> | ||
/// <param name="level">The compression level to use.</param> | ||
/// <exception cref="NotPackableException">Thrown when the stream Errors in any way.</exception> | ||
// discard returned adler32. The caller does not want it. | ||
public static void CompressData(byte[] inData, out byte[] outData, int level) | ||
=> CompressData(inData, out outData, level, out var adler32); | ||
|
||
/// <summary> | ||
/// Compresses data using an specific compression level. | ||
/// </summary> | ||
/// <param name="inData">The original input data.</param> | ||
/// <param name="outData">The compressed output data.</param> | ||
/// <param name="level">The compression level to use.</param> | ||
/// <param name="adler32">The output adler32 of the data.</param> | ||
/// <exception cref="NotPackableException">Thrown when the stream Errors in any way.</exception> | ||
public static void CompressData(byte[] inData, out byte[] outData, int level, out int adler32) | ||
{ | ||
using (var outMemoryStream = new MemoryStream()) | ||
using (var outZStream = new ZOutputStream(outMemoryStream, level)) | ||
using (Stream inMemoryStream = new MemoryStream(inData)) | ||
{ | ||
try | ||
{ | ||
inMemoryStream.CopyTo(outZStream); | ||
} | ||
catch (ZStreamException) | ||
{ | ||
// the compression or decompression failed. | ||
} | ||
|
||
outZStream.Flush(); | ||
try | ||
{ | ||
outZStream.Finish(); | ||
} | ||
catch (ZStreamException ex) | ||
{ | ||
throw new NotPackableException("Compression Failed.", ex); | ||
} | ||
|
||
outData = outMemoryStream.ToArray(); | ||
adler32 = (int)(outZStream.Z.Adler & 0xffff); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Decompresses data. | ||
/// </summary> | ||
/// <param name="inData">The compressed input data.</param> | ||
/// <param name="outData">The decompressed output data.</param> | ||
/// <exception cref="NotUnpackableException">Thrown when the stream Errors in any way.</exception> | ||
public static void DecompressData(byte[] inData, out byte[] outData) | ||
{ | ||
using (var outMemoryStream = new MemoryStream()) | ||
using (var outZStream = new ZOutputStream(outMemoryStream)) | ||
using (Stream inMemoryStream = new MemoryStream(inData)) | ||
{ | ||
try | ||
{ | ||
inMemoryStream.CopyTo(outZStream); | ||
} | ||
catch (ZStreamException) | ||
{ | ||
// the compression or decompression failed. | ||
} | ||
|
||
outZStream.Flush(); | ||
try | ||
{ | ||
outZStream.Finish(); | ||
} | ||
catch (ZStreamException ex) | ||
{ | ||
throw new NotUnpackableException("Decompression Failed.", ex); | ||
} | ||
|
||
outData = outMemoryStream.ToArray(); | ||
} | ||
} | ||
} | ||
} |
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,44 @@ | ||
// Copyright (c) 2014-2018, Els_kom org. | ||
// https://github.com/Elskom/ | ||
// All rights reserved. | ||
// license: MIT, see LICENSE for more details. | ||
|
||
namespace Elskom.Generic.Libs | ||
{ | ||
using System; | ||
using System.IO; | ||
|
||
/// <summary> | ||
/// Zlib Memory Packing failure error. | ||
/// </summary> | ||
[Serializable] | ||
public sealed class NotPackableException : IOException | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="NotPackableException"/> class. | ||
/// </summary> | ||
public NotPackableException() | ||
: base() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="NotPackableException"/> class. | ||
/// </summary> | ||
/// <param name="s">The error string.</param> | ||
public NotPackableException(string s) | ||
: base(s) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="NotPackableException"/> class. | ||
/// </summary> | ||
/// <param name="s">The error string.</param> | ||
/// <param name="ex">The Exception that caused this Exception.</param> | ||
public NotPackableException(string s, Exception ex) | ||
: base(s, ex) | ||
{ | ||
} | ||
} | ||
} |
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 @@ | ||
// Copyright (c) 2014-2018, Els_kom org. | ||
// https://github.com/Elskom/ | ||
// All rights reserved. | ||
// license: MIT, see LICENSE for more details. | ||
|
||
namespace Elskom.Generic.Libs | ||
{ | ||
using System; | ||
using System.IO; | ||
|
||
/// <summary> | ||
/// Zlib Memory Unpacking failure error. | ||
/// </summary> | ||
[Serializable] | ||
public sealed class NotUnpackableException : IOException | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="NotUnpackableException"/> class with no argrument. | ||
/// </summary> | ||
public NotUnpackableException() | ||
: base() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="NotUnpackableException"/> class with an string argrument. | ||
/// </summary> | ||
/// <param name="s">The error string.</param> | ||
public NotUnpackableException(string s) | ||
: base(s) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="NotUnpackableException"/> class with an string argrument | ||
/// and the exception that cuased this exception. | ||
/// </summary> | ||
/// <param name="s">The error string.</param> | ||
/// <param name="ex">The Exception that caused this Exception.</param> | ||
public NotUnpackableException(string s, Exception ex) | ||
: base(s, ex) | ||
{ | ||
} | ||
} | ||
} |
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
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
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
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
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