private static IDictionary <string, string> BuildRenameDict(string bsaName) { var dictPath = Path.Combine(InDir, "TTW Patches", bsaName, "RenameFiles.dict"); if (File.Exists(dictPath)) { var renameDict = Util.ReadOldDatabase(dictPath); var newRenPath = Path.Combine(OutDir, Path.ChangeExtension(bsaName, ".ren")); if (!File.Exists(newRenPath)) { using (var fileStream = File.OpenWrite(newRenPath)) using (var lzmaStream = new LzmaEncodeStream(fileStream)) using (var writer = new BinaryWriter(lzmaStream)) { writer.Write(renameDict.Count); foreach (var kvp in renameDict) { writer.Write(kvp.Key); writer.Write(kvp.Value); } } } return(renameDict); } return(new Dictionary <string, string>()); }
public void LzmaEncodeDecodeTest() { using (var output = new FileStream(TemporaryFile, FileMode.Create)) { var encoder = new LzmaEncodeStream(output); using (var inputSample = new FileStream(@"TestData\zip.zip", FileMode.Open)) { int bufSize = 24576, count; var buf = new byte[bufSize]; while ((count = inputSample.Read(buf, 0, bufSize)) > 0) { encoder.Write(buf, 0, count); } } encoder.Close(); } var newZip = Path.Combine(OutputDirectory, "new.zip"); using (var input = new FileStream(TemporaryFile, FileMode.Open)) { var decoder = new LzmaDecodeStream(input); using (var output = new FileStream(newZip, FileMode.Create)) { int bufSize = 24576, count; var buf = new byte[bufSize]; while ((count = decoder.Read(buf, 0, bufSize)) > 0) { output.Write(buf, 0, count); } } } Assert.IsTrue(File.Exists(newZip)); using (var extractor = new SevenZipExtractor(newZip)) { Assert.AreEqual(1, extractor.FilesCount); Assert.AreEqual("zip.txt", extractor.ArchiveFileNames[0]); } }
public static void Save(Vector <int>[,] data, DctParameters parameters) { using (var fileStream = File.Open(parameters.SavePath, FileMode.Create)) using (var encodeStream = new LzmaEncodeStream(fileStream)) using (var writer = new BinaryWriter(encodeStream)) { writer.Write((byte)parameters.DecimationType); writer.Write((byte)parameters.QuantizationType); switch (parameters.QuantizationType) { case QuantizationType.LargestN: writer.Write(parameters.Ny.Value); writer.Write(parameters.Nc.Value); break; case QuantizationType.QuantizationMatrix: writer.Write(parameters.GeneratorsY.Alpha); writer.Write(parameters.GeneratorsY.Gamma); writer.Write(parameters.GeneratorsC.Alpha); writer.Write(parameters.GeneratorsC.Gamma); break; case QuantizationType.DefaultJpegMatrix: break; default: throw new ArgumentOutOfRangeException(); } var n = data.GetLength(0); var m = data.GetLength(1); writer.Write(n); writer.Write(m); for (var i = 0; i < n; ++i) { for (var j = 0; j < m; ++j) { for (var k = 0; k < 3; ++k) { writer.Write(data[i, j][k]); } } } } }
private static void SaveWavelet(BitmapSource bitmap, WaveletParameters parameters, Vector <double[, ]> channels) { using (var outputStream = File.Open(parameters.SavePath, FileMode.Create)) using (var encodeStream = new LzmaEncodeStream(outputStream)) using (var writer = new BinaryWriter(encodeStream)) { writer.Write(bitmap.PixelHeight); writer.Write(bitmap.PixelWidth); writer.Write(bitmap.DpiX); writer.Write(bitmap.DpiY); writer.Write((byte)parameters.WaveletType); writer.Write(parameters.IterationsCount); writer.Write(channels.Length); for (var i = 0; i < channels.Length; ++i) { WriteChannel(writer, channels[i]); } } }
/// <summary> /// 使用Lzma算法压缩集合至字节流 /// </summary> /// <typeparam name="Data"></typeparam> /// <param name="datas"></param> /// <returns></returns> public static byte[] Encodes <Data>(List <Data> datas) { if (datas == null || datas.Count == 0) { return(null); } byte[] buffers = null; using (MemoryStream output = new MemoryStream()) { var encoder = new LzmaEncodeStream(output); using (var inputSample = new MemoryStream()) { BinaryFormatter formatter = new BinaryFormatter(); datas.ForEach(D => { try { formatter.Serialize(inputSample, D); } catch (SerializationException ex) { LogHelper.GetInstance().Write("对象" + D + "未能序列化!"); } } ); inputSample.Position = 0; int bufSize = 24576, count; byte[] buf = new byte[bufSize]; while ((count = inputSample.Read(buf, 0, bufSize)) > 0) { encoder.Write(buf, 0, count); } } encoder.Close(); buffers = new byte[output.Length]; output.Position = 0; output.Read(buffers, 0, buffers.Length); } return(buffers); }
private void StartLogUpload(bool isPreviousCrashLog = false) { UploadingLog = true; TopText = M3L.GetString(M3L.string_collectingLogInformation); NamedBackgroundWorker bw = new NamedBackgroundWorker(@"LogUpload"); bw.DoWork += (a, b) => { string logUploadText = LogCollector.CollectLogs(SelectedLog.filepath); using (var output = new MemoryStream()) { var encoder = new LzmaEncodeStream(output); using (var normalBytes = new MemoryStream(Encoding.UTF8.GetBytes(logUploadText))) { int bufSize = 24576, count; var buf = new byte[bufSize]; while ((count = normalBytes.Read(buf, 0, bufSize)) > 0) { encoder.Write(buf, 0, count); } } encoder.Close(); //Upload log to ME3Tweaks var lzmalog = output.ToArray(); try { //this doesn't need to technically be async, but library doesn't have non-async method. string responseString = @"https://me3tweaks.com/modmanager/logservice/logupload.php".PostUrlEncodedAsync(new { LogData = Convert.ToBase64String(lzmalog), ModManagerVersion = App.BuildNumber, CrashLog = isPreviousCrashLog }).ReceiveString().Result; Uri uriResult; bool result = Uri.TryCreate(responseString, UriKind.Absolute, out uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps); if (result) { //should be valid URL. //diagnosticsWorker.ReportProgress(0, new ThreadCommand(SET_DIAGTASK_ICON_GREEN, Image_Upload)); //e.Result = responseString; Log.Information(@"Result from server for log upload: " + responseString); b.Result = responseString; return; } else { Log.Error(@"Error uploading log. The server responded with: " + responseString); b.Result = M3L.GetString(M3L.string_interp_serverRejectedTheUpload, responseString); } } catch (AggregateException e) { Exception ex = e.InnerException; string exmessage = ex.Message; b.Result = M3L.GetString(M3L.string_interp_logWasUnableToUpload, exmessage); } catch (FlurlHttpTimeoutException) { // FlurlHttpTimeoutException derives from FlurlHttpException; catch here only // if you want to handle timeouts as a special case Log.Error(@"Request timed out while uploading log."); b.Result = M3L.GetString(M3L.string_interp_requestTimedOutUploading); } catch (Exception ex) { // ex.Message contains rich details, inclulding the URL, verb, response status, // and request and response bodies (if available) Log.Error(@"Handled error uploading log: " + App.FlattenException(ex)); string exmessage = ex.Message; var index = exmessage.IndexOf(@"Request body:"); if (index > 0) { exmessage = exmessage.Substring(0, index); } b.Result = M3L.GetString(M3L.string_interp_logWasUnableToUpload, exmessage); } } }; bw.RunWorkerCompleted += (a, b) => { if (b.Result is string response) { if (response.StartsWith(@"http")) { Utilities.OpenWebpage(response); } else { OnClosing(DataEventArgs.Empty); var res = M3L.ShowDialog(Window.GetWindow(this), response, M3L.GetString(M3L.string_logUploadFailed), MessageBoxButton.OK, MessageBoxImage.Error); return; } } OnClosing(DataEventArgs.Empty); }; bw.RunWorkerAsync(); }
public static int Main(string[] args) { bool showHelp = false; bool encodeIds = true; int length = 0; string filename = ""; string compression = "gzip"; string mode = ""; string prefix = ""; string suffix = ".qfq"; int cutoff = -1; bool encodeQualities = true; var p = new OptionSet() { { "m|mode=", "the mode: encode|decode", v => mode = v }, { "l|length=", "the length of the reads", (int v) => length = v }, { "z|compression=", "the compression method to use: none|gzip|lzma - default is gzip", v => compression = v }, { "p|prefix=", "the prefix for the output file", v => prefix = v }, { "i|noId", "do not encode/decode Ids", v => encodeIds = v == null }, { "c|cutoff=", "cutoff to be used when encoding losing qualities -\n" + "bases associated with a quality lower than the cutoff will be encoded as\n" + "N", (int v) => cutoff = v }, { "q|noQuality", "do not encode/decode qualities - will use cutoff if given", v => encodeQualities = v == null }, { "h|help", "show this message and exit", v => showHelp = v != null }, }; bool stop = false; List <string> extraArgs = null; string e = ""; try { extraArgs = p.Parse(args); } catch (OptionException oe) { stop = true; e = oe.Message; } if (length <= 0 || mode == "" || (mode == "encode" && prefix == "")) { stop = true; } if (mode == "encode" && cutoff != -1 && encodeQualities) { stop = true; e = "In encode mode -c option can be used only with -q option!"; } if (extraArgs.Count <= 1) { if (extraArgs.Count != 0) { filename = extraArgs[0]; } } else { stop = true; } if (showHelp) { ShowHelp(p); return(0); } if (stop) { Console.WriteLine(e); ShowHelp(p); return(-1); } string outputFile = prefix + suffix; if (compression == "gzip") { outputFile += ".gz"; } else if (compression == "lzma") { outputFile += ".lzma"; } IFastqDeCompresser fdc = new FastqDeCompresser(length, encodeIds, encodeQualities); try { if (mode == "encode") { FileStream outStream = new FileStream(outputFile, FileMode.Create); BinaryWriter writer = null; Stream zipWriter = null; try { if (compression == "none") { writer = new BinaryWriter(outStream); } else if (compression == "gzip") { zipWriter = new GZipOutputStream(outStream); writer = new BinaryWriter(zipWriter); } else if (compression == "lzma") { zipWriter = new LzmaEncodeStream(outStream); writer = new BinaryWriter(zipWriter); } else { Console.Error.WriteLine("Wrong compression method given"); ShowHelp(p); return(-1); } FastqReader reader = null; if (filename != "") { if (cutoff == -1) { reader = new FastqReader(filename, length); } else { reader = new FastqCutoffReader(filename, length, cutoff); } } else { if (cutoff == -1) { reader = new FastqReader(Console.In, length); } else { reader = new FastqCutoffReader(Console.In, length, cutoff); } } fdc.Compress(reader, writer); reader.Close(); } finally { writer.Close(); outStream.Close(); //XXX should close correct streams! } } else if (mode == "decode") { EncodedFastqReader reader = new EncodedFastqReader(File.OpenRead(outputFile), compression); StreamWriter writer = new StreamWriter(Console.OpenStandardOutput()); try { fdc.Decompress(reader, writer); } finally { reader.Close(); writer.Close(); } } else { ShowHelp(p); return(-1); } } catch (InvalidOperationException ioe) { Console.Error.WriteLine(ioe.Message); return(1); } catch (FileNotFoundException fnfe) { Console.Error.WriteLine("File {0} not found {1}!", filename, fnfe.Message); return(1); } /*finally { * // TODO ASK * }*/ return(0); }