public SetCoderProperties ( CoderPropID propIDs, object properties ) : void | ||
propIDs | CoderPropID | |
properties | object | |
리턴 | void |
public static MemoryStream Compress(MemoryStream inStream) { inStream.Position = 0; CoderPropId[] propIDs = { CoderPropId.DictionarySize, CoderPropId.PosStateBits, CoderPropId.LitContextBits, CoderPropId.LitPosBits, CoderPropId.Algorithm }; object[] properties = { (1 << 16), 2, 3, 0, 2 }; var outStream = new MemoryStream(); var encoder = new Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); for (var i = 0; i < 8; i++) outStream.WriteByte((byte) (inStream.Length >> (8 * i))); encoder.Code(inStream, outStream, -1, -1, null); outStream.Flush(); outStream.Position = 0; return outStream; }
public void writeCompressedData(byte[] data) { int dicSize; if (compressionLevel <= 5) { dicSize = 1 << (compressionLevel * 2 + 14); } else if (compressionLevel == 6) { dicSize = 1 << 25; } else { dicSize = 1 << 26; } object[] properties = { (Int32)dicSize, (Int32)(2), (Int32)(3), (Int32)(0), (Int32)(2), (Int32)(compressionLevel < 7 ? 32 : 64), "bt4", true }; Encoder encoder = new Encoder(); encoder.SetCoderProperties(propIDs, properties); MemoryStream inStream = new MemoryStream(data); MemoryStream outStream = new MemoryStream(); encoder.Code(inStream, outStream, -1, -1, null); byte[] compressedData = outStream.ToArray(); //This is the custom way of OpenCTM to write the LZMA properties writeLittleInt(compressedData.Length); encoder.WriteCoderProperties(BaseStream); Write(compressedData); }
public static void CompressFileLZMA(string inFile, string outFile) { Int32 dictionary = 1 << 23; 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"; bool eos = true; bool stdInMode = false; CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { (Int32)(dictionary), (Int32)(posStateBits), (Int32)(litContextBits), (Int32)(litPosBits), (Int32)(algorithm), (Int32)(numFastBytes), mf, eos }; using (FileStream inStream = new FileStream(inFile, FileMode.Open)) { using (FileStream outStream = new FileStream(outFile, FileMode.Create)) { SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); Int64 fileSize; if (eos || stdInMode) { fileSize = -1; } else { fileSize = inStream.Length; } for (int i = 0; i < 8; i++) { outStream.WriteByte((Byte)(fileSize >> (8 * i))); } encoder.Code(inStream, outStream, -1, -1, null); } } }
/// <summary> /// Compress input stream into output stream using LZMA. /// Remeber to set desirable positions input and output streams. /// Input reading starts at inStream.Position and ends at inStream.Length. /// Output writing starts at outStream.Position. /// </summary> /// <param name="inStream">Input stream</param> /// <param name="outStream">Output stream</param> public static void Compress(Stream inStream, Stream outStream) { Compression.LZMA.Encoder encoder = new Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); var writer = new BinaryWriter(outStream, Encoding.UTF8); // Write original size. writer.Write(inStream.Length - inStream.Position); // Save position with compressed size. long positionForCompressedSize = outStream.Position; // Leave placeholder for size after compression. writer.Write((Int64)0); long positionForCompressedDataStart = outStream.Position; encoder.Code(inStream, outStream, -1, -1, null); long positionAfterCompression = outStream.Position; // Seek back to the placeholder for compressed size. outStream.Position = positionForCompressedSize; // Write size after compression. writer.Write((Int64)(positionAfterCompression - positionForCompressedDataStart)); // Restore position. outStream.Position = positionAfterCompression; }
// Compress track back to a file that the games can read public static void Compress(string filePath, DecompressedTrack track) { // Setup LZMA encoder SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); SevenZip.CoderPropID[] coderPropIDs = { SevenZip.CoderPropID.DictionarySize, SevenZip.CoderPropID.LitContextBits, SevenZip.CoderPropID.LitPosBits, SevenZip.CoderPropID.PosStateBits }; object[] properties = LzmaPropertiesFromBytes(track.Properties); encoder.SetCoderProperties(coderPropIDs, properties); // Write compressed track to file using (FileStream fileStream = File.OpenWrite(filePath)) { // Write track header fileStream.Write(track.Header, 0, track.Header.Length); // Write LZMA header encoder.WriteCoderProperties(fileStream); fileStream.Write(BitConverter.GetBytes(track.Data.Length), 0, 4); // Write compressed track data using (MemoryStream memoryStream = new MemoryStream(track.Data)) { encoder.Code(memoryStream, fileStream, memoryStream.Length, -1, null); } } }
public static void Compress(Stream inStream, Stream outStream, ICodeProgress progress) { SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); //encoder.WriteCoderProperties(outStream); encoder.Code(inStream, outStream, -1, -1, progress); }
private static byte[] compress(byte[] decompressed) { byte[] retVal = null; bool eos = true; Int32 dictionary = 1 << 16; 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 = 64; string mf = "bt4"; var propIDs = new CoderPropID[] { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; var properties = new object[] { dictionary, posStateBits, litContextBits, litPosBits, algorithm, numFastBytes, mf, eos }; SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); using (Stream strmInStream = new MemoryStream(decompressed)) { strmInStream.Seek(0, 0); using (MemoryStream strmOutStream = new MemoryStream()) { encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(strmOutStream); Int64 fileSize = strmInStream.Length; for (int i = 0; i < 8; i++) { strmOutStream.WriteByte((Byte)(fileSize >> (8 * i))); } encoder.Code(strmInStream, strmOutStream, -1, -1, null); retVal = strmOutStream.ToArray(); } } return(retVal); }
public static void Compress(Stream inputStream, Stream outputStream) { Compression.LZMA.Encoder encoder = new Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outputStream); long fileSize = inputStream.Length; for (int i = 0; i < 8; i++) outputStream.WriteByte((Byte)(fileSize >> (8 * i))); encoder.Code(inputStream, outputStream, -1, -1, null); }
public static byte[] Compress(byte[] inputBytes) { MemoryStream inStream = new MemoryStream(inputBytes); MemoryStream outStream = new MemoryStream(); Encoder encoder = new Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); long fileSize = inStream.Length; for (int i = 0; i < 8; i++) outStream.WriteByte((Byte)(fileSize >> (8 * i))); encoder.Code(inStream, outStream, -1, -1, null); return outStream.ToArray(); }
public static byte[] Compress(byte[] inputBytes) { MemoryStream inStream = new MemoryStream(inputBytes); MemoryStream outStream = new MemoryStream(); SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); long fileSize = inStream.Length; for (int i = 0; i < 8; i++) { outStream.WriteByte((Byte)(fileSize >> (8 * i))); } encoder.Code(inStream, outStream, -1, -1, null); return(outStream.ToArray()); }
/// <summary> /// Compresses the inputBytes in Lzma. /// </summary> /// <param name="inputBytes">Bytes to compress.</param> /// <returns>Compressed bytes.</returns> public static byte[] Compress(byte[] inputBytes) { if (inputBytes == null) throw new ArgumentNullException("inputBytes"); var inStream = new MemoryStream(inputBytes); var outStream = new MemoryStream(); var encoder = new Encoder(); encoder.SetCoderProperties(PropertiesIDs, Properties); encoder.WriteCoderProperties(outStream); var fileSize = inStream.Length; for (int i = 0; i < 8; i++) outStream.WriteByte((byte)(fileSize >> (8 * i))); encoder.Code(inStream, outStream, -1, -1, null); return outStream.ToArray(); }
public static System.IO.Stream Compress(System.IO.Stream stream, uint length, bool EOF = false) { SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); System.IO.MemoryStream sout = new System.IO.MemoryStream(); if (EOF) { encoder.SetCoderProperties(new SevenZip.CoderPropID[] { SevenZip.CoderPropID.EndMarker }, new object[] { true }); } else { //写入文件长度 sout.Write(BitConverter.GetBytes(length), 0, 4); } encoder.WriteCoderProperties(sout); encoder.Code(stream, sout, (int)length, -1, null); sout.Position = 0; return sout; }
public static byte[] Compress(byte[] bytes) { using (MemoryStream input = new MemoryStream(bytes)) { MemoryStream output = new MemoryStream(); Encoder encoder = new Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(output); if (BitConverter.IsLittleEndian) { byte[] LengthHeader = BitConverter.GetBytes(input.Length); output.Write(LengthHeader, 0, LengthHeader.Length); } encoder.Code(input, output, input.Length, -1, null); return output.ToArray(); } }
private static Stream CompressInternal(Stream inStream) { var outStream = new MemoryStream(); var encoder = new LZMA.Encoder(); encoder.SetCoderProperties(s_propertiesIds, s_properties); encoder.WriteCoderProperties(outStream); // Uncompressed size. var outSize = inStream.Length; var outSizeBytes = BitConverter.GetBytes(outSize); outStream.Write(outSizeBytes, 0, 4); encoder.Code(inStream, outStream, -1, -1, null); outStream.Seek(0, SeekOrigin.Begin); return(outStream); }
public static bool Compress(byte[] inputBytes, string FileName, bool Overwrite) { if (File.Exists(FileName)) { if(Overwrite) { File.Delete(FileName); } else { return false; } } //MemoryStream inStream = new MemoryStream(inputBytes); using(FileStream outStream = File.Create(FileName)) { MemoryStream inStream = new MemoryStream(inputBytes); SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); long fileSize = inStream.Length; for (int i = 0; i < 8; i++) outStream.WriteByte((Byte)(fileSize >> (8 * i))); encoder.Code(inStream, outStream, -1, -1, null); } return true; }
private static void Compress(Stream inputStream, Stream outputStream, LZMADictionarySize dictSize) { SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder(); Int32 dictSize32 = (Int32)dictSize; coder.SetCoderProperties (new SevenZip.CoderPropID[] {SevenZip.CoderPropID.DictionarySize}, new object[] {dictSize32}); // Write encoder properties to output stream coder.WriteCoderProperties(outputStream); // Write size of input stream to output stream. outputStream.Write(BitConverter.GetBytes(inputStream.Length), 0, 8); // Encode coder.Code(inputStream, outputStream, inputStream.Length, -1, null); outputStream.Flush(); outputStream.Close(); }
public override byte[] Code(Byte[] data) { int dictionary = 1 << 23; 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; CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { (Int32)(dictionary), (Int32)(posStateBits), (Int32)(litContextBits), (Int32)(litPosBits), (Int32)(algorithm), (Int32)(numFastBytes), "bt4", false }; MemoryStream inpuMemoryStream = new MemoryStream(data); MemoryStream outputMemoryStream = new MemoryStream(); SevenZip.Compression.LZMA.Encoder encoder = new Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outputMemoryStream); Int64 fileSize = inpuMemoryStream.Length; for (int i = 0; i < 8; i++) { byte b = (Byte) (fileSize >> (8*i)); outputMemoryStream.WriteByte(b); } encoder.Code(inpuMemoryStream, outputMemoryStream, -1, -1, null); var outBuffer = outputMemoryStream.ToArray(); return outBuffer; //int keyIndex = 0; //for (uint i = 0; i < data.Length; ++i) //{ // data[i] ^= mKey[keyIndex]; // if (keyIndex < mKey.Length - 1) // { // ++keyIndex; // } // else // { // keyIndex = 0; // } //} //return data; }
public static void Main() { var l = new HttpListener(); l.Prefixes.Add("http://*:80/"); l.Start(); Console.WriteLine("HttpListener is listening..."); while (l.IsListening) { var cont = l.GetContext(); Console.WriteLine("Incoming request for: {0}", cont.Request.RawUrl.Replace("/", @"\")); var fname = Environment.CurrentDirectory + cont.Request.RawUrl.Replace("/", @"\"); if (!File.Exists(fname)) { var modfname = fname.Substring(0, fname.Length - 4); if (Path.GetExtension(fname) == ".7zl" && File.Exists(modfname)) { var outStream = new FileStream(fname, FileMode.Create, FileAccess.Write); var inStream = new FileStream(modfname, FileMode.Open, FileAccess.Read); var encoder = new LZ.Encoder(); encoder.SetCoderProperties(PropIDs, Properties); encoder.WriteCoderProperties(outStream); var instreamLen = inStream.Length; outStream.Write(BitConverter.GetBytes(instreamLen), 0, 8); encoder.Code(inStream, outStream, -1, -1, null); outStream.Close(); inStream.Close(); } else { cont.Response.Close(); continue; } } var buff = File.ReadAllBytes(fname); var mime = "text/html"; switch (Path.GetExtension(fname)) { case "txt": mime = "text/plain"; break; case "tga": mime = "image/targa"; break; case "bmp": case "png": case "gif": case "tiff": case "jpeg": mime = String.Format("image/{0}", Path.GetExtension(fname)); break; case "tif": mime = "image/tiff"; break; case "jpg": case "jpe": mime = "image/jpeg"; break; case "js": mime = "text/javascript"; break; case "css": mime = "text/css"; break; } try { cont.Response.ContentType = mime; cont.Response.ContentLength64 = buff.Length; cont.Response.OutputStream.Write(buff, 0, buff.Length); //cont.Response.Close(); } catch (HttpListenerException) { } } }
static SevenZipHelper() { encoder = new Encoder(); encoder.SetCoderProperties(propIDs, properties); decoder = new Decoder(); }
static int Main2(string[] args) { System.Console.WriteLine("\nLZMA# 4.61 2008-11-23\n"); if (args.Length == 0) { PrintHelp(); return 0; } SwitchForm[] kSwitchForms = new SwitchForm[13]; int sw = 0; kSwitchForms[sw++] = new SwitchForm("?", SwitchType.Simple, false); kSwitchForms[sw++] = new SwitchForm("H", SwitchType.Simple, false); kSwitchForms[sw++] = new SwitchForm("A", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("D", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("FB", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("LC", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("LP", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("PB", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("MF", SwitchType.UnLimitedPostString, false, 1); kSwitchForms[sw++] = new SwitchForm("EOS", SwitchType.Simple, false); kSwitchForms[sw++] = new SwitchForm("SI", SwitchType.Simple, false); kSwitchForms[sw++] = new SwitchForm("SO", SwitchType.Simple, false); kSwitchForms[sw++] = new SwitchForm("T", SwitchType.UnLimitedPostString, false, 1); Parser parser = new Parser(sw); try { parser.ParseStrings(kSwitchForms, args); } catch { return IncorrectCommand(); } if (parser[(int)Key.Help1].ThereIs || parser[(int)Key.Help2].ThereIs) { PrintHelp(); return 0; } System.Collections.ArrayList nonSwitchStrings = parser.NonSwitchStrings; int paramIndex = 0; if (paramIndex >= nonSwitchStrings.Count) return IncorrectCommand(); string command = (string)nonSwitchStrings[paramIndex++]; command = command.ToLower(); bool dictionaryIsDefined = false; Int32 dictionary = 1 << 21; if (parser[(int)Key.Dictionary].ThereIs) { Int32 dicLog; if (!GetNumber((string)parser[(int)Key.Dictionary].PostStrings[0], out dicLog)) IncorrectCommand(); dictionary = (Int32)1 << dicLog; dictionaryIsDefined = true; } string mf = "bt4"; if (parser[(int)Key.MatchFinder].ThereIs) mf = (string)parser[(int)Key.MatchFinder].PostStrings[0]; mf = mf.ToLower(); if (command == "b") { const Int32 kNumDefaultItereations = 10; Int32 numIterations = kNumDefaultItereations; if (paramIndex < nonSwitchStrings.Count) if (!GetNumber((string)nonSwitchStrings[paramIndex++], out numIterations)) numIterations = kNumDefaultItereations; return LzmaBench.LzmaBenchmark(numIterations, (UInt32)dictionary); } string train = ""; if (parser[(int)Key.Train].ThereIs) train = (string)parser[(int)Key.Train].PostStrings[0]; bool encodeMode = false; if (command == "e") encodeMode = true; else if (command == "d") encodeMode = false; else IncorrectCommand(); bool stdInMode = parser[(int)Key.StdIn].ThereIs; bool stdOutMode = parser[(int)Key.StdOut].ThereIs; Stream inStream = null; if (stdInMode) { throw (new Exception("Not implemeted")); } else { if (paramIndex >= nonSwitchStrings.Count) IncorrectCommand(); string inputName = (string)nonSwitchStrings[paramIndex++]; inStream = new FileStream(inputName, FileMode.Open, FileAccess.Read); } FileStream outStream = null; if (stdOutMode) { throw (new Exception("Not implemeted")); } else { if (paramIndex >= nonSwitchStrings.Count) IncorrectCommand(); string outputName = (string)nonSwitchStrings[paramIndex++]; outStream = new FileStream(outputName, FileMode.Create, FileAccess.Write); } FileStream trainStream = null; if (train.Length != 0) trainStream = new FileStream(train, FileMode.Open, FileAccess.Read); if (encodeMode) { if (!dictionaryIsDefined) dictionary = 1 << 23; 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; bool eos = parser[(int)Key.EOS].ThereIs || stdInMode; if (parser[(int)Key.Mode].ThereIs) if (!GetNumber((string)parser[(int)Key.Mode].PostStrings[0], out algorithm)) IncorrectCommand(); if (parser[(int)Key.FastBytes].ThereIs) if (!GetNumber((string)parser[(int)Key.FastBytes].PostStrings[0], out numFastBytes)) IncorrectCommand(); if (parser[(int)Key.LitContext].ThereIs) if (!GetNumber((string)parser[(int)Key.LitContext].PostStrings[0], out litContextBits)) IncorrectCommand(); if (parser[(int)Key.LitPos].ThereIs) if (!GetNumber((string)parser[(int)Key.LitPos].PostStrings[0], out litPosBits)) IncorrectCommand(); if (parser[(int)Key.PosBits].ThereIs) if (!GetNumber((string)parser[(int)Key.PosBits].PostStrings[0], out posStateBits)) IncorrectCommand(); CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { (Int32)(dictionary), (Int32)(posStateBits), (Int32)(litContextBits), (Int32)(litPosBits), (Int32)(algorithm), (Int32)(numFastBytes), mf, eos }; Compression.LZMA.Encoder encoder = new Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); Int64 fileSize; if (eos || stdInMode) fileSize = -1; else fileSize = inStream.Length; for (int i = 0; i < 8; i++) outStream.WriteByte((Byte)(fileSize >> (8 * i))); if (trainStream != null) { CDoubleStream doubleStream = new CDoubleStream(); doubleStream.s1 = trainStream; doubleStream.s2 = inStream; doubleStream.fileIndex = 0; inStream = doubleStream; long trainFileSize = trainStream.Length; doubleStream.skipSize = 0; if (trainFileSize > dictionary) doubleStream.skipSize = trainFileSize - dictionary; trainStream.Seek(doubleStream.skipSize, SeekOrigin.Begin); encoder.SetTrainSize((uint)(trainFileSize - doubleStream.skipSize)); } encoder.Code(inStream, outStream, -1, -1, null); } else if (command == "d") { byte[] properties = new byte[5]; if (inStream.Read(properties, 0, 5) != 5) throw (new Exception("input .lzma is too short")); Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder(); decoder.SetDecoderProperties(properties); if (trainStream != null) { if (!decoder.Train(trainStream)) throw (new Exception("can't train")); } long outSize = 0; for (int i = 0; i < 8; i++) { int v = inStream.ReadByte(); if (v < 0) throw (new Exception("Can't Read 1")); outSize |= ((long)(byte)v) << (8 * i); } long compressedSize = inStream.Length - inStream.Position; decoder.Code(inStream, outStream, compressedSize, outSize, null); } else throw (new Exception("Command Error")); return 0; }
/// <summary> /// Compresses the report for uploading. /// </summary> /// <param name="progress">The <see cref="ProgressManager"/> instance that the /// Upload function is using.</param> /// <param name="progressChanged">The progress changed event handler that should /// be called for upload progress updates.</param> private void Compress(SteppedProgressManager progress, ProgressChangedEventHandler progressChanged) { using (FileStream archiveStream = new FileStream(ReportBaseName + ".tar", FileMode.Create, FileAccess.Write)) { //Add the report into a tar file TarArchive archive = TarArchive.CreateOutputTarArchive(archiveStream); foreach (FileInfo file in Report.Files) { TarEntry entry = TarEntry.CreateEntryFromFile(file.FullName); entry.Name = Path.GetFileName(entry.Name); archive.WriteEntry(entry, false); } archive.Close(); } ProgressManager step = new ProgressManager(); progress.Steps.Add(new SteppedProgressManagerStep(step, 0.5f, "Compressing")); CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { (Int32)(1 << 24), //Dictionary Size (Int32)2, //PosState Bits (Int32)0, //LitContext Bits (Int32)2, //LitPos Bits (Int32)2, //Algorithm (Int32)128, //Fast Bytes "bt4", //Match Finger true //Write end-of-stream }; SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); using (FileStream sevenZipFile = new FileStream(ReportBaseName + ".tar.7z", FileMode.Create)) using (FileStream tarStream = new FileStream(ReportBaseName + ".tar", FileMode.Open, FileAccess.Read, FileShare.Read, 262144, FileOptions.DeleteOnClose)) { encoder.WriteCoderProperties(sevenZipFile); Int64 fileSize = -1; for (int i = 0; i < 8; i++) { sevenZipFile.WriteByte((Byte)(fileSize >> (8 * i))); } step.Total = tarStream.Length; ICodeProgress callback = progressChanged == null ? null : new SevenZipProgressCallback(this, progress, step, progressChanged); encoder.Code(tarStream, sevenZipFile, -1, -1, callback); } }
/// <inheritdoc /> public byte[] Compress(byte[] data, Action<double> progressFunc = null) { CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { 1 << 23, 2, 3, 0, 2, 128, "bt4", false }; var x = new MemoryStream(); var encoder = new Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(x); Int64 fileSize; fileSize = data.Length; for (int i = 0; i < 8; i++) x.WriteByte((Byte)(fileSize >> (8 * i))); ICodeProgress progress = null; if (progressFunc != null) progress = new CompressionLogger(progressFunc, data.Length); encoder.Code(new MemoryStream(data), x, -1, -1, progress); return x.ToArray(); }
public static void CompressFileLZMA(MemoryStream inFile, string outFile) { int dictionary = 33554432; int posStateBits = 2; int litContextBits = 3; int litPosBits = 0; int algorithm = 2; int numFastBytes = 32; string mf = "bt4"; bool eos = true; bool stdInMode = false; CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { dictionary, posStateBits, litContextBits, litPosBits, algorithm, numFastBytes, mf, eos }; byte[] TibiaHeader = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; using FileStream outStream = new FileStream(outFile, FileMode.Create); outStream.Write(TibiaHeader); SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); long fileSize; if (eos || stdInMode) { fileSize = -1; } else { fileSize = inFile.Length; } for (int i = 0; i < 8; i++) { outStream.WriteByte((Byte)(fileSize >> (8 * i))); } encoder.Code(inFile, outStream, -1, -1, null); MemoryStream tmp = new MemoryStream(); int size = (int)outStream.Length - 32; MyBinaryWriter bw = new MyBinaryWriter(tmp); bw.Write(new byte[] { 0x70, 0x0A, 0xFA, 0x80, 0x24 }); bw.Write7BitEncodedInt(size); if (tmp.Length == 8) { outStream.Position = 0x18; } else { outStream.Position = 0x19; } outStream.Write(tmp.ToArray(), 0, (int)tmp.Length); outStream.Position = 0; outStream.Close(); }
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); }
static public int LzmaBenchmark(Int32 numIterations, UInt32 dictionarySize) { if (numIterations <= 0) return 0; if (dictionarySize < (1 << 18)) { System.Console.WriteLine("\nError: dictionary size for benchmark must be >= 19 (512 KB)"); return 1; } System.Console.Write("\n Compressing Decompressing\n\n"); Compression.LZMA.Encoder encoder = new Compression.LZMA.Encoder(); Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder(); CoderPropID[] propIDs = { CoderPropID.DictionarySize, }; object[] properties = { (Int32)(dictionarySize), }; UInt32 kBufferSize = dictionarySize + kAdditionalSize; UInt32 kCompressedBufferSize = (kBufferSize / 2) + kCompressedAdditionalSize; encoder.SetCoderProperties(propIDs, properties); System.IO.MemoryStream propStream = new System.IO.MemoryStream(); encoder.WriteCoderProperties(propStream); byte[] propArray = propStream.ToArray(); CBenchRandomGenerator rg = new CBenchRandomGenerator(); rg.Set(kBufferSize); rg.Generate(); CRC crc = new CRC(); crc.Init(); crc.Update(rg.Buffer, 0, rg.BufferSize); CProgressInfo progressInfo = new CProgressInfo(); progressInfo.ApprovedStart = dictionarySize; UInt64 totalBenchSize = 0; UInt64 totalEncodeTime = 0; UInt64 totalDecodeTime = 0; UInt64 totalCompressedSize = 0; MemoryStream inStream = new MemoryStream(rg.Buffer, 0, (int)rg.BufferSize); MemoryStream compressedStream = new MemoryStream((int)kCompressedBufferSize); CrcOutStream crcOutStream = new CrcOutStream(); for (Int32 i = 0; i < numIterations; i++) { progressInfo.Init(); inStream.Seek(0, SeekOrigin.Begin); compressedStream.Seek(0, SeekOrigin.Begin); encoder.Code(inStream, compressedStream, -1, -1, progressInfo); TimeSpan sp2 = DateTime.UtcNow - progressInfo.Time; UInt64 encodeTime = (UInt64)sp2.Ticks; long compressedSize = compressedStream.Position; if (progressInfo.InSize == 0) throw (new Exception("Internal ERROR 1282")); UInt64 decodeTime = 0; for (int j = 0; j < 2; j++) { compressedStream.Seek(0, SeekOrigin.Begin); crcOutStream.Init(); decoder.SetDecoderProperties(propArray); UInt64 outSize = kBufferSize; System.DateTime startTime = DateTime.UtcNow; decoder.Code(compressedStream, crcOutStream, 0, (Int64)outSize, null); TimeSpan sp = (DateTime.UtcNow - startTime); decodeTime = (ulong)sp.Ticks; if (crcOutStream.GetDigest() != crc.GetDigest()) throw (new Exception("CRC Error")); } UInt64 benchSize = kBufferSize - (UInt64)progressInfo.InSize; PrintResults(dictionarySize, encodeTime, benchSize, false, 0); System.Console.Write(" "); PrintResults(dictionarySize, decodeTime, kBufferSize, true, (ulong)compressedSize); System.Console.WriteLine(); totalBenchSize += benchSize; totalEncodeTime += encodeTime; totalDecodeTime += decodeTime; totalCompressedSize += (ulong)compressedSize; } System.Console.WriteLine("---------------------------------------------------"); PrintResults(dictionarySize, totalEncodeTime, totalBenchSize, false, 0); System.Console.Write(" "); PrintResults(dictionarySize, totalDecodeTime, kBufferSize * (UInt64)numIterations, true, totalCompressedSize); System.Console.WriteLine(" Average"); return 0; }
private void CreateManifest_(List<FileInfo> results, string[] args) { string dataDir = args[0]; string patcherExe = args[1]; string destPath = args[2]; string outputFilename = args[3]; string projectPath = args[4]; SetStatus("Getting patcher version", 0); ProcessOutput patcher = null; try { patcher = ProcessOutput.Run(patcherExe, "-version", "", null, false, false); } catch (System.Exception) { } string patcherVersion = "[Fix me by hand]"; if (patcher != null) { patcher.Wait(); patcherVersion = patcher.stdout.Aggregate((i, j) => i + "\n" + j); } CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker, }; object[] properties = { (Int32)(1<<26), (Int32)(2), (Int32)(3), (Int32)(2), (Int32)(128), "bt4", false, }; SetStatus("Starting XML file...", 0); XmlDocument doc = new XmlDocument(); XmlElement rootNode = doc.CreateElement("dayzrp"); XmlElement serversNode = doc.CreateElement("servers"); XmlElement patchNode = doc.CreateElement("patch"); XmlAttribute data = doc.CreateAttribute("data"); XmlAttribute launcherVersion = doc.CreateAttribute("launcherVersion"); XmlAttribute launcherUrl = doc.CreateAttribute("launcherUrl"); XmlAttribute patchMode = doc.CreateAttribute("mode"); launcherVersion.Value = patcherVersion; launcherUrl.Value = Path.GetFileName(patcherExe); data.Value = dataDir; patchMode.Value = "lzma"; patchNode.Attributes.Append(data); patchNode.Attributes.Append(launcherUrl); patchNode.Attributes.Append(launcherVersion); patchNode.Attributes.Append(patchMode); XmlComment comment = doc.CreateComment("<server name='RP1 : S1' host='81.170.227.227' port='2302'/>"); serversNode.AppendChild(comment); rootNode.AppendChild(serversNode); rootNode.AppendChild(patchNode); doc.AppendChild(rootNode); int progress = 0; double progressCount = (double)results.Count * 2; foreach (var file in results) { XmlElement fileNode = doc.CreateElement("file"); XmlAttribute path = doc.CreateAttribute("path"); XmlAttribute hash = doc.CreateAttribute("hash"); XmlAttribute url = doc.CreateAttribute("url"); string nameValue = GetRelativeName(file, projectPath); string hashValue; SetStatus("Hashing " + nameValue, progress++ / progressCount); using (Stream stream = file.OpenRead()) { hashValue = StringExtension.ToHexString(m_md5.ComputeHash(stream)); stream.Close(); } path.Value = nameValue; hash.Value = hashValue; url.Value = dataDir + "/" + nameValue + ".lzma"; fileNode.Attributes.Append(path); fileNode.Attributes.Append(hash); fileNode.Attributes.Append(url); patchNode.AppendChild(fileNode); SetStatus("Compressing " + nameValue, progress++ / progressCount); using (Stream inStream = file.OpenRead()) { string outFile = Path.Combine(destPath, nameValue + ".lzma"); EnsurePathExists(outFile); // if( (new FileInfo(outFile)).Exists ) // continue; Stream outStream = new FileStream(outFile, FileMode.Create, FileAccess.Write); LZMA.Encoder encoder = new LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); Int64 fileSize; fileSize = inStream.Length; for (int i = 0; i < 8; i++) outStream.WriteByte((Byte)(fileSize >> (8 * i))); encoder.Code(inStream, outStream, -1, -1, null); inStream.Close(); outStream.Close(); } } SetStatus("Saving XML...", 1); doc.Save(outputFilename); SetStatus("Done!", 0); Dispatcher.BeginInvoke(DispatcherPriority.Send, (Action)delegate() { builtButton.IsEnabled = true; }); }