According to this link How do I create 7-Zip archives with .NET? , WOPR tell us how to compress a file with LMZA (7z compression algorithm) using 7z SDK ( http://www.7-zip.o
This question is a little old, but google fails to provide a satisfactory answer so this is for those like me still seeking it out. If you look into the LMZAAlone folder of the SDK there is code that compresses and decompresses files. Using it as an example it would seem you need to write and read the encoder properties and decompresses file size to your output file:
private static void CompressFileLZMA(string inFile, string outFile)
{
SevenZip.Compression.LZMA.Encoder coder = new SevenZip.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();
}
private static void DecompressFileLZMA(string inFile, string outFile)
{
SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder();
FileStream input = new FileStream(inFile, FileMode.Open);
FileStream output = new FileStream(outFile, FileMode.Create);
// Read the decoder properties
byte[] properties = new byte[5];
input.Read(properties, 0, 5);
// Read in the decompress file size.
byte [] fileLengthBytes = new byte[8];
input.Read(fileLengthBytes, 0, 8);
long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);
coder.SetDecoderProperties(properties);
coder.Code(input, output, input.Length, fileLength, null);
output.Flush();
output.Close();
}
Note that the files created this way can be extracted by the 7zip program as well but will not retain their filename or any other metadata.
I needed LZMA compression for sending images over network, not sure it's the best alternative but at least it works in my ecosystem! So here is something that should work right away for that purpose.
using System;
using System.IO;
using SevenZip;
public class LZMA{
public static byte[] Compress(byte[] toCompress)
{
SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder();
using(MemoryStream input = new MemoryStream(toCompress))
using(MemoryStream output = new MemoryStream()){
coder.WriteCoderProperties(output);
for (int i = 0; i < 8; i++) {
output.WriteByte((byte)(input.Length >> (8 * i)));
}
coder.Code(input, output, -1, -1, null);
return output.ToArray();
}
}
public static byte[] Decompress(byte[] toDecompress)
{
SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder();
using(MemoryStream input = new MemoryStream(toDecompress))
using(MemoryStream output = new MemoryStream()){
// Read the decoder properties
byte[] properties = new byte[5];
input.Read(properties, 0, 5);
// Read in the decompress file size.
byte [] fileLengthBytes = new byte[8];
input.Read(fileLengthBytes, 0, 8);
long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);
coder.SetDecoderProperties(properties);
coder.Code(input, output, input.Length, fileLength, null);
return output.ToArray();
}
}
}
I highly recommend managed-lzma:
https://github.com/weltkante/managed-lzma
It preserves file info and directory structure in file encoding.