public static void Archive(string infilename, string outfilename, UtlFileAccess storage, int compressionType) { GZipStream stream = null; bool flag = false; if (storage.IsStreamElement(infilename)) { try { byte[] buffer = new byte[0x10000]; using (Stream stream2 = storage.OpenInputStreamElement(infilename)) { using (Stream stream3 = storage.OpenOutputStreamElement(outfilename)) { int num; Stream stream4 = null; if (compressionType != 0) { if ((compressionType - 1) > 1) { throw new Exception("FileArchiver" + compressionType); } } else { stream4 = stream3; goto Label_0061; } stream = new GZipStream(stream3, CompressionMode.Compress); Label_0061: num = stream2.Read(buffer, 0, buffer.Length); if (num != 0) { stream4.Write(buffer, 0, num); goto Label_0061; } if (stream != null) { stream.Flush(); } } } flag = true; } catch (Exception exception) { try { if (!flag && storage.IsStreamElement(outfilename)) { storage.RemoveElement(outfilename); } } catch (Exception) { } throw new IOException(exception.Message, exception); } } }
public static object Decompress(byte[] compressedData) { object result = null; using (MemoryStream memory = new MemoryStream()) { memory.Write(compressedData, 0, compressedData.Length); memory.Position = 0L; using (GZipStream zip = new GZipStream(memory, CompressionMode.Decompress, true)) { zip.Flush(); BinaryFormatter formatter = new BinaryFormatter(); result = formatter.Deserialize(zip); } } return(result); }
/// <summary> /// Serializes a XML file to EVEMon.Common\Resources. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="datafile">The datafile.</param> /// <param name="filename">The filename.</param> internal static void SerializeXml <T>(T datafile, string filename) { string path = Path.Combine(GetSolutionDirectory(), @"src\EVEMon.Common\Resources", filename); FileStream stream = Common.Util.GetFileStream(path, FileMode.Create, FileAccess.Write); using (GZipStream zstream = new GZipStream(stream, CompressionMode.Compress)) { XmlSerializer serializer = new XmlSerializer(typeof(T)); serializer.Serialize(zstream, datafile); zstream.Flush(); } Console.WriteLine(@"-----------------------------------------------"); Console.WriteLine(@"Updated : {0}", filename); Console.WriteLine(@"-----------------------------------------------"); // As long as EVEMon.Common is not rebuilt, files are not updated in output directories Copy(path, Path.Combine(GetSolutionDirectory(), @"src\EVEMon.Common\", GetOutputPath(), "Resources", filename)); // Update the file in the settings directory string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); Copy(path, Path.Combine(appData, "EVEMon", filename)); Console.WriteLine(); }
static public byte[] ToSerialized(System.IntPtr model, int cols, int maxClasses, bool compress) { SVMModelRepresentation smr = new SVMModelRepresentation(); smr.cols = cols; smr.maxClasses = maxClasses; smr.means = new double[cols]; smr.sdevs = new double[cols]; getScales(model, smr.means, smr.sdevs); smr.modelString = saveModelString(model); IFormatter formatter = new BinaryFormatter(); MemoryStream memoryStream = new MemoryStream(); if (compress) { using (GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Compress)) { formatter.Serialize(gzipStream, smr); gzipStream.Flush(); } } else { formatter.Serialize(memoryStream, smr); } return(memoryStream.ToArray()); }
/// <summary> /// Comprimir un Stream en formato GZIP /// </summary> /// <param name="stream"></param> /// <returns></returns> public static void Compress(Stream inStream, Stream outStream) { if (inStream == null) { throw new ArgumentNullException("inStream"); } if (outStream == null) { throw new ArgumentNullException("outStream"); } byte[] bytes = null; int n = 0; inStream.Position = 0; using (GZipStream stGzip = new GZipStream(outStream, CompressionMode.Compress, true)) { bytes = new byte[8192]; while ((n = inStream.Read(bytes, 0, bytes.Length)) > 0) { stGzip.Write(bytes, 0, n); } stGzip.Flush(); } }
public static void WriteGzip(XmlDocument theDoc, Stream theStream) { MemoryStream ms = new MemoryStream(); XmlWriterSettings xmlSettings = new XmlWriterSettings(); xmlSettings.Encoding = Encoding.UTF8; xmlSettings.ConformanceLevel = ConformanceLevel.Document; xmlSettings.Indent = false; xmlSettings.NewLineOnAttributes = false; xmlSettings.CheckCharacters = true; xmlSettings.IndentChars = ""; XmlWriter tw = XmlWriter.Create(ms, xmlSettings); theDoc.WriteTo(tw); tw.Flush(); tw.Close(); byte[] buffer = ms.GetBuffer(); GZipStream compressedzipStream = new GZipStream(theStream, CompressionMode.Compress, true); compressedzipStream.Write(buffer, 0, buffer.Length); // Close the stream. compressedzipStream.Flush(); compressedzipStream.Close(); // Force a flush theStream.Flush(); }
public ManagedBitmap(string fileName) { try { byte[] buffer = new byte[1024]; MemoryStream fd = new MemoryStream(); Stream fs = File.OpenRead(fileName); using (Stream csStream = new GZipStream(fs, CompressionMode.Decompress)) { int nRead; while ((nRead = csStream.Read(buffer, 0, buffer.Length)) > 0) { fd.Write(buffer, 0, nRead); } csStream.Flush(); buffer = fd.ToArray(); } Width = buffer[4] << 8 | buffer[5]; Height = buffer[6] << 8 | buffer[7]; _colors = new Color[Width * Height]; int start = 8; for (int i = 0; i < _colors.Length; i++) { _colors[i] = Color.FromArgb(buffer[start], buffer[start + 1], buffer[start + 2], buffer[start + 3]); start += 4; } } catch { LoadedOk = false; } }
/// <summary> /// Schreibt einen Stream von Daten in eine Datei /// </summary> /// <param name="data">Die Daten</param> /// <param name="filepath">Der Pfad</param> private void WriteFile(MemoryStream data, string filepath) { data.Position = 0; using (FileStream fstream = File.Create(filepath)) { Console.WriteLine(fstream.CanWrite); //Wenn in den Einstellungen bei COMPRESS TRUE angegeben wurde, //wird die Datei komprimiert gespeichert und gelesen if (Settings.Default.COMPRESS) { using (GZipStream gzstream = new GZipStream(fstream, CompressionMode.Compress)) { data.CopyTo(gzstream); gzstream.Flush(); fstream.Flush(); } } else { Console.WriteLine(data.Length); data.CopyTo(fstream); Console.Write(fstream.Length); fstream.Flush(); } } }
private void RecreateWebRequest() { // we now need to clone the request, since just calling GetRequest again wouldn't do anything var newWebRequest = CreateRequest(); HttpRequestHelper.CopyHeaders(WebRequest, newWebRequest); if (postedToken != null) { WriteToken(newWebRequest); } if (postedData != null) { Write(postedData); } if (postedStream != null) { postedStream.Position = 0; using (var stream = newWebRequest.GetRequestStream()) using (var compressedData = new GZipStream(stream, CompressionMode.Compress)) { postedStream.CopyTo(compressedData); stream.Flush(); compressedData.Flush(); } } WebRequest = newWebRequest; }
private static MemoryStream CompressGZip(Stream outStream) { byte[] buffer = new byte[1024 * 1000]; int bytesRead; string tempFile = Path.GetTempFileName(); try { using (FileStream compressed = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.None)) { using (GZipStream compressedStream = new GZipStream(compressed, CompressionMode.Compress)) { while ((bytesRead = outStream.Read(buffer, 0, buffer.Length)) > 0) { compressedStream.Write(buffer, 0, bytesRead); } compressedStream.Flush(); } } return(new MemoryStream(File.ReadAllBytes(tempFile))); } finally { File.Delete(tempFile); } }
public void GZipStreamTest() { string testString = "Some testing string to compress/decompress using the GzipStream object!"; // compress. var testStringBytes = ASCIIEncoding.ASCII.GetBytes(testString); var compressedStream = new MemoryStream(); var stream = new GZipStream(compressedStream, CompressionMode.Compress); stream.Write(testStringBytes, 0, testStringBytes.Length); stream.Flush(); byte[] compressedTestString = compressedStream.ToArray(); // decompress. compressedStream = new MemoryStream(compressedTestString); var decompressiongStream = new GZipStream(compressedStream, CompressionMode.Decompress); var decompressedStream = new MemoryStream(); decompressiongStream.CopyTo(decompressedStream); var decompressedTestString = new byte[decompressedStream.Length]; decompressedStream.Read(decompressedTestString, 0, decompressedTestString.Length); string resultingTestString = ASCIIEncoding.ASCII.GetString(decompressedTestString); }
/// <summary> /// 压缩字符串 /// </summary> public static byte[] CompressString(string source) { if (string.IsNullOrEmpty(source)) { return(null); } if (source.Length > 128) { MemoryStream destinationStream = new MemoryStream(); using (GZipStream gzip = new GZipStream(destinationStream, CompressionMode.Compress)) { byte[] buf = Encoding.UTF8.GetBytes(source); gzip.Write(buf, 0, buf.Length); gzip.Flush(); } return(destinationStream.ToArray()); } else { byte[] temp = Encoding.UTF8.GetBytes(source); byte[] result = new byte[temp.Length + 4]; result[0] = 0x0; result[1] = 0x0; result[2] = 0x0; result[3] = 0x0; for (int i = 0; i < temp.Length; i++) { result[i + 4] = temp[i]; } return(result); } }
internal ArraySegment <byte> Compress(byte[] payloadBuffer) { using (var originStream = new MemoryStream(payloadBuffer, 0, payloadBuffer.Length)) { using (MemoryStream memory = new MemoryStream()) { using (GZipStream gZipStream = new GZipStream(memory, CompressionLevel.Optimal, leaveOpen: true)) { byte[] buffer = SimpleBufferManager.Shared.TakeBuffer(DefaultBufferSize); try { int read; while ((read = originStream.Read(buffer, 0, DefaultBufferSize)) != 0) { gZipStream.Write(buffer, 0, read); } gZipStream.Flush(); } finally { SimpleBufferManager.Shared.ReturnBuffer(buffer); } } return(new ArraySegment <byte>(memory.GetBuffer(), 0, (int)memory.Length)); } } }
//------------------------------------------------------------------------------------------ /// <summary> /// Compress and returns the supplied source bytes-array data. Optionally the use of the GZip algorithm can be specified (default is Deflate). /// NOTE: Usage of the GZip algorithm adds cyclic-redudancy checks, which is well suitted for publishing the result externally (as in files). /// </summary> public static byte[] Compress(byte[] Source, bool AsGZip = false) { using (var Result = new MemoryStream()) { if (AsGZip) { using (var Compressor = new GZipStream(Result, CompressionMode.Compress)) { Compressor.Write(Source, 0, Source.Length); Compressor.Flush(); Compressor.Close(); return(Result.ToArray()); } } else { using (var Compressor = new DeflateStream(Result, CompressionMode.Compress)) { Compressor.Write(Source, 0, Source.Length); Compressor.Flush(); Compressor.Close(); return(Result.ToArray()); } } } }
public static byte[] Decompress(byte[] zLibCompressedBuffer) { byte[] resBuffer; if (zLibCompressedBuffer.Length <= 1) { return(zLibCompressedBuffer); } var mInStream = new MemoryStream(zLibCompressedBuffer); var mOutStream = new MemoryStream(zLibCompressedBuffer.Length); var infStream = new GZipStream(mInStream, CompressionMode.Decompress); mInStream.Position = 0; try { infStream.CopyTo(mOutStream); resBuffer = mOutStream.ToArray(); } finally { infStream.Flush(); mInStream.Flush(); mOutStream.Flush(); } return(resBuffer); }
private byte[] zipLog(string log) { if (String.IsNullOrEmpty(log)) { return(null); } try { using (var mem = new MemoryStream()) { int orgSize = 0; using (var zip = new GZipStream(mem, CompressionMode.Compress)) { var bytes = System.Text.Encoding.UTF8.GetBytes(log); orgSize = bytes.Length; zip.Write(bytes, 0, bytes.Length); zip.Flush(); } var result = mem.ToArray(); LogInfo("compress log from size {0} to {1}, compress ratio is {2}", orgSize, result.Length, result.Length * 1.0 * 100 / orgSize); return(result); } } catch (Exception ex) { LogFatal("cannot compress log", ex); return(System.Text.Encoding.UTF8.GetBytes(log)); } }
private void SendToNewRelicLogs(string body) { ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12; if (!(WebRequest.Create(EndpointUrl) is HttpWebRequest request)) { return; } if (!string.IsNullOrWhiteSpace(LicenseKey)) { request.Headers.Add("X-License-Key", LicenseKey); } else { request.Headers.Add("X-Insert-Key", InsertKey); } request.Headers.Add("Content-Encoding", "gzip"); request.Timeout = 40000; //It's basically fire-and-forget request.Credentials = CredentialCache.DefaultCredentials; request.ContentType = "application/gzip"; request.Accept = "*/*"; request.Method = "POST"; request.KeepAlive = false; var byteStream = Encoding.UTF8.GetBytes(body); try { using (var zippedRequestStream = new GZipStream(request.GetRequestStream(), CompressionMode.Compress)) { zippedRequestStream.Write(byteStream, 0, byteStream.Length); zippedRequestStream.Flush(); zippedRequestStream.Close(); } } catch (WebException ex) { SelfLog.WriteLine("Failed to create WebRequest to NewRelic Logs: {0} {1}", ex.Message, ex.StackTrace); return; } try { using (var response = request.GetResponse() as HttpWebResponse) { if (response == null || response.StatusCode != HttpStatusCode.Accepted) { SelfLog.WriteLine("Self-log: Response from NewRelic Logs is missing or negative: {0}", response?.StatusCode); } } } catch (WebException ex) { SelfLog.WriteLine("Failed to parse response from NewRelic Logs: {0} {1}", ex.Message, ex.StackTrace); } }
private Script GenerateContent() { byte[] ub = null; byte[] cb = null; string scriptText = Generator.GetScript(); using (var ms = new MemoryStream(scriptText.Length)) { using (var sw = new StreamWriter(ms, new UTF8Encoding(true))) { sw.Write(scriptText); sw.Flush(); ub = ms.ToArray(); ms.Seek(0, SeekOrigin.Begin); if (ms.Length > 4096) { using (var cs = new MemoryStream((int)ms.Length)) { using (var gz = new GZipStream(cs, CompressionMode.Compress)) { ms.CopyTo(gz); gz.Flush(); } cb = cs.ToArray(); } } } var script = new Script { Hash = GetMD5HashString(ub), Time = DateTime.UtcNow, ScriptText = scriptText, CompressedBytes = cb, UncompressedBytes = ub, Expiration = Generator.Expiration == TimeSpan.Zero ? DateTime.MaxValue : DateTime.Now.Add(Generator.Expiration) }; this.content = script; if (Generator.GroupKey == null) { return(script); } TwoLevelCache.GetLocalStoreOnly("DynamicScriptCheck:" + this.Name, Generator.Expiration, Generator.GroupKey, () => { return(new object()); }); return(script); } }
public async Task SmugglerImportStreamShouldThrowTimeout() { var paths = CopyServer(); using (var embedded = new EmbeddedServer()) { embedded.StartServer(new ServerOptions { ServerDirectory = paths.ServerDirectory, DataDirectory = paths.DataDirectory, }); const string databaseName = "test"; var dummyDump = CreateDummyDump(1); using (var ctx = JsonOperationContext.ShortTermSingleUse()) using (var bjro = ctx.ReadObject(dummyDump, "dump")) using (var ms = new MemoryStream()) using (var zipStream = new GZipStream(ms, CompressionMode.Compress)) { await bjro.WriteJsonToAsync(zipStream); zipStream.Flush(); ms.Position = 0; using (var store = embedded.GetDocumentStore(new DatabaseOptions(databaseName))) { var testingStuff = store.Smuggler.ForTestingPurposesOnly(); using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1))) using (testingStuff.CallBeforeSerializeToStreamAsync(() => { Thread.Sleep(2000); })) { Exception e = null; try { var operation = await store.Smuggler.ForDatabase(databaseName) .ImportAsync( new DatabaseSmugglerImportOptions { OperateOnTypes = DatabaseItemType.Documents | DatabaseItemType.Identities | DatabaseItemType.CompareExchange }, ms, cts.Token); await operation.WaitForCompletionAsync(); } catch (Exception exception) { e = exception; } Assert.NotNull(e); AssertException(e); } } } } }
private void StopRecording() { lock (_recordingLock) { _recordingTimer.Stop(); _gzipStreamBinaryWriter.Flush(); _gzipStream.Flush(); _fileStream.Flush(); Common.Util.DisposeObject(_gzipStreamBinaryWriter); Common.Util.DisposeObject(_gzipStream); WriteHeader(); Common.Util.DisposeObject(_fileStream); _gzipStreamBinaryWriter = null; _gzipStream = null; _fileStream = null; } }
public static void CompressGZip(this byte[] @this, Stream output) { using (var stream = new GZipStream(output, CompressionMode.Compress, true)) { stream.Write(@this, 0, @this.Length); stream.Flush(); } }
/// <inheritdoc/> protected override void BaseDecompress(Stream inputStream, Stream outputStream) { using var gZipStream = new GZipStream(inputStream, CompressionMode.Decompress, true); gZipStream.CopyTo(outputStream); outputStream.Flush(); gZipStream.Flush(); }
/// <inheritdoc/> protected override void BaseCompress(Stream inputStream, Stream outputStream) { using var gZipStream = new GZipStream(outputStream, Level, true); inputStream.CopyTo(gZipStream); inputStream.Flush(); gZipStream.Flush(); }
/// <inheritdoc/> protected override void WriteObject(XmlObjectSerializer serializer, Stream stream, object graph) { using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Compress, true)) { base.WriteObject(serializer, gzipStream, graph); gzipStream.Flush(); } }
public static void CompressTo(this Stream stream, Stream outputStream) { using (var gZipStream = new GZipStream(outputStream, CompressionMode.Compress)) { stream.CopyTo(gZipStream); gZipStream.Flush(); } }
public void FlushFailsAfterDispose() { var ms = new MemoryStream(); var ds = new GZipStream(ms, CompressionMode.Compress); ds.Dispose(); Assert.Throws <ObjectDisposedException>(() => { ds.Flush(); }); }
/// <summary> /// Compresses to. /// </summary> /// <param name="input">The input.</param> /// <param name="output">The output.</param> public static void CompressTo(this Stream input, Stream output) { using (GZipStream gz = new GZipStream(output, CompressionMode.Compress, true)) { input.TransfertTo(gz); gz.Flush(); } }
/// <summary> /// !!!Stream is flushed but kept open!!! /// </summary> /// <exception cref="ArgumentException"></exception> /// <param name="cl">The compression level to use</param> /// <param name="data">The data to be used</param> /// <returns>A GZip stream thats open and with a clean buffer</returns> public static GZipStream Compress <T>(this T i, CompressionLevel cl, byte[] data) where T : Stream { var gzs = new GZipStream(i, cl); gzs.Write(data, 0, data.Length); gzs.Flush(); return(gzs); }
public void CheckClosedFlush() { MemoryStream backing = new MemoryStream(); GZipStream compressing = new GZipStream(backing, CompressionMode.Compress); compressing.Close(); compressing.Flush(); }
/// <summary> /// Serializes an IBatch to a XML and compresses it /// </summary> /// <param name="batch">The IBatch to serialize</param> /// <param name="stream">The stream to write the serialized and compressed data to</param> internal static void SerializeCompressedXml(this IBatch batch, Stream stream) { using (var gzipStream = new GZipStream(stream, CompressionMode.Compress, true)) { batch.SerializeUncompressedXml(gzipStream); gzipStream.Flush(); } }
public void FlushFailsAfterDispose() { var ms = new MemoryStream(); var ds = new GZipStream(ms, CompressionMode.Compress); ds.Dispose(); Assert.Throws<ObjectDisposedException>(() => { ds.Flush(); }); }
public async Task Flush() { var ms = new MemoryStream(); var ds = new GZipStream(ms, CompressionMode.Compress); ds.Flush(); await ds.FlushAsync(); // Just ensuring Flush doesn't throw }
public void TestNotSupported() { Stream baseStream = new MemoryStream(this.compressedData); GZipStream gzipStream = new GZipStream(baseStream, CompressionMode.Decompress); try { gzipStream.Write(null, 0, 0); } catch (NotSupportedException) {} try { gzipStream.Flush(); } catch (NotSupportedException) {} try { gzipStream.Seek(0, SeekOrigin.Begin); } catch (NotSupportedException) {} try { gzipStream.SetLength(0); } catch (NotSupportedException) {} }
/// <summary> /// Serializes an IBatch to a JSON and compresses it /// </summary> /// <param name="batch">The IBatch to serialize</param> /// <param name="stream">The stream to write the serialized data to</param> internal static void SerializeCompressedJson(this IBatch batch, Stream stream) { using (var gzipStream = new GZipStream(stream, CompressionMode.Compress, true)) { TextWriter textWriter = new StreamWriter(gzipStream); textWriter.Write(batch.SerializeToJson()); textWriter.Flush(); gzipStream.Flush(); } }