/// <inheriteddoc /> public HttpResponseBase Prefix(IEnumerable <byte> data) { lock (this._SYNC) { byte[] dataArray = CollectionHelper.AsArray(data); if (dataArray != null && dataArray.Length > 0) { using (MemoryStream backup = new MemoryStream()) { // backup this.Stream.Position = 0; IOHelper.CopyTo(this.Stream, backup); this.Clear(); this.Stream.Write(dataArray, 0, dataArray.Length); // restore backup.Position = 0; IOHelper.CopyTo(backup, this.Stream); } } } return(this); }
public Zip SetStream(Stream stream) { var ms = new MemoryStream(); var buf = new Byte[3]; stream.Read(buf, 0, buf.Length); stream.Position -= 3; // 仅支持Gzip压缩,可用7z软件先压缩为gz格式 if (buf[0] == 0x1F & buf[1] == 0x8B && buf[2] == 0x08) { IOHelper.DecompressGZip(stream, ms); } else { IOHelper.CopyTo(stream, ms); } ms.Position = 0; Stream = ms; Index_Set = GetUInt32(); Index_End = GetUInt32(); Index_Count = (Index_End - Index_Set) / 7u + 1u; return(this); }
/// <summary> /// /// </summary> /// <see cref="KeyValuePairConfigRepository.OnUpdated()" /> protected override void OnUpdated() { base.OnUpdated(); using (MemoryStream temp = new MemoryStream()) { using (StreamWriter writer = new StreamWriter(temp, this.GetEncoding())) { using (JsonTextWriter jsonWriter = new JsonTextWriter(writer)) { JsonSerializer serializer = this.CreateJsonSerializer(); serializer.Serialize(jsonWriter, this._CONFIG_VALUES, typeof(global::System.Collections.Generic.IDictionary <string, object>)); jsonWriter.Flush(); FileInfo jsonFile = new FileInfo(this.FilePath); if (jsonFile.Exists) { jsonFile.Delete(); jsonFile.Refresh(); } temp.Position = 0; using (FileStream jsonFileStream = jsonFile.OpenWrite()) { IOHelper.CopyTo(temp, jsonFileStream); } } } } }
/// <summary> /// Downloads a file from this server. /// </summary> /// <param name="filePath">The full path from where the file should be downloaded.</param> /// <param name="target">The stream where to write the downloaded data to.</param> /// <exception cref="ArgumentException"> /// <paramref name="filePath" /> is invalid. /// </exception> /// <exception cref="ArgumentNullException"> /// <paramref name="filePath" /> and/or <paramref name="target" /> /// are <see langword="null" />. /// </exception> /// <exception cref="FileNotFoundException"> /// <paramref name="filePath" /> does not exist. /// </exception> /// <exception cref="IOException"> /// <paramref name="target" /> cannot be written. /// </exception> public void DownloadFile(IEnumerable <char> filePath, Stream target) { string path = StringHelper.AsString(filePath); if (path == null) { throw new ArgumentNullException("filePath"); } path = path.Trim(); if (path == string.Empty) { throw new ArgumentException("filePath"); } if (target == null) { throw new ArgumentNullException("data"); } if (target.CanWrite == false) { throw new IOException(); } HttpWebRequest httpRequest = this.Server.CreateHttpRequest(this.GetBaseUri()); httpRequest.Method = "GET"; if (path != null) { httpRequest.Headers["X-MJKTM-CloudNET-File"] = path; } HttpWebResponse httpResponse; try { httpResponse = (HttpWebResponse)httpRequest.GetResponse(); } catch (WebException wex) { HttpWebResponse resp = wex.Response as HttpWebResponse; if (resp != null) { if (resp.StatusCode == HttpStatusCode.NotFound) { throw new FileNotFoundException(path ?? string.Empty); } } throw; } using (Stream stream = httpResponse.GetResponseStream()) { IOHelper.CopyTo(stream, target); } }
/// <summary>释放文件</summary> /// <param name="asm"></param> /// <param name="fileName"></param> /// <param name="destFile"></param> /// <param name="overWrite"></param> public static void ReleaseFile(this Assembly asm, String fileName, String destFile = null, Boolean overWrite = false) { if (fileName.IsNullOrEmpty()) { return; } if (asm == null) { asm = Assembly.GetCallingAssembly(); } var stream = GetFileResource(asm, fileName); if (stream == null) { throw new ArgumentException("filename", String.Format("在程序集{0}中无法找到名为{1}的资源!", asm.GetName().Name, fileName)); } if (destFile.IsNullOrEmpty()) { destFile = fileName; } destFile = destFile.GetFullPath(); if (File.Exists(destFile) && !overWrite) { return; } //var path = Path.GetDirectoryName(dest); //if (!path.IsNullOrWhiteSpace() && !Directory.Exists(path)) Directory.CreateDirectory(path); destFile.EnsureDirectory(true); try { if (File.Exists(destFile)) { File.Delete(destFile); } using (var fs = File.Create(destFile)) { IOHelper.CopyTo(stream, fs); } } catch { } finally { stream.Dispose(); } }
/// <summary> /// /// </summary> /// <see cref="IHttpRequest.GetBodyData()" /> public byte[] GetBodyData() { using (Stream stream = this.GetBody()) { if (stream != null) { using (MemoryStream temp = new MemoryStream()) { IOHelper.CopyTo(stream, temp); return(temp.ToArray()); } } } return(null); }
/// <summary> /// Uploads a file to this server. /// </summary> /// <param name="filePath">The full path where the file should be stored.</param> /// <param name="data">The data to upload.</param> /// <exception cref="ArgumentException"> /// <paramref name="filePath" /> is invalid. /// </exception> /// <exception cref="ArgumentNullException"> /// <paramref name="filePath" /> and/or <paramref name="data" /> /// are <see langword="null" />. /// </exception> /// <exception cref="IOException"> /// <paramref name="data" /> cannot be read. /// </exception> public void UploadFile(IEnumerable <char> filePath, Stream data) { string path = StringHelper.AsString(filePath); if (path == null) { throw new ArgumentNullException("filePath"); } path = path.Trim(); if (path == string.Empty) { throw new ArgumentException("filePath"); } if (data == null) { throw new ArgumentNullException("data"); } if (data.CanRead == false) { throw new IOException(); } HttpWebRequest httpRequest = this.Server.CreateHttpRequest(this.GetBaseUri()); httpRequest.Method = "PUT"; httpRequest.SendChunked = true; if (path != null) { httpRequest.Headers["X-MJKTM-CloudNET-File"] = path; } using (Stream stream = httpRequest.GetRequestStream()) { IOHelper.CopyTo(data, stream); stream.Close(); } httpRequest.GetResponse().Close(); }
/// <summary> /// /// </summary> /// <see cref="KeyValuePairConfigRepository.OnUpdated()" /> protected override void OnUpdated() { base.OnUpdated(); using (MemoryStream temp = new MemoryStream()) { using (StreamWriter writer = new StreamWriter(temp, this.GetEncoding())) { foreach (KeyValuePair <string, IDictionary <string, object> > categoryValues in this._CONFIG_VALUES) { // create section writer.WriteLine("[{0}]", StringHelper.AsString(this.ParseIniSectionName(categoryValues.Key))); // writes values foreach (KeyValuePair <string, object> item in categoryValues.Value) { writer.WriteLine(string.Format("{0}={1}", StringHelper.AsString(this.ParseIniSectionKey(item.Key)), StringHelper.AsString(this.ToIniSectionValue(item.Value)))); } writer.WriteLine(); } writer.Flush(); FileInfo iniFile = new FileInfo(this.FilePath); if (iniFile.Exists) { iniFile.Delete(); iniFile.Refresh(); } temp.Position = 0; using (FileStream iniFileStream = iniFile.OpenWrite()) { IOHelper.CopyTo(temp, iniFileStream); } } } }
// Protected Methods (1) /// <inheriteddoc /> protected override sealed void OnHash(Stream srcStream, Stream targetStream) { byte[] hash; using (MemoryStream temp = new MemoryStream()) { IOHelper.CopyTo(srcStream, temp); if (this._SALT != null) { // use salt temp.Write(this._SALT, 0, this._SALT.Length); } temp.Position = 0; hash = this._ALGORITHM .ComputeHash(temp); } targetStream.Write(hash, 0, hash.Length); }
/// <summary>释放文件夹</summary> /// <param name="asm"></param> /// <param name="prefix"></param> /// <param name="dest"></param> /// <param name="overWrite"></param> /// <param name="filenameResolver"></param> public static void ReleaseFolder(this Assembly asm, String prefix, String dest, Boolean overWrite = false, Func <String, String> filenameResolver = null) { if (asm == null) { asm = Assembly.GetCallingAssembly(); } // 找到符合条件的资源 var names = asm.GetManifestResourceNames(); if (names == null || names.Length < 1) { return; } IEnumerable <String> ns = null; if (prefix.IsNullOrWhiteSpace()) { ns = names.AsEnumerable(); } else { ns = names.Where(e => e.StartsWithIgnoreCase(prefix)); } if (String.IsNullOrEmpty(dest)) { dest = AppDomain.CurrentDomain.BaseDirectory; } dest = dest.GetFullPath(); // 开始处理 foreach (var item in ns) { var stream = asm.GetManifestResourceStream(item); // 计算filename String filename = null; // 去掉前缀 if (filenameResolver != null) { filename = filenameResolver(item); } if (String.IsNullOrEmpty(filename)) { filename = item; if (!String.IsNullOrEmpty(prefix)) { filename = filename.Substring(prefix.Length); } if (filename[0] == '.') { filename = filename.Substring(1); } var ext = Path.GetExtension(item); filename = filename.Substring(0, filename.Length - ext.Length); filename = filename.Replace(".", @"\") + ext; filename = Path.Combine(dest, filename); } if (File.Exists(filename) && !overWrite) { return; } //var path = Path.GetDirectoryName(filename); //if (!path.IsNullOrWhiteSpace() && !Directory.Exists(path)) Directory.CreateDirectory(path); filename.EnsureDirectory(true); try { if (File.Exists(filename)) { File.Delete(filename); } using (var fs = File.Create(filename)) { IOHelper.CopyTo(stream, fs); } } catch { } finally { stream.Dispose(); } } }