public Write ( byte buffer, int offset, int count ) : void | ||
buffer | byte | Buffer to write |
offset | int | Offset of first byte in buf to write |
count | int | Number of bytes to write |
Résultat | void |
public void TestGZip() { MemoryStream ms = new MemoryStream(); GZipOutputStream outStream = new GZipOutputStream(ms); byte[] buf = new byte[100000]; System.Random rnd = new Random(); rnd.NextBytes(buf); outStream.Write(buf, 0, buf.Length); outStream.Flush(); outStream.Finish(); ms.Seek(0, SeekOrigin.Begin); GZipInputStream inStream = new GZipInputStream(ms); byte[] buf2 = new byte[buf.Length]; int pos = 0; while (true) { int numRead = inStream.Read(buf2, pos, 4096); if (numRead <= 0) { break; } pos += numRead; } for (int i = 0; i < buf.Length; ++i) { Assertion.AssertEquals(buf2[i], buf[i]); } }
public void TestGZip() { MemoryStream ms = new MemoryStream(); GZipOutputStream outStream = new GZipOutputStream(ms); byte[] buf = new byte[100000]; System.Random rnd = new Random(); rnd.NextBytes(buf); outStream.Write(buf, 0, buf.Length); outStream.Flush(); outStream.Finish(); ms.Seek(0, SeekOrigin.Begin); GZipInputStream inStream = new GZipInputStream(ms); byte[] buf2 = new byte[buf.Length]; int currentIndex = 0; int count = buf2.Length; while (true) { int numRead = inStream.Read(buf2, currentIndex, count); if (numRead <= 0) { break; } currentIndex += numRead; count -= numRead; } Assert.AreEqual(0, count); for (int i = 0; i < buf.Length; ++i) { Assert.AreEqual(buf2[i], buf[i]); } }
public static byte[] CtorErrMsg(string msg, NameValueCollection requestParam) { using (MemoryStream ms = new MemoryStream()) { //包总长度 int len = 0; long pos = 0; //包总长度,占位 WriteValue(ms, len); int actionid = Convert.ToInt32(requestParam["actionid"]); //StatusCode WriteValue(ms, 10001); //msgid WriteValue(ms, Convert.ToInt32(requestParam["msgid"])); WriteValue(ms, msg); WriteValue(ms, actionid); WriteValue(ms, "st"); //playerdata WriteValue(ms, 0); //固定0 WriteValue(ms, 0); ms.Seek(pos, SeekOrigin.Begin); WriteValue(ms, (int)ms.Length); using (var gms = new MemoryStream()) using (var gzs = new GZipOutputStream(gms)) { gzs.Write(ms.GetBuffer(), 0, (int)ms.Length); gzs.Flush(); gzs.Close(); return gms.ToArray(); } } }
public void GZip_Compress_Extract_Test() { var plainStream = PlainText.ToStream(); plainStream.Seek(0, SeekOrigin.Begin); var plainData = Encoding.UTF8.GetBytes(PlainText); byte[] compressedData; byte[] extractedData; // Compress using(var compressedStream = new MemoryStream()) using(var gzs = new GZipOutputStream(compressedStream)) { gzs.SetLevel(5); gzs.Write(plainData, 0, plainData.Length); gzs.Finish(); compressedData = compressedStream.ToArray(); } Assert.IsNotNull(compressedData); // Extract using(var compressedStream = new MemoryStream(compressedData)) { // compressedStream.Seek(0, SeekOrigin.Begin); using(var gzs = new GZipInputStream(compressedStream)) using(var extractedStream = new MemoryStream()) { StreamTool.CopyStreamToStream(gzs, extractedStream); extractedData = extractedStream.ToArray(); } } Assert.IsNotNull(extractedData); string extractedText = Encoding.UTF8.GetString(extractedData).TrimEnd('\0'); Assert.AreEqual(PlainText, extractedText); }
public void TestGZip() { MemoryStream ms = new MemoryStream(); GZipOutputStream outStream = new GZipOutputStream(ms); byte[] buf = new byte[100000]; System.Random rnd = new Random(); rnd.NextBytes(buf); outStream.Write(buf, 0, buf.Length); outStream.Flush(); outStream.Finish(); ms.Seek(0, SeekOrigin.Begin); GZipInputStream inStream = new GZipInputStream(ms); byte[] buf2 = new byte[buf.Length]; int pos = 0; while (true) { int numRead = inStream.Read(buf2, pos, 4096); if (numRead <= 0) { break; } pos += numRead; } for (int i = 0; i < buf.Length; ++i) { Assertion.AssertEquals(buf2[i], buf[i]); } }
/// <summary> /// 지정된 데이타를 압축한다. /// </summary> /// <param name="input">압축할 Data</param> /// <returns>압축된 Data</returns> public override byte[] Compress(byte[] input) { if(IsDebugEnabled) log.Debug(CompressorTool.SR.CompressStartMsg); // check input data if(input.IsZeroLength()) { if(IsDebugEnabled) log.Debug(CompressorTool.SR.InvalidInputDataMsg); return CompressorTool.EmptyBytes; } byte[] output; using(var compressedStream = new MemoryStream(input.Length)) { using(var gzs = new GZipOutputStream(compressedStream)) { gzs.SetLevel(ZipLevel); gzs.Write(input, 0, input.Length); gzs.Finish(); } output = compressedStream.ToArray(); } if(IsDebugEnabled) log.Debug(CompressorTool.SR.CompressResultMsg, input.Length, output.Length, output.Length / (double)input.Length); return output; }
/// <summary> /// 压缩文件为gz文件 /// </summary> /// <param name="sourcefilename">压缩的文件路径</param> /// <param name="zipfilename">压缩后的gz文件路径</param> /// <returns></returns> public static bool Compress(string sourcefilename, string zipfilename) { bool blResult;//表示压缩是否成功的返回结果 //为源文件创建读取文件的流实例 FileStream srcFile = File.OpenRead(sourcefilename); //为压缩文件创建写入文件的流实例 GZipOutputStream zipFile = new GZipOutputStream(File.Open(zipfilename, FileMode.Create)); try { byte[] FileData = new byte[srcFile.Length];//创建缓冲数据 srcFile.Read(FileData, 0, (int)srcFile.Length);//读取源文件 zipFile.Write(FileData, 0, FileData.Length);//写入压缩文件 blResult = true; return blResult; } catch { blResult = false; return blResult; } finally { srcFile.Close();//关闭源文件 zipFile.Close();//关闭压缩文件 } }
/// <summary> /// Compresses byte array using gzip /// Compressed data is returned as a byte array /// </summary> /// <param name="inBytes">The bytes to compress</param> public static byte[] compress_gzip(byte[] inBytes) { MemoryStream ContentsGzippedStream = new MemoryStream(); //create the memory stream to hold the compressed file Stream s = new GZipOutputStream(ContentsGzippedStream); //create the gzip filter s.Write(inBytes, 0, inBytes.Length); //write the file contents to the filter s.Flush(); //make sure everythings ready s.Close(); //close and write the compressed data to the memory stream return ContentsGzippedStream.ToArray(); }
/// <summary> /// 压缩byte数组 /// </summary> /// <param name="inBytes">需要压缩的byte数组</param> /// <returns>压缩好的byte数组</returns> public static byte[] CompressByte(byte[] inBytes) { MemoryStream outStream = new MemoryStream(); Stream zipStream = new GZipOutputStream(outStream); zipStream.Write(inBytes, 0, inBytes.Length); zipStream.Close(); byte[] outData = outStream.ToArray(); outStream.Close(); return outData; }
/// <summary> /// 压缩字节数组 /// 返回:已压缩的字节数组 /// </summary> /// <param name="bytData">待压缩的字节数组</param> /// <returns></returns> public static byte[] CompressBytes(byte[] data) { MemoryStream o = new MemoryStream(); Stream s = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(o); s.Write(data, 0, data.Length); s.Close(); o.Flush(); o.Close(); return o.ToArray(); }
/// <summary> /// 压缩字节数组 /// </summary> /// <param name="str"></param> public static byte[] ByteCompress(byte[] inputBytes) { MemoryStream ms = new MemoryStream(); GZipOutputStream gzip = new GZipOutputStream(ms); gzip.Write(inputBytes, 0, inputBytes.Length); gzip.Close(); byte[] press = ms.ToArray(); return(press); }
public static byte[] returnZippedbyteArray(string stringToZip) { MemoryStream memStream = new MemoryStream(); Stream compressedStream = new GZipOutputStream(memStream); byte[] byteArrayToZip = Encoding.UTF8.GetBytes(stringToZip.ToCharArray()); compressedStream.Write(byteArrayToZip ,0,byteArrayToZip.Length); compressedStream.Flush(); compressedStream.Close(); return memStream.ToArray(); }
public static byte[] Compression(byte[] data) { MemoryStream o = new MemoryStream(); Stream s = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(o); s.Write(data, 0, data.Length); s.Close(); o.Flush(); o.Close(); return(o.ToArray()); }
public byte[] zip(string input) { byte[] data = Encoding.UTF8.GetBytes (input); using (MemoryStream memory = new MemoryStream()) { using (GZipOutputStream gzip = new GZipOutputStream(memory)) { gzip.Write (data, 0, data.Length); }//using return memory.ToArray (); }//using }
/// <summary> /// 压缩字节数组 /// </summary> /// <param name="inputBytes">Input bytes.</param> public static byte[] Compress (byte[] inputBytes) { using (var outStream = new MemoryStream ()) { using (var zipStream = new GZipOutputStream (outStream)) { zipStream.Write (inputBytes, 0, inputBytes.Length); zipStream.Close (); } return outStream.ToArray (); } }
public static byte[] Compress( byte[] bytesToCompress ) { byte[] rebyte = null; MemoryStream ms = new MemoryStream(); GZipOutputStream s = new GZipOutputStream(ms); s.Write( bytesToCompress , 0 , bytesToCompress.Length ); s.Close(); rebyte = ms.ToArray(); ms.Close(); return rebyte; }
/// <summary> /// 压缩 /// </summary> public static byte[] Compression(byte[] src) { if (IsGZip(src)) return src; MemoryStream ms = new MemoryStream(); GZipOutputStream gos = new GZipOutputStream(ms); gos.Write(src, 0, src.Length); gos.Close(); // 由于从第五个字节开始的4个长度都是表示修改时间,因此可以强行设定 byte[] result = ms.ToArray(); result[4] = result[5] = result[6] = result[7] = 0x00; return result; }
private const int COMPRESS_LEVEL = 7;// 0-9, 9 being the highest compression #endregion #region ICompressionProvider Members public byte[] Compress(byte[] data) { using (var outputStream = new MemoryStream()) { using (var compressStream = new GZipOutputStream(outputStream)) { compressStream.SetLevel(COMPRESS_LEVEL); compressStream.Write(data, 0, data.Length); compressStream.Finish(); compressStream.Close(); return outputStream.ToArray(); } } }
public void Process(BundleContext context, BundleResponse response) { var contentBytes = new UTF8Encoding().GetBytes(response.Content); var outputStream = new MemoryStream(); var gzipOutputStream = new GZipOutputStream(outputStream); gzipOutputStream.Write(contentBytes, 0, contentBytes.Length); var outputBytes = outputStream.GetBuffer(); response.Content = Convert.ToBase64String(outputBytes); // NOTE: this part is broken -> http://stackoverflow.com/a/11353652 context.HttpContext.Response.Headers["Content-Encoding"] = "gzip"; response.ContentType = _contentType; }
public override void Send(IMessage message) { using (MemoryStream outputStream = new MemoryStream()) { using (GZipOutputStream zipStream = new GZipOutputStream(outputStream)) { zipStream.Write(message.RawData.Array, message.RawData.Offset, message.RawData.Count); } Headers[CONTENT_LENGTH] = outputStream.ToArray().Length.ToString(); Headers[CONTENT_TYPE] = message.ContentType; Headers[ACCEPT_ENCODING] = ENCODING_GZIP; StartCoroutine(ReceiveCallback(new WWW(string.Format(URL, RemoteAddress, Port, message.ID), outputStream.ToArray(), Headers), message.ID)); OnSend(message.ID); } }
static int Main(string[] args) { try { byte[] buf = new byte[1024]; int n; using (Stream input = Console.OpenStandardInput()) using (Stream output = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(Console.OpenStandardOutput())) while ((n = input.Read(buf, 0, buf.Length)) != 0) output.Write(buf, 0, n); return 0; } catch(Exception e) { Console.Error.WriteLine("Error: "+e.Message); return 1; } }
public byte[] GZip(string text) { var buffer = Encoding.UTF8.GetBytes(text); using (var ms = new MemoryStream()) using (var zipStream = new GZipOutputStream(ms)) { zipStream.Write(buffer, 0, buffer.Length); zipStream.Close(); var compressed = ms.ToArray(); var gzBuffer = new byte[compressed.Length + 4]; Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length); Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4); return gzBuffer; } }
public static void GzipCompressFile(string inputPath, string outputPath, int compressionLevel) { using (var inputStream = new FileStream(inputPath, FileMode.Open, FileAccess.Read)) { using (var outputStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write)) { using (var gzipStream = new GZipOutputStream(outputStream)) { gzipStream.SetLevel(compressionLevel); int size; byte[] data = new byte[_defaultBufferSize]; do { size = inputStream.Read(data, 0, data.Length); gzipStream.Write(data, 0, size); } while (size > 0); } } } }
static int Main(string[] args) { try { byte[] buf = new byte[1024]; int n; using (Stream input = Console.OpenStandardInput()) using (Stream output = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(Console.OpenStandardOutput())) while ((n = input.Read(buf, 0, buf.Length)) != 0) { output.Write(buf, 0, n); } return(0); } catch (Exception e) { Console.Error.WriteLine("Error: " + e.Message); return(1); } }
/// <inheritdoc /> public override void ForwardProcessDataStream(Stream inStream, Stream outStream, Dictionary<string, string> options, out long writtenBytes) { using (GZipOutputStream gzStream = new GZipOutputStream(outStream)) { gzStream.IsStreamOwner = false; byte[] buffer = new byte[4096]; while (true) { int readCount = inStream.Read(buffer, 0, buffer.Length); if (readCount == 0) break; gzStream.Write(buffer, 0, readCount); } } writtenBytes = outStream.Position; }
override public void AddToStream (Stream stream, EventTracker tracker) { if (ChildCount > 1) throw new Exception ("Gzip file " + Uri + " has " + ChildCount + " children"); if (tracker != null) tracker.ExpectingAdded (UriFu.UriToEscapedString (this.Uri)); UnclosableStream unclosable; unclosable = new UnclosableStream (stream); GZipOutputStream gz_out; gz_out = new GZipOutputStream (unclosable); MemoryStream memory; memory = new MemoryStream (); // There should just be one child foreach (FileObject file in Children) file.AddToStream (memory, tracker); gz_out.Write (memory.ToArray (), 0, (int) memory.Length); memory.Close (); gz_out.Close (); }
/// zip a utf8 string using gzip into a base64 encoded string public static string ZipString(string ATextToCompress) { Byte[] originalText = Encoding.UTF8.GetBytes(ATextToCompress); MemoryStream memoryStream = new MemoryStream(); GZipOutputStream gzStream = new GZipOutputStream(memoryStream); gzStream.Write(originalText, 0, originalText.Length); gzStream.Flush(); gzStream.Finish(); memoryStream.Position = 0; Byte[] compressedBuffer = new byte[memoryStream.Length]; memoryStream.Read(compressedBuffer, 0, compressedBuffer.Length); gzStream.Close(); return Convert.ToBase64String(compressedBuffer); }
public static string EncodeBase64(byte[] input) { using (var compressedStream = new MemoryStream()) { // mono requires an installed zlib library for GZipStream to work :( // using (Stream csStream = new GZipStream(compressedStream, CompressionMode.Compress)) using (Stream csStream = new GZipOutputStream(compressedStream)) { csStream.Write(input, 0, input.Length); } string returnValue = Convert.ToBase64String(compressedStream.ToArray()); // Added the following to fix issue #429: Base64 content can include the slash character '/', and // if it happens to have two of them contiguously, it forms a comment in the persistence file and // truncates the value. So change them to a different character to protect the file. // The comma ',' char is not used by base64 so it's a safe alternative to use as we'll be able to // swap all of the commas back to slashes on reading, knowing that commas can only appear as the // result of this swap on writing: returnValue = returnValue.Replace('/',','); //SafeHouse.Logger.SuperVerbose("About to store the following Base64 string:\n" + returnValue); return returnValue; } }
public byte[] ToCompressedByteArray() { byte[] returnArray; byte[] streambuff = new byte[(XSize * YSize * ZSize) * Voxel.BYTE_SIZE]; byte[] buffer = new byte[Voxel.BYTE_SIZE]; int o = 0; for (int x = 0; x < XSize; x++) for (int y = 0; y < YSize; y++) for (int z = 0; z < ZSize; z++) { buffer = Voxels[x + XSize * (y + YSize * z)].ToBytes(); for (int i = 0; i < Voxel.BYTE_SIZE; i++) { streambuff[o] = buffer[i]; o++; } } using (var ms = new MemoryStream()) { using (var gzs = new GZipOutputStream(ms)) { gzs.SetLevel(1); gzs.Write(streambuff, 0, streambuff.Length); } returnArray = ms.ToArray(); } return returnArray; }
public void TrailingGarbage() { /* ARRANGE */ var ms = new MemoryStream(); var outStream = new GZipOutputStream(ms); // input buffer to be compressed byte[] buf = new byte[100000]; var rnd = new Random(); rnd.NextBytes(buf); // compress input buffer outStream.Write(buf, 0, buf.Length); outStream.Flush(); outStream.Finish(); // generate random trailing garbage and add to the compressed stream byte[] garbage = new byte[4096]; rnd.NextBytes(garbage); ms.Write(garbage, 0, garbage.Length); // rewind the concatenated stream ms.Seek(0, SeekOrigin.Begin); /* ACT */ // decompress concatenated stream var inStream = new GZipInputStream(ms); byte[] buf2 = new byte[buf.Length]; int currentIndex = 0; int count = buf2.Length; while (true) { int numRead = inStream.Read(buf2, currentIndex, count); if (numRead <= 0) { break; } currentIndex += numRead; count -= numRead; } /* ASSERT */ Assert.AreEqual(0, count); for (int i = 0; i < buf.Length; ++i) { Assert.AreEqual(buf2[i], buf[i]); } }
/* KMP message data format * Uncompressed data: [bool-false : data] * Compressed data: [bool-true : Int32-uncompressed length : compressed_data] */ public static byte[] Compress(byte[] data, bool forceUncompressed = false) { if (data == null) return null; byte[] compressedData = null; MemoryStream ms = null; GZipOutputStream gzip = null; try { ms = new MemoryStream(); if (data.Length < MESSAGE_COMPRESSION_THRESHOLD || forceUncompressed) { //Small message, don't compress using (BinaryWriter writer = new BinaryWriter(ms)) { writer.Write(false); writer.Write(data, 0, data.Length); compressedData = ms.ToArray(); ms.Close(); writer.Close(); } } else { //Compression enabled Int32 size = data.Length; using (BinaryWriter writer = new BinaryWriter(ms)) { writer.Write(true); writer.Write(size); gzip = new GZipOutputStream(ms); gzip.Write(data, 0, data.Length); gzip.Close(); compressedData = ms.ToArray(); ms.Close(); writer.Close(); } } } catch { return null; } finally { if (gzip != null) gzip.Dispose(); if (ms != null) ms.Dispose(); } return compressedData; }
public override void ExecuteResult(ControllerContext context) { if (_Headers != null) { foreach (var kv in Headers) { context.HttpContext.Response.AddHeader(kv.Key, kv.Value); } } if (Gzip) { using (var os = new GZipOutputStream(context.HttpContext.Response.OutputStream)) { os.Write(Data, 0, Data.Length); } context.HttpContext.Response.AddHeader("Content-Encoding", "gzip"); } else { context.HttpContext.Response.BinaryWrite(Data); } context.HttpContext.Response.End(); }
public static Mock<HttpMessageHandler> GetSharpZipLibGzipHandler(string originalContent) { return GetHandler( "gzip", originalContent, b => { var responseStream = new MemoryStream(); using (var compressStream = new GZipOutputStream(responseStream) {IsStreamOwner = false}) { compressStream.Write(b, 0, b.Length); } responseStream.Position = 0; return responseStream; }); }
private static byte[] ICSharpCompress(byte[] inputBytes) { byte[] returnBytes = null; using (MemoryStream ms = new MemoryStream()) { using (GZipOutputStream gs = new GZipOutputStream(ms)) { gs.Write(inputBytes, 0, inputBytes.Length); } returnBytes = ms.ToArray(); } return returnBytes; }