Пример #1
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var path      = Path.GetDirectoryName(filePath);
            var name      = Path.GetFileNameWithoutExtension(filePath);
            var extension = Path.GetExtension(filePath);

            var inputBytes = File.ReadAllBytes(filePath);

            var compressedRaw = new byte[LZ4Codec.MaximumOutputSize(inputBytes.Length)];

            int compressedSize = LZ4Codec.Encode(inputBytes, 0, inputBytes.Length, compressedRaw, 0, compressedRaw.Length);

            if (compressedSize <= 0)
            {
                // Debug.LogError($"Compression failure on {filename}, lz4 returned {compressedSize}");
                return;
            }

            var exportPath = $"{filePath}.LZ4";

            Debug.Write(exportPath);
            using (var fs = new FileStream(exportPath, FileMode.Create, FileAccess.Write))
            {
                byte[] intBytes = BitConverter.GetBytes(inputBytes.Length);

                fs.Write(intBytes, 0, intBytes.Length);
                fs.Write(compressedRaw, 0, compressedSize);
            }
        }
Пример #2
0
        /// <summary>Flushes current chunk.</summary>
        private void FlushCurrentChunk()
        {
            if (_bufferOffset <= 0)
            {
                return;
            }

            var compressed       = new byte[_bufferOffset];
            var compressedLength = _highCompression
                                ? LZ4Codec.EncodeHC(_buffer, 0, _bufferOffset, compressed, 0, _bufferOffset)
                                : LZ4Codec.Encode(_buffer, 0, _bufferOffset, compressed, 0, _bufferOffset);

            if (compressedLength <= 0 || compressedLength >= _bufferOffset)
            {
                // incompressible block
                compressed       = _buffer;
                compressedLength = _bufferOffset;
            }

            var isCompressed = compressedLength < _bufferOffset;

            if (fileHeaderInfo == null)
            {
                fileHeaderInfo = new LZ4FileHeaderInfo();
                fileHeaderInfo.FrameDescriptor_BD_BlockMaxSize     = BlockMaximumSize.Block64K;
                fileHeaderInfo.FrameDescriptor_FLG_BChecksum       = false;
                fileHeaderInfo.FrameDescriptor_FLG_BIndependence   = true;
                fileHeaderInfo.FrameDescriptor_FLG_ContentChecksum = true;
                fileHeaderInfo.FrameDescriptor_FLG_ContentSize     = false;
                fileHeaderInfo.FrameDescriptor_FLG_Version         = 64;


                //U32 const xxh = XXH32(header, length, 0);
                //return (BYTE)(xxh >> 8);
                //    *dstPtr = LZ4F_headerChecksum(headerStart, dstPtr - headerStart);
                LZ4FileHeaderInfo.WriteHeader(_innerStream, fileHeaderInfo);
            }
            LZ4HeaderChunkInfo chunkInfo = new LZ4HeaderChunkInfo();

            chunkInfo.IsCompressed = true;
            chunkInfo.ChunkSize    = compressedLength;
            LZ4HeaderChunkInfo.WriteHeader(_innerStream, chunkInfo);

            _innerStream.Write(compressed, 0, compressedLength);

            _bufferOffset = 0;
        }
Пример #3
0
        private async Task FlushCurrentChunkAsync()
        {
            if (_bufferOffset <= 0)
            {
                return;
            }

            var compressed       = new byte[_bufferOffset];
            var compressedLength = _highCompression
                ? LZ4Codec.EncodeHC(_buffer, 0, _bufferOffset, compressed, 0, _bufferOffset)
                : LZ4Codec.Encode(_buffer, 0, _bufferOffset, compressed, 0, _bufferOffset);

            if (compressedLength <= 0 || compressedLength >= _bufferOffset)
            {
                // incompressible block
                compressed       = _buffer;
                compressedLength = _bufferOffset;
            }

            var isCompressed = compressedLength < _bufferOffset;

            var flags = ChunkFlags.None;

            if (isCompressed)
            {
                flags |= ChunkFlags.Compressed;
            }
            if (_highCompression)
            {
                flags |= ChunkFlags.HighCompression;
            }

            await WriteVarIntAsync((ulong)flags).ConfigureAwait(false);
            await WriteVarIntAsync((ulong)_bufferOffset).ConfigureAwait(false);

            if (isCompressed)
            {
                await WriteVarIntAsync((ulong)compressedLength).ConfigureAwait(false);
            }

            await _innerStream.WriteAsync(compressed, 0, compressedLength).ConfigureAwait(false);

            _bufferOffset = 0;
        }
Пример #4
0
        /// <summary>Flushes current chunk.</summary>
        private void FlushCurrentChunk()
        {
            if (_bufferOffset <= 0)
            {
                return;
            }

            var compressed       = new byte[_bufferOffset];
            var compressedLength = _highCompression
                                ? LZ4Codec.EncodeHC(_buffer, 0, _bufferOffset, compressed, 0, _bufferOffset)
                                : LZ4Codec.Encode(_buffer, 0, _bufferOffset, compressed, 0, _bufferOffset);

            if (compressedLength <= 0 || compressedLength >= _bufferOffset)
            {
                // incompressible block
                compressed       = _buffer;
                compressedLength = _bufferOffset;
            }

            var isCompressed = compressedLength < _bufferOffset;

            var flags = ChunkFlags.None;

            if (isCompressed)
            {
                flags |= ChunkFlags.Compressed;
            }
            if (_highCompression)
            {
                flags |= ChunkFlags.HighCompression;
            }

            WriteVarInt((ulong)flags);
            WriteVarInt((ulong)_bufferOffset);
            if (isCompressed)
            {
                WriteVarInt((ulong)compressedLength);
            }

            _innerStream.Write(compressed, 0, compressedLength);

            _bufferOffset = 0;
        }