How to Use the 7Z Sdk to Compress and Decompress a File

How to use the 7z SDK to compress and decompress a XZ file

I researched this problem by myself. In fact, you can't decompress XZ files using 7-Zip SDK for C#.

From http://www.7-zip.org/sdk.html:

ANSI-C compatible source code for LZMA / LZMA2 / XZ compression and decompression

C# source code for LZMA compression and decompression

There is XZ and LZMA2 decoder source code only for ANSI-C. C# has only LZMA decoder. However you can still decode XZ in C# using third party binaries from xzutils.

using 7zip sdk to compress a file, but can not decompress using winrar or 7zip

You can try to use 7zSharp wrapper or at least analyze wrappers code how everything is done.

Code to compress file(taken from 7zSharp):

  public void EncodeSingleFile(string inFile, string outFile)
{
using (FileStream inStream = new FileStream(inFile, FileMode.Open, FileAccess.Read))
{
using (FileStream outStream = new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write))
{
EncodeSingleFile(inStream, outStream);
}
}
}

public void EncodeSingleFile(FileStream inStream, FileStream outStream)
{
bool eos = false;
Int32 dictionary = 1 << 21;
Int32 posStateBits = 2;
Int32 litContextBits = 3; // for normal files
// UInt32 litContextBits = 0; // for 32-bit data
Int32 litPosBits = 0;
// UInt32 litPosBits = 2; // for 32-bit data
Int32 algorithm = 2;
Int32 numFastBytes = 128;
string mf = "bt4";

propIDs = new CoderPropID[]
{
CoderPropID.DictionarySize,
CoderPropID.PosStateBits,
CoderPropID.LitContextBits,
CoderPropID.LitPosBits,
CoderPropID.Algorithm,
CoderPropID.NumFastBytes,
CoderPropID.MatchFinder,
CoderPropID.EndMarker
};
properties = new object[]
{
dictionary,
posStateBits,
litContextBits,
litPosBits,
algorithm,
numFastBytes,
mf,
eos
};

Encoder encoder = new Encoder();
encoder.SetCoderProperties(propIDs, properties);
encoder.WriteCoderProperties(outStream);
Int64 fileSize = inStream.Length;
for (int i = 0; i < 8; i++)
{
outStream.WriteByte((Byte) (fileSize >> (8*i)));
}
encoder.Code(inStream, outStream, -1, -1, null);
}

Decompress multiple files using 7z SDK

I did something like this with gzipstream. but its all about how you want pack the files.

In this case something like this would be ideal.

string payload = Path.GetTempFileName();
using (FileStream temp_fs = new FileStream(payload, FileMode.OpenOrCreate))
{
using (BinaryWriter output_s = new BinaryWriter(new FileStream("target.out", FileMode.OpenOrCreate)))
{
//Write a blank. must be a long!
output_s.Write((long)0);
foreach (string file in Files)
{

//Write the files name
output_s.Write(file);
long start = temp_fs.Position;

//Write the starting point
output_s.Write(start);

//Compress the file to the payload
using (GZipStream gzip = new GZipStream(temp_fs, CompressionMode.Compress, true))
{
using (FileStream fs = new FileStream(file, FileMode.Open))
{
fs.CopyTo(gzip);
}
}

//Write the length
output_s.Write(temp_fs.Position - start);
}

//When all files are written
//Get the size of our header
long headersize = output_s.BaseStream.Length - 8;

//Copy the temp file data to the end
temp_fs.CopyTo(output_s.BaseStream);

//Reset to the start of the stream
output_s.BaseStream.Position = 0;

//override our zero
output_s.Write(headersize);
}

}
File.Delete(payload);

When you read your file will a binary read you will be able to grab the file by name then the next to long after the filename will be its size and start.
then you would decompress section header+pos to length. to retrieve your file.

using 7zip sdk to compress a file, but the archive file is not as original and can not decompress using unrar

There is an example on CodeProject of someone creating a C# interface for 7z using the SDK. He also mentions it is now possible to use COM against the DLL's, but I don't know how that works.

Check out C# (.NET) Interface for 7-Zip Archive DLLs on The Code Project.

Ho to setup and use the LZMA compression algorithm SDK

In the LZMA SDK , i found the file LzmaUtil.slnthat you can run easily in visual studio.

After building the solution you can either add arguments in visual studio or just run it from command line

Usage:  lzma <e|d> inputFile outputFile
e: encode file
d: decode file

7zip SDK: Reading/Writing properties. What is written and how to read it in human form?

I figured that:

I am saving the file name and extension in a variable like that:

        string nameOfFile = new FileInfo(filesToCompress[i]).Name;

To write the file name as a property on my compressed file, I have to transform that into a byte array and use the fileStream to write it into the file:

            //write the file name
byte[] filenameByts = Encoding.Default.GetBytes(nameOfFile);
output.Write(filenameByts, 0, filenameByts.Length);

How to develop incremental compression/decompression in LZMA SDK?

The LZMA SDK already permit this option , you can easily choose the number of iterations to use and the size of input / output Buffers to be used in the algorithm.



Related Topics



Leave a reply



Submit