public static unsafe int Compress(byte[] data, int offset, int length, ref byte[] buffer, Lz4Mode mode) { int num; var num2 = LZ4_compressBound(length) + 8; if ((buffer == null) || (buffer.Length < num2)) { buffer = new byte[num2]; } fixed (byte* numRef = &(data[offset])) { fixed (byte* numRef2 = &(buffer[8])) { num = (mode == Lz4Mode.Fast) ? LZ4_compress(numRef, numRef2, length) : LZ4_compressHC(numRef, numRef2, length); var numPtr = (byte*)&length; buffer[0] = numPtr[0]; buffer[1] = numPtr[1]; buffer[2] = numPtr[2]; buffer[3] = numPtr[3]; numPtr = (byte*)# buffer[4] = numPtr[0]; buffer[5] = numPtr[1]; buffer[6] = numPtr[2]; buffer[7] = numPtr[3]; } } return (num + 8); }
public Lz4PackageEntry AddEntry(string name, string originFile, Lz4Mode mode = 0, int blockSize = 0x100000) { FileStream stream = new FileStream(originFile, FileMode.Open, FileAccess.Read, FileShare.Read, blockSize); FileInfo info = new FileInfo(originFile); Lz4PackageEntry entry = new Lz4PackageEntry { Filename = name, Package = this, EntryCreateTime = DateTime.Now.ToString("o"), OriginSize = info.Length, CreateTime = info.CreationTime.ToString("o"), LastModifyTime = info.LastWriteTime.ToString("o") }; AddEntry(stream, mode, blockSize, entry); if (_entries != null) { _entries.Add(entry); } if (_map != null) { _map[entry.Filename] = entry; } Save(entry); return entry; }
public Lz4CompressionStream(Stream targetStream, byte[] writeBuffer, byte[] compressionBuffer, Lz4Mode mode = 0, bool closeStream = false) { m_closeStream = closeStream; m_targetStream = targetStream; m_writeBuffer = writeBuffer; m_compressedBuffer = compressionBuffer; m_compressionMode = mode; }
protected Lz4CompressionStreamBase(Stream targetStream, byte[] writeBuffer, byte[] compressionBuffer, Lz4Mode mode, bool closeStream) { _closeStream = closeStream; _targetStream = targetStream; _writeBuffer = writeBuffer; _compressedBuffer = compressionBuffer; _compressionMode = mode; }
public static byte[] CompressBytes(byte[] data, int offset, int length, Lz4Mode mode) { if (length > data.Length) { throw new ArgumentOutOfRangeException("length"); } byte[] buffer = null; var count = Compress(data, 0, length, ref buffer, mode); var dst = new byte[count]; Buffer.BlockCopy(buffer, 0, dst, 0, count); return dst; }
public static byte[] CompressBytes(byte[] data, int offset, int length, Lz4Mode mode) { if (length > data.Length) { throw new ArgumentOutOfRangeException("length"); } byte[] buffer = null; int count = Compress(data, 0, length, ref buffer, mode); byte[] dst = new byte[count]; Buffer.BlockCopy(buffer, 0, dst, 0, count); return(dst); }
public static CompressResult Compress([NotNull] Stream inputFile, [NotNull] Stream compressFile = null, int blockSize = 0x100000, Lz4Mode mode = 0, Action <CompressResult> blockCallback = null) { long position; if (inputFile == null) { throw new ArgumentNullException(nameof(inputFile)); } if (compressFile == null) { throw new ArgumentNullException(nameof(compressFile)); } CompressResult result = new CompressResult(); byte[] buffer = new byte[blockSize]; try { position = compressFile.Position; } catch (Exception) { position = -1L; result.CompressedBytes = -1L; } using (Lz4CompressionStream stream = new Lz4CompressionStream(compressFile, 0x100000, mode)) { goto Label_00E9; Label_006D: int num2 = inputFile.Read(buffer, 0, buffer.Length); if (num2 == 0) { return(result); } result.Bytes += num2; stream.Write(buffer, 0, num2); if (position != -1L) { long num3 = compressFile.Position; result.CompressedBytes += num3 - position; position = num3; } if (blockCallback != null) { blockCallback(result); } Label_00E9: goto Label_006D; } }
public ExtraCompressStream([NotNull] Stream targetStream, int bufferSize = 0x100000, Lz4Mode compressionMode = 0, bool closeStream = false) : base( targetStream, new byte[bufferSize], new byte[Lz4.LZ4_compressBound(bufferSize)], compressionMode, closeStream) { if (targetStream == null) { throw new ArgumentNullException(nameof(targetStream)); } if (bufferSize <= 0) { throw new ArgumentOutOfRangeException(nameof(bufferSize)); } }
public static CompressResult Compress([NotNull] Stream inputFile, [NotNull] Stream compressFile = null, int blockSize = 0x100000, Lz4Mode mode = 0, Action<CompressResult> blockCallback = null) { long position; if (inputFile == null) { throw new ArgumentNullException("inputFile"); } if (compressFile == null) { throw new ArgumentNullException("compressFile"); } CompressResult result = new CompressResult(); byte[] buffer = new byte[blockSize]; try { position = compressFile.Position; } catch (Exception) { position = -1L; result.CompressedBytes = -1L; } using (Lz4CompressionStream stream = new Lz4CompressionStream(compressFile, 0x100000, mode)) { goto Label_00E9; Label_006D: int num2 = inputFile.Read(buffer, 0, buffer.Length); if (num2 == 0) { return result; } result.Bytes += num2; stream.Write(buffer, 0, num2); if (position != -1L) { long num3 = compressFile.Position; result.CompressedBytes += num3 - position; position = num3; } if (blockCallback != null) { blockCallback(result); } Label_00E9: goto Label_006D; } }
public LzEntryOutputStream(Stream stream, Lz4PackageEntry entry, int bufferSize = 0x100000, Lz4Mode mode = 0) { _baseStream = stream; _lz = new ExtraCompressStream(stream, bufferSize, mode); try { _pos = stream.Position; } catch (Exception) { _pos = -1L; } _entry = entry; _entry.CompressedSize = 0L; _entry.OriginSize = 0L; _entry.Entry = stream.Position; }
public Lz4CompressStream([NotNull] Stream targetStream, int bufferSize = 0x100000, Lz4Mode compressionMode = 0, bool closeStream = false) { if (targetStream == null) { throw new ArgumentNullException("targetStream"); } if (bufferSize <= 0) { throw new ArgumentOutOfRangeException("bufferSize"); } _targetStream = targetStream; _compressionMode = compressionMode; _closeStream = closeStream; _writeBuffer = new byte[bufferSize]; _compressedBuffer = new byte[Lz4.LZ4_compressBound(bufferSize)]; }
private void AddEntry(Stream stream, Lz4Mode mode, int blockSize, Lz4PackageEntry entry) { using (FileStream stream2 = OpenWrite(blockSize)) { entry.Entry = stream2.Position; using (Lz4CompressionStream stream3 = new Lz4CompressionStream(stream2, blockSize, mode)) { int num; byte[] buffer = new byte[blockSize]; while ((num = stream.Read(buffer, 0, buffer.Length)) > 0) { entry.OriginSize += num; stream3.Write(buffer, 0, num); } } entry.CompressedSize = stream2.Position - entry.Entry; } }
public static CompressResult Compress([NotNull] string inputFile, [CanBeNull] string compressFile = null, int blockSize = 0x100000, Lz4Mode mode = 0, Action<CompressResult> blockCallback = null) { CompressResult result; if (inputFile == null) { throw new ArgumentNullException("inputFile"); } compressFile = compressFile ?? GetCompressFilename(inputFile); using (Stream stream = OpenUncompressedStream(inputFile)) { using (FileStream stream2 = new FileStream(compressFile, FileMode.Create, FileAccess.Write, FileShare.None, 0x100000)) { stream2.SetLength(0L); result = Compress(stream, stream2, blockSize, mode, blockCallback); } } return result; }
public Stream AddEntry(string name, DateTime createTime, DateTime lastModify, Lz4Mode mode, int blockSize) { Lz4PackageEntry item = new Lz4PackageEntry { Filename = name, Package = this, EntryCreateTime = DateTime.Now.ToString("o"), CreateTime = createTime.ToString("o"), LastModifyTime = lastModify.ToString("o") }; if (_entries != null) { _entries.Add(item); } if (_map != null) { _map[item.Filename] = item; } FileStream stream = OpenWrite(blockSize); item.Entry = stream.Position; return new LzEntryOutputStream(stream, item, blockSize, mode); }
public Lz4PackageEntry AddEntry(string name, Stream stream, DateTime createTime, DateTime lastModify, Lz4Mode mode = 0, int blockSize = 0x100000) { Lz4PackageEntry entry = new Lz4PackageEntry { Filename = name, Package = this, EntryCreateTime = DateTime.Now.ToString("o"), CreateTime = createTime.ToString("o"), LastModifyTime = lastModify.ToString("o") }; AddEntry(stream, mode, blockSize, entry); if (_entries != null) { _entries.Add(entry); } if (_map != null) { _map[entry.Filename] = entry; } Save(entry); return(entry); }
public static Lz4CompressionStream OpenWrite(string filename, int blockSize = 0x100000, Lz4Mode mode = 0) { return new Lz4CompressionStream(new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None, blockSize), blockSize, mode, true); }
public Lz4CompressionStream(Stream targetStream, Lz4Mode mode = 0, bool closeStream = false) : this(targetStream, 0x40000, mode, closeStream) { }
public Lz4CompressionStream(Stream targetStream, int bufferSize, Lz4Mode mode = 0, bool closeStream = false) : this(targetStream, new byte[bufferSize], new byte[Lz4.LZ4_compressBound(bufferSize)], mode, closeStream) { }
/// <summary> /// Compresses the byte buffer. /// This method stores a 8-byte header to store the original and compressed buffer size. /// </summary> /// <param name="data">The data to be compressed.</param> /// <param name="mode">The compression mode [Fast, HighCompression].</param> /// <returns>The compressed byte array</returns> public static byte[] CompressBytes(byte[] data, Lz4Mode mode = Lz4Mode.Fast) { return(CompressBytes(data, 0, data.Length, mode)); }
/// <summary> /// Compresses the byte buffer. /// This method stores a 8-byte header to store the original and compressed buffer size. /// </summary> /// <param name="data">The data to be compressed.</param> /// <param name="offset">The offset.</param> /// <param name="length">The length.</param> /// <param name="mode">The compression mode [Fast, HighCompression].</param> /// <returns>The compressed byte array</returns> /// <exception cref="ArgumentOutOfRangeException">If length if outside data array bounds</exception> public static byte[] CompressBytes (byte[] data, Int32 offset, Int32 length, Lz4Mode mode) { byte[] buffer = null; int sz = Compress (data, offset, length, ref buffer, mode); // adjust final array size byte[] finalBuffer = new byte[sz]; System.Buffer.BlockCopy (buffer, 0, finalBuffer, 0, sz); return finalBuffer; }
public Stream AddEntry(string name, Lz4Mode mode, int blockSize) { return AddEntry(name, DateTime.Now, DateTime.Now, mode, blockSize); }
public Lz4PackageEntry AddEntry(string name, Stream stream, DateTime createTime, DateTime lastModify, Lz4Mode mode = 0, int blockSize = 0x100000) { Lz4PackageEntry entry = new Lz4PackageEntry { Filename = name, Package = this, EntryCreateTime = DateTime.Now.ToString("o"), CreateTime = createTime.ToString("o"), LastModifyTime = lastModify.ToString("o") }; AddEntry(stream, mode, blockSize, entry); if (_entries != null) { _entries.Add(entry); } if (_map != null) { _map[entry.Filename] = entry; } Save(entry); return entry; }
public static string CompressString(string text, Lz4Mode mode = 0) { byte[] bytes = Encoding.UTF8.GetBytes(text); return(Convert.ToBase64String(CompressBytes(bytes, 0, bytes.Length, mode))); }
public static unsafe int Compress(byte[] data, int offset, int length, ref byte[] buffer, Lz4Mode mode) { int num; int num2 = LZ4_compressBound(length) + 8; if ((buffer == null) || (buffer.Length < num2)) { buffer = new byte[num2]; } fixed(byte *numRef = &(data[offset])) { fixed(byte *numRef2 = &(buffer[8])) { num = (mode == Lz4Mode.Fast) ? LZ4_compress(numRef, numRef2, length) : LZ4_compressHC(numRef, numRef2, length); byte *numPtr = (byte *)&length; buffer[0] = numPtr[0]; buffer[1] = numPtr[1]; buffer[2] = numPtr[2]; buffer[3] = numPtr[3]; numPtr = (byte *)# buffer[4] = numPtr[0]; buffer[5] = numPtr[1]; buffer[6] = numPtr[2]; buffer[7] = numPtr[3]; } } return(num + 8); }
public Stream AddEntry(string name, Lz4Mode mode, int blockSize) { return(AddEntry(name, DateTime.Now, DateTime.Now, mode, blockSize)); }
public Lz4PackageEntry AddEntry(string name, Stream stream, Lz4Mode mode = 0, int blockSize = 0x100000) { return(AddEntry(name, stream, DateTime.Now, DateTime.Now, mode, blockSize)); }
/// <summary> /// Compresses the byte buffer. /// This method stores a 8-byte header to store the original and compressed buffer size. /// </summary> /// <param name="data">The data to be compressed.</param> /// <param name="offset">The offset.</param> /// <param name="length">The length.</param> /// <param name="buffer">The compression buffer. If the buffer is null or the size is insuficient, a new array will be created.</param> /// <param name="mode">The compression mode [Fast, HighCompression].</param> /// <returns>The compressed byte array</returns> public static int Compress (byte[] data, Int32 offset, Int32 length, ref byte[] buffer, Lz4Mode mode) { // sanity checks if (data == null) throw new ArgumentNullException ("data"); if ((length + offset) > data.Length) throw new ArgumentOutOfRangeException ("length"); byte* ptr; Int32 compressedSize = 0; // check decompression buffer size int minLen = LZ4_compressBound (length) + 8; if (buffer == null || buffer.Length < minLen) buffer = new byte[minLen]; // get buffers pointers fixed (byte* pData = &data[offset], pBuffer = &buffer[8]) { // compress data if (length > 0) { compressedSize = (mode == Lz4Mode.Fast) ? LZ4_compress (pData, pBuffer, length) : LZ4_compressHC (pData, pBuffer, length); } // store original size and compressed size for latter decompression ptr = (byte*)&(length); buffer[0] = ptr[0]; buffer[1] = ptr[1]; buffer[2] = ptr[2]; buffer[3] = ptr[3]; // compressed size ptr = (byte*)&(compressedSize); buffer[4] = ptr[0]; buffer[5] = ptr[1]; buffer[6] = ptr[2]; buffer[7] = ptr[3]; } // return data length return compressedSize + 8; }
/// <summary> /// Initializes a new instance of the <see cref="ZlibCompressionStream" /> class. /// </summary> /// <param name="targetStream">The target.</param> /// <param name="closeStream">The close stream.</param> public Lz4CompressionStream (Stream targetStream, Lz4Mode mode = Lz4Mode.Fast, bool closeStream = false) : this (targetStream, 1 << 18, mode, closeStream) // 256 kb { }
public Lz4CompressionStream(Stream targetStream, byte[] writeBuffer, byte[] compressionBuffer, Lz4Mode mode = 0, bool closeStream = false) : base(targetStream, writeBuffer, compressionBuffer, mode, closeStream) { }
/// <summary> /// Initializes a new instance of the <see cref="ZlibCompressionStream" /> class. /// </summary> /// <param name="targetStream">The target stream.</param> /// <param name="writeBuffer">The write buffer.</param> /// <param name="compressionBuffer">The compression buffer.</param> /// <param name="closeStream">The close stream.</param> public Lz4CompressionStream(Stream targetStream, byte[] writeBuffer, byte[] compressionBuffer, Lz4Mode mode = Lz4Mode.Fast, bool closeStream = false) { m_closeStream = closeStream; m_targetStream = targetStream; m_writeBuffer = writeBuffer; m_compressedBuffer = compressionBuffer; m_compressionMode = mode; }
/// <summary> /// Compresses the byte buffer. /// This method stores a 8-byte header to store the original and compressed buffer size. /// </summary> /// <param name="data">The data to be compressed.</param> /// <param name="mode">The compression mode [Fast, HighCompression].</param> /// <returns>The compressed byte array</returns> public static byte[] CompressBytes (byte[] data, Lz4Mode mode = Lz4Mode.Fast) { return CompressBytes (data, 0, data.Length, mode); }
/// <summary> /// Initializes a new instance of the <see cref="ZlibCompressionStream" /> class. /// </summary> /// <param name="targetStream">The target.</param> /// <param name="closeStream">The close stream.</param> public Lz4CompressionStream(Stream targetStream, Lz4Mode mode = Lz4Mode.Fast, bool closeStream = false) : this(targetStream, 1 << 18, mode, closeStream) // 256 kb { }
/// <summary> /// Compresses the byte buffer. /// This method stores a 8-byte header to store the original and compressed buffer size. /// </summary> /// <param name="data">The data to be compressed.</param> /// <param name="offset">The offset.</param> /// <param name="length">The length.</param> /// <param name="buffer">The compression buffer. If the buffer is null or the size is insuficient, a new array will be created.</param> /// <param name="mode">The compression mode [Fast, HighCompression].</param> /// <returns>The compressed byte array</returns> public static int Compress(byte[] data, Int32 offset, Int32 length, ref byte[] buffer, Lz4Mode mode) { // sanity checks if (data == null) { throw new ArgumentNullException("data"); } if ((length + offset) > data.Length) { throw new ArgumentOutOfRangeException("length"); } byte *ptr; Int32 compressedSize = 0; // check decompression buffer size int minLen = LZ4_compressBound(length) + 8; if (buffer == null || buffer.Length < minLen) { buffer = new byte[minLen]; // get buffers pointers fixed(byte *pData = &data[offset], pBuffer = &buffer[8]) { // compress data if (length > 0) { compressedSize = (mode == Lz4Mode.Fast) ? LZ4_compress(pData, pBuffer, length) : LZ4_compressHC(pData, pBuffer, length); } // store original size and compressed size for latter decompression ptr = (byte *)&(length); buffer[0] = ptr[0]; buffer[1] = ptr[1]; buffer[2] = ptr[2]; buffer[3] = ptr[3]; // compressed size ptr = (byte *)&(compressedSize); buffer[4] = ptr[0]; buffer[5] = ptr[1]; buffer[6] = ptr[2]; buffer[7] = ptr[3]; } // return data length return(compressedSize + 8); }
public static Lz4CompressionStream OpenWrite(string filename, int blockSize = 0x100000, Lz4Mode mode = 0) { return(new Lz4CompressionStream(new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None, blockSize), blockSize, mode, true)); }
public static CompressResult Compress([NotNull] string inputFile, [CanBeNull] string compressFile = null, int blockSize = 0x100000, Lz4Mode mode = 0, Action <CompressResult> blockCallback = null) { CompressResult result; if (inputFile == null) { throw new ArgumentNullException(nameof(inputFile)); } compressFile = compressFile ?? GetCompressFilename(inputFile); using (Stream stream = OpenUncompressedStream(inputFile)) { using (FileStream stream2 = new FileStream(compressFile, FileMode.Create, FileAccess.Write, FileShare.None, 0x100000)) { stream2.SetLength(0L); result = Compress(stream, stream2, blockSize, mode, blockCallback); } } return(result); }
public static string CompressString(string text, Lz4Mode mode = 0) { byte[] bytes = Encoding.UTF8.GetBytes(text); return Convert.ToBase64String(CompressBytes(bytes, 0, bytes.Length, mode)); }
/// <summary> /// Compresses the specified text and return a Base64 enconded string. /// </summary> /// <param name="text">The text.</param> /// <param name="mode">The compression mode [Fast, HighCompression].</param> /// <returns>The compressed text as a Base64 enconded string</returns> public static string CompressString (string text, Lz4Mode mode = Lz4Mode.Fast) { // get string as bytes byte[] buffer = Encoding.UTF8.GetBytes (text); // compress byte[] compressed = CompressBytes (buffer, 0, buffer.Length, mode); // convert to base 64 to allow general use of the string return Convert.ToBase64String (compressed); }
public Lz4PackageEntry AddEntry(string name, Stream stream, Lz4Mode mode = 0, int blockSize = 0x100000) { return AddEntry(name, stream, DateTime.Now, DateTime.Now, mode, blockSize); }
public Stream AddEntry(string name, DateTime createTime, DateTime lastModify, Lz4Mode mode, int blockSize) { Lz4PackageEntry item = new Lz4PackageEntry { Filename = name, Package = this, EntryCreateTime = DateTime.Now.ToString("o"), CreateTime = createTime.ToString("o"), LastModifyTime = lastModify.ToString("o") }; if (_entries != null) { _entries.Add(item); } if (_map != null) { _map[item.Filename] = item; } FileStream stream = OpenWrite(blockSize); item.Entry = stream.Position; return(new LzEntryOutputStream(stream, item, blockSize, mode)); }