2013-04-11 11:04:16 +00:00
|
|
|
|
using System.IO;
|
2012-07-23 21:28:23 +00:00
|
|
|
|
using SharpCompress.Compressor;
|
|
|
|
|
using SharpCompress.Compressor.BZip2;
|
|
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.GZBuilder.Data
|
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
|
internal static class SharpCompressHelper
|
|
|
|
|
{
|
2014-12-03 23:15:26 +00:00
|
|
|
|
internal static MemoryStream CompressStream(Stream stream)
|
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
|
byte[] arr = new byte[stream.Length];
|
|
|
|
|
stream.Read(arr, 0, (int)stream.Length);
|
2012-07-23 21:28:23 +00:00
|
|
|
|
|
2013-09-11 09:47:53 +00:00
|
|
|
|
MemoryStream ms = new MemoryStream();
|
2014-12-29 12:28:40 +00:00
|
|
|
|
BZip2Stream bzip = new BZip2Stream(ms, CompressionMode.Compress, true, false);
|
2012-07-23 21:28:23 +00:00
|
|
|
|
|
2013-09-11 09:47:53 +00:00
|
|
|
|
bzip.Write(arr, 0, arr.Length);
|
|
|
|
|
bzip.Close();
|
2012-07-23 21:28:23 +00:00
|
|
|
|
|
2013-09-11 09:47:53 +00:00
|
|
|
|
return ms;
|
|
|
|
|
}
|
2012-07-23 21:28:23 +00:00
|
|
|
|
|
2014-12-03 23:15:26 +00:00
|
|
|
|
internal static MemoryStream DecompressStream(Stream stream)
|
|
|
|
|
{
|
2015-09-25 13:20:53 +00:00
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
2014-12-29 12:28:40 +00:00
|
|
|
|
BZip2Stream bzip = new BZip2Stream(stream, CompressionMode.Decompress, false, false);
|
2012-07-23 21:28:23 +00:00
|
|
|
|
|
2013-09-11 09:47:53 +00:00
|
|
|
|
byte[] buffer = new byte[16 * 1024];
|
|
|
|
|
MemoryStream ms = new MemoryStream();
|
2012-07-23 21:28:23 +00:00
|
|
|
|
|
2013-09-11 09:47:53 +00:00
|
|
|
|
int read;
|
2015-12-28 15:01:53 +00:00
|
|
|
|
while((read = bzip.Read(buffer, 0, buffer.Length)) > 0)
|
2013-09-11 09:47:53 +00:00
|
|
|
|
ms.Write(buffer, 0, read);
|
2012-07-23 21:28:23 +00:00
|
|
|
|
|
2013-09-11 09:47:53 +00:00
|
|
|
|
return ms;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-23 21:28:23 +00:00
|
|
|
|
}
|