public override void Flush() { if (string.IsNullOrEmpty(_finalString)) { return; } _finalString = Regex.Replace(_finalString, "/// <.+>", ""); _finalString = Regex.Replace(_finalString, @">[\s\S]*?<", Evaluate); var data = Encoding.UTF8.GetBytes(_finalString); if (_compression.Equals("deflate")) { data = Deflate.Compress(data); } else { data = GZip.Compress(data); } _stream.Write(data, 0, data.Length); _finalString = ""; }
protected void Write(Stream stream, SocketMessager messager) { MemoryStream ms = new MemoryStream(); byte[] buff = Encoding.UTF8.GetBytes(messager.GetCanParseString()); ms.Write(buff, 0, buff.Length); if (messager.Arg != null) { buff = Deflate.Compress(BaseSocket.Serialize(messager.Arg)); ms.Write(buff, 0, buff.Length); } this.Write(stream, ms.ToArray()); ms.Close(); }
public override void OnActionExecuted(HttpActionExecutedContext actionContext) { var content = actionContext.Response.Content; var sourceBytes = content == null ? null : content.ReadAsByteArrayAsync().Result; var zipContent = sourceBytes == null ? new byte[0] : Deflate.Compress(sourceBytes); actionContext.Response.Content = new ByteArrayContent(zipContent); actionContext.Response.Content.Headers.Remove("Content-Type"); actionContext.Response.Content.Headers.Add("Content-encoding", "deflate"); //actContext.Response.Content.Headers.Add("Content-Type", "application/json"); actionContext.Response.Content.Headers.Add("Content-Type", "application/json;charset=utf-8"); base.OnActionExecuted(actionContext); }
public void Client_DeflateCompress_Server_DeflateDecompress() { Console.WriteLine("用戶端用Deflate壓縮資料→伺服器端解壓縮後回傳結果→驗證解壓縮結果和Client壓縮前是否相同"); var url = "api/test/DeflateDecompression"; var builder = CreateData(); var contentBytes = Encoding.UTF8.GetBytes(builder); var zipContent = Deflate.Compress(contentBytes); var request = new HttpRequestMessage(HttpMethod.Post, url) { Content = new ByteArrayContent(zipContent) }; var response = MsTestHook.Client.SendAsync(request).Result; var result = response.Content.ReadAsStringAsync().Result; Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); Assert.AreEqual(builder, result); }
static void Deflatetest() { BufferFormat fan = new BufferFormat(1000, new FDataExtraHandle((o) => { return(Deflate.Compress(o)); })); fan.AddItem(true); fan.AddItem("abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc"); fan.AddItem(123); byte[] data = fan.Finish(); ReadBytes read = new ReadBytes(data, 4, -1, new RDataExtraHandle((o) => { return(Deflate.Decompress(o)); })); int lengt; int cmd; bool var1; string var2; int var3; if (read.IsDataExtraSuccess && read.ReadInt32(out lengt) && lengt == read.Length && read.ReadInt32(out cmd) && read.ReadBoolean(out var1) && read.ReadString(out var2) && read.ReadInt32(out var3)) { Console.WriteLine("压缩前长度:{0}", read.Data.Length); Console.WriteLine("压缩后长度:{0}", read.Length); Console.WriteLine("This Deflate-> Length:{0} Cmd:{1} var1:{2} var2:{3} var3:{4}", lengt, cmd, var1, var2, var3); } }
/// <summary> /// Writes the binary data to the stream. /// </summary> /// <param name="grf"></param> /// <param name="writer"></param> internal void WriteToBinaryTable(Format grf, BinaryWriter writer) { // Skip deleted files if (IsDeleted) { return; } byte[] buf; // Update new offset DataOffset = (uint)writer.BaseStream.Position; // Either new or changed? if (IsUpdated == false && IsAdded == false) { // Auto-convert to 0x200 compression (deflate) if (grf.Version != 0x200) { // #1: Decompress buf and update length buf = grf.GetFileData(NameHash, true); LengthUnCompressed = (uint)buf.Length; // #2: Compress and update length buf = Deflate.Compress(buf, true); LengthCompressed = (uint)buf.Length; LengthCompressedAlign = (uint)buf.Length; } else { // Get compressed data buf = grf.GetFileData(NameHash, false); } } else { // Added or updated files, load data from origin filepath if (File.Exists(NewFilepath) == false) { throw new Exception("WriteItems(): File of new or updated item not found: " + NewFilepath); } buf = File.ReadAllBytes(NewFilepath); LengthUnCompressed = (uint)buf.Length; buf = Deflate.Compress(buf, true); LengthCompressed = LengthCompressedAlign = (uint)buf.Length; } try { // Check if the buf is compressed if (buf.Length != LengthCompressed && buf.Length != LengthCompressedAlign) { // The buf has to be compressed, so decompress it byte[] bufUncompressed = Deflate.Decompress(buf); // Update length, if decompression seems to be correct if (bufUncompressed.Length == 0 || bufUncompressed.Length != LengthUnCompressed) { // Narf, corrupt file or something like that // Just write it.. //throw new Exception("WriteItems(): Item " + Filepath + ", DataLen missmatch"); } else { // Decompression was succesfull, so update size LengthCompressed = (uint)Deflate.GetCompressedLength(bufUncompressed); } } // Seems like a valid buf, write it writer.Write(buf); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e); } }
/// <summary> /// Writes the grf to the given Filename /// </summary> /// <param name="destinationPath"></param> /// <param name="repack"> </param> /// <returns></returns> public bool WriteGrf(string destinationPath, bool repack) { // Write to temp file string tmpDestinationPath = destinationPath + "tmp"; using (FileStream fs = File.OpenWrite(tmpDestinationPath)) { using (_writer = new BinaryWriter(fs)) { int iLengthUnCompressed; byte[] fileTableDataCompressed; using (var fileTableStream = new MemoryStream()) { _writer.Seek((int)GrfHeaderLen, SeekOrigin.Begin); // Write file binary data & temporary write file table int filesWritten; if (repack || AlwaysRepack) { filesWritten = WriteFileData(fileTableStream); } else { filesWritten = WriteFileDataDirty(fileTableStream); } // Save the offset after writing binary data var thisPos = (int)_writer.BaseStream.Position; // Write grf header _writer.Seek(0, SeekOrigin.Begin); foreach (var c in MagicHeader) { _writer.Write((byte)c); // header (15) } foreach (var c in AllowEncrypt) { _writer.Write((byte)c); // encrypt (15) } _writer.Write((uint)(thisPos - GrfHeaderLen)); // tableOffset _writer.Write(_filecountNumber1); _writer.Write((uint)(filesWritten + _filecountNumber1 + 7)); // number2 // Always default version Version = GrfDefaultVersion; _writer.Write(Version); // GRF Version _writer.Seek(thisPos, SeekOrigin.Begin); // Compress file table data iLengthUnCompressed = (int)fileTableStream.Length; fileTableDataCompressed = Deflate.Compress(fileTableStream.ToArray(), true); } // Write length and data _writer.Write(fileTableDataCompressed.Length); // compressed _writer.Write(iLengthUnCompressed); // uncompressed _writer.Write(fileTableDataCompressed, 0, fileTableDataCompressed.Length); // data itself } } // If we want to overwrite the previous opened GRF, close it first if (_filepath == destinationPath) { Flush(); } // Ensure nothing blocks the move File.Delete(destinationPath); // Move it finally File.Move(destinationPath + "tmp", destinationPath); // Fore clean up GC.Collect(); return(true); }