public static bool CompressFileLZMA(string inFile, string outFile) { try { SevenZip.Sdk.Compression.Lzma.Encoder coder = new SevenZip.Sdk.Compression.Lzma.Encoder(); FileStream input = new FileStream(inFile, FileMode.Open); FileStream output = new FileStream(outFile, FileMode.Create); // Write the encoder properties coder.WriteCoderProperties(output); // Write the decompressed file size. output.Write(BitConverter.GetBytes(input.Length), 0, 8); // Encode the file. coder.Code(input, output, input.Length, -1, null); output.Flush(); output.Close(); } catch (Exception) { return false; } return true; }
public static Stream CompressFileLZMA(Stream inFile) { Stream outFile = null; try { SevenZip.Sdk.Compression.Lzma.Encoder coder = new SevenZip.Sdk.Compression.Lzma.Encoder(); outFile = new MemoryStream(); if (outFile != null && outFile.CanSeek) outFile.Position = 0; // Write the encoder properties coder.WriteCoderProperties(outFile); // Write the decompressed file size. outFile.Write(BitConverter.GetBytes(inFile.Length), 0, 8); // Encode the file. coder.Code(inFile, outFile, inFile.Length, -1, null); if (outFile != null && outFile.CanSeek) outFile.Position = 0; } catch { return null; } return outFile; }
/// <summary> /// Provides a filter for compressing a <see cref="Stream"/> with LZMA. /// </summary> /// <param name="stream">The underlying <see cref="Stream"/> to write the compressed data to.</param> /// <param name="bufferSize">The maximum number of uncompressed bytes to buffer. 32k (the step size of <see cref="SevenZip"/>) is a sensible minimum.</param> /// <remarks> /// This method internally uses multi-threading and a <see cref="CircularBufferStream"/>. /// The <paramref name="stream"/> may be closed with a delay. /// </remarks> private static Stream GetCompressionStream(Stream stream, int bufferSize = 128 * 1024) { var bufferStream = new CircularBufferStream(bufferSize); var encoder = new Encoder(); var consumerThread = new Thread(() => { try { // Write LZMA header encoder.SetCoderProperties( new[] {CoderPropId.DictionarySize, CoderPropId.PosStateBits, CoderPropId.LitContextBits, CoderPropId.LitPosBits, CoderPropId.Algorithm, CoderPropId.NumFastBytes, CoderPropId.MatchFinder, CoderPropId.EndMarker}, new object[] {1 << 23, 2, 3, 0, 2, 128, "bt4", true}); encoder.WriteCoderProperties(stream); // Write "uncompressed length" header var uncompressedLengthData = BitConverter.GetBytes(TarLzmaExtractor.UnknownSize); if (!BitConverter.IsLittleEndian) Array.Reverse(uncompressedLengthData); stream.Write(uncompressedLengthData); encoder.Code( inStream: bufferStream, outStream: stream, inSize: TarLzmaExtractor.UnknownSize, outSize: TarLzmaExtractor.UnknownSize, progress: null); } catch (ObjectDisposedException) { // If the buffer stream is closed too early the user probably just canceled the compression process } finally { stream.Dispose(); } }) {IsBackground = true}; consumerThread.Start(); return new DisposeWarpperStream(bufferStream, disposeHandler: () => { bufferStream.DoneWriting(); consumerThread.Join(); }); }
/// <summary> /// Compresses byte array with LZMA algorithm (C# inside) /// </summary> /// <param name="data">Byte array to compress</param> /// <returns>Compressed byte array</returns> public static byte[] CompressBytes(byte[] data) { using (var inStream = new MemoryStream(data)) { using (var outStream = new MemoryStream()) { var encoder = new Encoder(); WriteLzmaProperties(encoder); encoder.WriteCoderProperties(outStream); long streamSize = inStream.Length; for (int i = 0; i < 8; i++) outStream.WriteByte((byte)(streamSize >> (8 * i))); encoder.Code(inStream, outStream, -1, -1, null); return outStream.ToArray(); } } }
/// <summary> /// Compresses the specified stream with LZMA algorithm (C# inside) /// </summary> /// <param name="inStream">The source uncompressed stream</param> /// <param name="outStream">The destination compressed stream</param> /// <param name="inLength">The length of uncompressed data (null for inStream.Length)</param> /// <param name="codeProgressEvent">The event for handling the code progress</param> public static void CompressStream(Stream inStream, Stream outStream, int? inLength, EventHandler<ProgressEventArgs> codeProgressEvent) { if (!inStream.CanRead || !outStream.CanWrite) { throw new ArgumentException("The specified streams are invalid."); } var encoder = new Encoder(); WriteLzmaProperties(encoder); encoder.WriteCoderProperties(outStream); long streamSize = inLength.HasValue ? inLength.Value : inStream.Length; for (int i = 0; i < 8; i++) { outStream.WriteByte((byte)(streamSize >> (8 * i))); } encoder.Code(inStream, outStream, -1, -1, new LzmaProgressCallback(streamSize, codeProgressEvent)); }
internal static void WriteLzmaProperties(Encoder encoder) { #region LZMA properties definition CoderPropId[] propIDs = { CoderPropId.DictionarySize, CoderPropId.PosStateBits, CoderPropId.LitContextBits, CoderPropId.LitPosBits, CoderPropId.Algorithm, CoderPropId.NumFastBytes, CoderPropId.MatchFinder, CoderPropId.EndMarker }; object[] properties = { _lzmaDictionarySize, 2, 3, 0, 2, 256, "bt4", false }; #endregion encoder.SetCoderProperties(propIDs, properties); }
private void Init() { _buffer.Capacity = _bufferCapacity; SevenZipCompressor.LzmaDictionarySize = _bufferCapacity; _lzmaEncoder = new Encoder(); SevenZipCompressor.WriteLzmaProperties(_lzmaEncoder); }