I am using 7zip sdk (http://www.7-zip.org/sdk.html) to compress a file.
it works fine using this wrapper:
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);
}
However, I have got a problem:
I can only decompress it using 7zip shell installed from 7-zip.org, can not decompress with unrar. is that possible if I set some parameters and it will work with unrar as well?
If I open the file in 7zip and check the properties, I got:
- Name: abc.pdb
- Size: 1 809 920
- Packed Size: 249 305
- Method: LZMA:21
- Type: lzma
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.
If you are looking to create a RAR archive, you may be out of luck, as it is closed-source. You can probably use external libraries, but the compression algorithm is proprietary.
From the RAR Wiki Page:
RAR files may be created only with commercial software WinRAR, RAR, and software that has permission from the licensor Alexander Roshal
I would recommend investigating one of the many unencumbered alternatives.
来源:https://stackoverflow.com/questions/8340636/using-7zip-sdk-to-compress-a-file-but-the-archive-file-is-not-as-original-and-c