public static void DecompressTgzStream(string inputFile, OnFile onFile) { using (FileStream inputStream = File.OpenRead(inputFile)) using (GZipStream tarStream = new GZipStream(inputStream, CompressionMode.Decompress)) { var tarReader = new TarReader(tarStream); while (tarReader.MoveNext(true)) { // Just files if (tarReader.FileInfo.EntryType != EntryType.File && tarReader.FileInfo.EntryType != EntryType.FileObsolete) { continue; } // Save to string and process it using (var memoryStream = new MemoryStream()) { using (var writer = new StreamWriter(memoryStream)) { tarReader.Read(writer.BaseStream); } onFile(Path.GetFileName(tarReader.FileInfo.FileName), Encoding.ASCII.GetString(memoryStream.ToArray())); } } } }
void Traverse(string directory, DirectoryTraverserResult result) { try { var dirInfo = new DirectoryInfo(directory); result.Directories++; OnDirectory?.Invoke(dirInfo); if (OnFile != null) { foreach (var file in dirInfo.EnumerateFiles()) { OnFile.Invoke(file); } } foreach (var dir in dirInfo.EnumerateDirectories()) { Traverse(Path.Combine(directory, dir.Name), result); } } catch { result.Exceptions++; } }
public void FilterFiles(string directory, Filter filter, OnFile matchHandler, bool recurseSubdirectories) { DirectoryUtils.OnFileHandler fh = (filedata, dir) => { var fileInfo = new FileInfo(dir + "\\" + filedata.cFileName); if (filter(fileInfo)) { matchHandler(fileInfo); } }; DirectoryUtils.TraverseDirectory(directory, fh, null, null, recurseSubdirectories); }
/// <summary> /// Faz uma recorrência com token. /// </summary> public ResponseBase Recurring(String merchantId, String merchantKey, String referenceNum, decimal chargeTotal , String customerId, String token, String processorId, String numberOfInstallments , String chargeInterest, String ipAddress, String action, String startDate , String frequency, String period, String installments, String failureThreshold , String currencyCode) { FillRecurringBase(merchantId, merchantKey, referenceNum, chargeTotal, processorId, numberOfInstallments , chargeInterest, ipAddress, action, startDate, frequency, period, installments , failureThreshold, currencyCode); TransactionDetail detail = this.request.Order.RecurringPayment.TransactionDetail; PayType payType = detail.PayType; OnFile onFile = new OnFile(); payType.OnFile = onFile; onFile.CustomerId = customerId; onFile.Token = token; return(new Utils().SendRequest <TransactionRequest>(this.request, this.Environment)); }
private void _client_OnMessage(object sender, byte[] data) { ((UserToken)_client.ReceiveSocket.UserToken).UnPackage(data, (p) => { try { var msg = SerializeHelper.ProtolBufDeserialize <Message>(p.Content); ActiveTime = DateTime.Now; switch (msg.Protocal) { case (byte)MessageProtocalEnum.RLogin: this.OnLogined?.Invoke(this, Encoding.UTF8.GetString(msg.Data)); break; case (byte)MessageProtocalEnum.RSubscribe: this.OnSubed(Encoding.UTF8.GetString(msg.Data)); break; case (byte)MessageProtocalEnum.File: OnFile?.Invoke(this, msg); break; case (byte)MessageProtocalEnum.Heart: ActiveTime = DateTime.Now; break; default: OnMessage?.Invoke(this, msg); break; } } catch { } }); }
public void WsInite() { ws.OnMessage += (sender, e) => { if (e.Data != null) { try { Message msg = SerializeHelper.Deserialize <Message>(e.Data); if (msg != null) { switch (msg.Protocal) { case (byte)MessageProtocalEnum.Heart: ActiveTime = DateTime.Now; //OnNotice?.Invoke(new Message //{ // Data = Encoding.UTF8.GetBytes("心跳:"+ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")) //}); break; case (byte)MessageProtocalEnum.RLogin: this.OnLogined?.Invoke(this, Encoding.UTF8.GetString(msg.Data)); isLogin = true; OnNotice?.Invoke(msg); break; case (byte)MessageProtocalEnum.RemoteConnect: //var aa = Encoding.UTF8.GetString(msg.Data); OnNotice?.Invoke(msg); break; default: OnMessage?.Invoke(this, msg); break; } } } catch { } } else { Message msgFile = SerializeHelper.ProtolBufDeserialize <Message>(e.RawData); if (msgFile.Protocal == (byte)MessageProtocalEnum.File) { OnFile?.Invoke(this, msgFile.Data, "1"); } if (msgFile.Protocal == (byte)MessageProtocalEnum.FileSlice) { OnFile?.Invoke(this, msgFile.Data, "2"); } OnNotice?.Invoke(new Message { Data = Encoding.UTF8.GetBytes("接收到图片") }); } }; ws.OnError += (sender, e) => { }; ws.OnClose += (sender, e) => { }; // ws.OnOpen += (sender, e) => ws.Send("name:Hi, there!"); }
/// <summary> /// Parses body of the request including form and multi-part form data. /// </summary> /// <param name="request">HTTP request.</param> /// <param name="args">Key-value pairs populated by the form data by this function.</param> /// <param name="onFile"> /// Function called if a file is about to be parsed. The stream is attached to a corresponding <see cref="HttpFile"/>. /// <para>By default, <see cref="MemoryStream"/> is used, but for large files, it is recommended to open <see cref="FileStream"/> directly.</para> /// </param> /// <returns>Name-file pair collection.</returns> public static Dictionary <string, HttpFile> ParseBody(this HttpListenerRequest request, Dictionary <string, string> args, OnFile onFile) { if (request == null) { throw new ArgumentNullException(nameof(request)); } if (args == null) { throw new ArgumentNullException(nameof(args)); } if (onFile == null) { throw new ArgumentNullException(nameof(onFile)); } var files = new Dictionary <string, HttpFile>(); if (request.ContentType.StartsWith("application/x-www-form-urlencoded")) { ParseForm(request, args); } else if (request.ContentType.StartsWith("multipart/form-data")) { files = ParseMultipartForm(request, args, onFile); } else { throw new NotSupportedException("The body content-type is not supported."); } return(files); }
private static (string Name, Stream Value, string FileName, string ContentType) parseSection(Stream source, string boundary, OnFile onFile) { var(n, fn, ct) = readContentDisposition(source); source.ReadByte(); source.ReadByte(); //\r\n (empty row) var dst = String.IsNullOrEmpty(fn) ? new MemoryStream() : onFile(n, fn, ct); if (dst == null) { throw new ArgumentException(nameof(onFile), "The on-file callback must return a stream."); } parseUntillBoundaryEnd(source, dst, boundary); return(n, dst, fn, ct); }
static Dictionary <string, HttpFile> ParseMultipartForm(HttpListenerRequest request, Dictionary <string, string> args, OnFile onFile) { if (request.ContentType.StartsWith("multipart/form-data") == false) { throw new InvalidDataException("Not 'multipart/form-data'."); } var boundary = Regex.Match(request.ContentType, "boundary=(.+)").Groups[1].Value; boundary = "--" + boundary; var files = new Dictionary <string, HttpFile>(); var inputStream = new BufferedStream(request.InputStream); parseUntillBoundaryEnd(inputStream, new MemoryStream(), boundary); while (true) { var(n, v, fn, ct) = parseSection(inputStream, "\r\n" + boundary, onFile); if (String.IsNullOrEmpty(n)) { break; } v.Position = 0; if (!String.IsNullOrEmpty(fn)) { files.Add(n, new HttpFile(fn, v, ct)); } else { args.Add(n, readAsString(v)); } } return(files); }