public override ScenarioInstance <ICompressor> CreateInstance() { var compressor = new GzipCompressor(); var instance = new ScenarioInstance <ICompressor>(compressor); return(instance); }
public static void CompressorTest(string[] args) { MemoryDuplex memory = new MemoryDuplex(); GzipCompressor c = new GzipCompressor(); c.Pipe(memory); GzipDecompressor d = new GzipDecompressor(); memory.Pipe(d); d.OnData += (data) => Console.WriteLine("data {0}", data.Stringify()); c.Write(new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }); c.Finish(); d.Finish(); Console.ReadKey(); }
/// <summary> /// Сжать файл. /// </summary> /// <param name="sourceFilename">Имя исходного файла.</param> /// <param name="archiveFilename">Имя файла архива.</param> /// <returns>В случае успеха функция возвращает 0, при ошибке 1.</returns> private static int ExecuteCompress(string sourceFilename, string archiveFilename) { if (!File.Exists(sourceFilename)) { Console.WriteLine("File not found:" + sourceFilename); return(1); } int errorType; using (GzipCompressor compressor = new GzipCompressor()) { errorType = compressor.PackFile(new ArchiveSettings(sourceFilename, archiveFilename)); } return(errorType); }
public void OneThreadCompressorTest() { string sourceFilename = TestFolder.File("Emgu.chm"); string archiveFilename = "!pack.Emgu.1_thread.gz"; int errorType; using (GzipCompressor compressor = new GzipCompressor()) { errorType = compressor.PackFile(new ArchiveSettings(sourceFilename, archiveFilename) { ThreadCount = 1 }); } Assert.AreEqual(errorType, 0); FileInfo fileInfo = new FileInfo(archiveFilename); Assert.AreEqual(fileInfo.Length, 11810196); }
public void Compress(CompressionType compressionType) { if (IsSet) { throw new InvalidOperationException("Explicitly setting compression type must be performed before headers get sent"); } Compression = compressionType; switch (compressionType) { case CompressionType.None: ContentTransform = new RawUnifiedDuplex(); IsCompressionSet = false; break; case CompressionType.Gzip: ContentTransform = new GzipCompressor(CompressionLevel); IsCompressionSet = true; break; case CompressionType.Deflate: ContentTransform = new DeflateCompressor(CompressionLevel); IsCompressionSet = true; break; case CompressionType.Compress: throw new NotImplementedException("Got Compress, an unimplemented compression, as compression type"); default: throw new ArgumentException("Got Unknown as compression type"); } }
public bool TrySetFor(MessageHead head) { ThrowIfEnded(); BodyType?bodyType = BodyType.TryDetectFor(head); if (bodyType == null) { return(false); } CompressionType compression = bodyType.Value.CompressionType; TransferEncoding transfer = bodyType.Value.TransferEncoding; int contentLength = bodyType.Value.ContentLength; if (IsSet) { Finish(); } IsSet = true; TransferEncoding = transfer; ContentLength = contentLength; if (Compression != CompressionType.Unknown) { return(true); } Compression = compression; switch (compression) { case CompressionType.None: ContentTransform = new RawUnifiedDuplex(); IsCompressionSet = false; break; case CompressionType.Gzip: ContentTransform = new GzipCompressor(CompressionLevel); IsCompressionSet = true; break; case CompressionType.Deflate: ContentTransform = new DeflateCompressor(CompressionLevel); IsCompressionSet = true; break; case CompressionType.Compress: return(false); default: return(false); } return(true); }
public void GzipCompressor_Does_Actually_Compress() { var compressor = new GzipCompressor(); var source = Encoding.GetEncoding("utf-16").GetBytes(_fixture.TestString); byte[] gzipBytes = null; using (var input = new MemoryStream(source)) using (var output = new MemoryStream()) { compressor.Compress(input, output); var sourceBytes = input.ToArray(); gzipBytes = output.ToArray(); Assert.Equal(source, sourceBytes); Assert.True(sourceBytes.Length > gzipBytes.Length); Assert.NotEqual(sourceBytes, gzipBytes); var gzipString = Encoding.UTF8.GetString(gzipBytes); Assert.NotEqual(_fixture.TestString, gzipString); } using (var input = new MemoryStream(gzipBytes)) using (var output = new MemoryStream()) { compressor.Decompress(input, output); var gzippedBytes = input.ToArray(); var sourceBytes = output.ToArray(); Assert.Equal(gzipBytes, gzippedBytes); Assert.True(gzippedBytes.Length < sourceBytes.Length); Assert.NotEqual(gzippedBytes, sourceBytes); Assert.Equal(source, sourceBytes); var sourceString = Encoding.GetEncoding("utf-16").GetString(sourceBytes); Assert.Equal(_fixture.TestString, sourceString); } }