public static void ObjectToStream <TItem>(TItem obj, Stream stream, SerializationMode mode, bool compress) { if (mode == SerializationMode.Json) { var json = JsonConvert.SerializeObject(obj, JsonSettings()); if (compress) { var outZStream = new GZipOutputStream(stream); var writer = new BinaryWriter(outZStream); writer.Write(json); outZStream.Flush(); outZStream.Finish(); } else { var writer = new BinaryWriter(stream); writer.Write(json); } } else { Serializer.SerializeWithLengthPrefix(stream, obj, PrefixStyle.Fixed32); } }
public static byte[] ObjectToBytes <TItem>(TItem obj, SerializationMode mode, TypeDescription typeDescription) { using (var output = new MemoryStream()) { if (mode == SerializationMode.Json) { var json = ObjectToJson(obj, typeDescription); if (typeDescription.UseCompression) { using (var outZStream = new GZipOutputStream(output)) { var writer = new BinaryWriter(outZStream); writer.Write(json); outZStream.Flush(); outZStream.Finish(); } } else { var writer = new BinaryWriter(output); writer.Write(json); } } else { Serializer.SerializeWithLengthPrefix(output, obj, PrefixStyle.Fixed32); } return(output.ToArray()); } }
public static byte[] Compress(byte[] bytesToCompress) { byte[] rebyte = null; MemoryStream ms = new MemoryStream(); GZipOutputStream s = new GZipOutputStream(ms); try { s.Write(bytesToCompress, 0, bytesToCompress.Length); s.Flush(); s.Finish(); } catch (System.Exception ex) { #if UNITY_EDITOR Debug.Log(ex); #endif } ms.Seek(0, SeekOrigin.Begin); rebyte = ms.ToArray(); s.Close(); ms.Close(); s.Dispose(); ms.Dispose(); return(rebyte); }
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) { Assert.AreEqual(buf2[i], buf[i]); } }
/// <summary> /// 压缩字节数组 /// </summary> /// <param name="data">待压缩的字节数组</param> /// <param name="isClearData">压缩完成后,是否清除待压缩字节数组里面的内容</param> /// <returns>已压缩的字节数组</returns> public byte[] GZipCompress(byte[] data, bool isClearData = true) { byte[] bytes = null; try { using (MemoryStream o = new MemoryStream()) { using (Stream s = new GZipOutputStream(o)) { s.Write(data, 0, data.Length); s.Flush(); } bytes = o.ToArray(); } } catch (SharpZipBaseException) { } catch (IndexOutOfRangeException) { } if (isClearData) { Array.Clear(data, 0, data.Length); } return(bytes); }
/// <summary> /// Gzip压缩缓存文件 /// </summary> /// <param name="filePath">缓存文件路径</param> /// <returns>压缩后的二进制</returns> public static byte[] GZipStress(string filePath) { string path = filePath; if (!File.Exists(path)) { path = string.Format("{0}/Config/{1}.boxcache", AppDomain.CurrentDomain.BaseDirectory, typeof(T).FullName); } using (FileStream ms = new FileStream(path, FileMode.Open)) { using (MemoryStream stream = new MemoryStream()) { GZipOutputStream outStream = new GZipOutputStream(stream); int readed; byte[] buffer = new byte[2048]; do { readed = ms.Read(buffer, 0, buffer.Length); outStream.Write(buffer, 0, readed); }while (readed != 0); outStream.Flush(); outStream.Finish(); return(stream.GetBuffer()); } } }
public void DelayedHeaderWriteFlushWithData() { var ms = new MemoryStream(); Assert.AreEqual(0, ms.Length); using (GZipOutputStream outStream = new GZipOutputStream(ms) { IsStreamOwner = false }) { Assert.AreEqual(0, ms.Length); // #382 - test flushing the stream before writing to it. outStream.Flush(); outStream.WriteByte(45); } ms.Seek(0, SeekOrigin.Begin); // Test that the gzip stream can be read var readStream = new MemoryStream(); using (GZipInputStream inStream = new GZipInputStream(ms)) { inStream.CopyTo(readStream); } // Check that the data was read byte[] data = readStream.ToArray(); CollectionAssert.AreEqual(new byte[] { 45 }, data, "Decompressed data should match initial data"); }
//------------------------------------------------------------------------- public static byte[] CompressGZIP(byte[] buf) { if (null == buf || 0 == buf.Length) { Console.WriteLine("CompressGZIP buf is nil"); return(buf); } byte[] bufCompress = buf; try { MemoryStream ms = new MemoryStream(); GZipOutputStream outStream = new GZipOutputStream(ms); outStream.Write(buf, 0, buf.Length); outStream.Flush(); outStream.Finish(); bufCompress = ms.GetBuffer(); Array.Resize(ref bufCompress, (int)outStream.Length); outStream.Close(); ms.Close(); } catch (Exception ex) { Console.WriteLine("CompressGZIP 错误:" + ex.Message); } return(bufCompress); }
// write manifest & checksum of manifest private static void WriteManifest(PackageBuildInfo buildInfo, Manifest manifest) { var json = JsonUtility.ToJson(manifest); var bytes = Encoding.UTF8.GetBytes(json); var manifestRawPath = Path.Combine(buildInfo.packagePath, Manifest.ManifestFileName + ".json"); var manifestChecksumPath = Path.Combine(buildInfo.packagePath, Manifest.ChecksumFileName); byte[] zData; using (var zStream = new MemoryStream()) { using (var outputStream = new GZipOutputStream(zStream)) { outputStream.SetLevel(Deflater.BEST_COMPRESSION); outputStream.Write(bytes, 0, bytes.Length); outputStream.Flush(); } zStream.Flush(); zData = zStream.ToArray(); } buildInfo.filelist.Add(Manifest.ManifestFileName); buildInfo.filelist.Add(Manifest.ManifestFileName + ".json"); var fileEntry = AsManifestEntry(EncryptData(buildInfo, Manifest.ManifestFileName, zData), buildInfo.data.chunkSize); var fileEntryJson = JsonUtility.ToJson(fileEntry); Debug.LogFormat("write manifest: {0}", fileEntryJson); File.WriteAllBytes(manifestRawPath, bytes); File.WriteAllText(manifestChecksumPath, fileEntryJson); }
// Token: 0x0600001A RID: 26 RVA: 0x00002C04 File Offset: 0x00000E04 public static byte[] GZipCompress(byte[] data, bool isClearData = true) { byte[] result = null; try { using (MemoryStream memoryStream = new MemoryStream()) { using (Stream stream = new GZipOutputStream(memoryStream)) { stream.Write(data, 0, data.Length); stream.Flush(); } result = memoryStream.ToArray(); } } catch (SharpZipBaseException) { } catch (IndexOutOfRangeException) { } if (isClearData) { Array.Clear(data, 0, data.Length); } return(result); }
public void DelayedHeaderWriteFlushNoData() { var ms = new MemoryStream(); Assert.AreEqual(0, ms.Length); using (GZipOutputStream outStream = new GZipOutputStream(ms) { IsStreamOwner = false }) { // #382 - test flushing the stream before writing to it. outStream.Flush(); } ms.Seek(0, SeekOrigin.Begin); // Test that the gzip stream can be read var readStream = new MemoryStream(); using (GZipInputStream inStream = new GZipInputStream(ms)) { inStream.CopyTo(readStream); } byte[] data = readStream.ToArray(); Assert.That(data, Is.Empty, "Should not have any decompressed data"); }
public byte[] Compress(string message) { if (message == null) { return(null); } using (var dataStream = new MemoryStream()) using (var zipStream = new GZipOutputStream(dataStream)) { zipStream.SetLevel((int)CompressionLevel); var rawBytes = Encoding.UTF8.GetBytes(message); zipStream.Write(rawBytes, 0, rawBytes.Length); zipStream.Flush(); zipStream.Finish(); var compressedBytes = new byte[dataStream.Length]; dataStream.Seek(0, SeekOrigin.Begin); dataStream.Read(compressedBytes, 0, compressedBytes.Length); return(compressedBytes); } }
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()); } } }
private void CreateOrModifyCacheFile(BinaryData cacheBinary, bool compress) { SN.File f = null; MemoryStream cacheStream = new MemoryStream(); if (compress) { GZipOutputStream gzipStream = new GZipOutputStream(cacheStream); byte[] buff = Encoding.ASCII.GetBytes(this._content.ToCharArray()); gzipStream.Write(buff, 0, buff.Length); gzipStream.Flush(); gzipStream.Close(); // set compressed binary byte[] compressedData = cacheStream.ToArray(); cacheBinary.SetStream(new MemoryStream(compressedData)); } else { cacheBinary.SetStream(Tools.GetStreamFromString(_content)); } // gets cache file or creates a new one, the new stream will be saved in both cases if (!Node.Exists(FullCacheFilePath)) { f = SN.File.CreateByBinary(this.CacheFolder, cacheBinary); f.Name = _cacheFile; } else { f = Node.Load <SN.File>(this.FullCacheFilePath); f.Binary = cacheBinary; } f.Save(); }
public void OriginalFilename() { var content = "FileContents"; using var ms = new MemoryStream(); using (var outStream = new GZipOutputStream(ms) { IsStreamOwner = false }) { outStream.FileName = "/path/to/file.ext"; var writeBuffer = Encoding.ASCII.GetBytes(content); outStream.Write(writeBuffer, 0, writeBuffer.Length); outStream.Flush(); outStream.Finish(); } ms.Seek(0, SeekOrigin.Begin); using (var inStream = new GZipInputStream(ms)) { var readBuffer = new byte[content.Length]; inStream.Read(readBuffer, 0, readBuffer.Length); Assert.AreEqual(content, Encoding.ASCII.GetString(readBuffer)); Assert.AreEqual("file.ext", inStream.GetFilename()); } }
private void WriteWithoutCache(HttpContext context) { GZipOutputStream gzipStream = new GZipOutputStream(context.Response.OutputStream); byte[] buff = GetEncoding.GetBytes(_content.ToCharArray()); gzipStream.Write(buff, 0, buff.Length); gzipStream.Flush(); gzipStream.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()); }
public static byte[] compress(byte[] data) { MemoryStream memoryStream = new MemoryStream(); GZipOutputStream gZipOutputStream = new GZipOutputStream(memoryStream); gZipOutputStream.Write(data, 0, data.Length); gZipOutputStream.Flush(); gZipOutputStream.Finish(); gZipOutputStream.Close(); return(memoryStream.ToArray()); }
public static byte[] Compress(byte[] input) { MemoryStream mem = new MemoryStream(input); GZipOutputStream gs = new GZipOutputStream(mem); gs.Write(input, 0, input.Length); gs.Flush(); gs.Close(); mem.Flush(); mem.Close(); return(mem.ToArray()); }
public static string Compress(string data) { MemoryStream ms = new MemoryStream(); GZipOutputStream zs = new GZipOutputStream(ms); byte[] buf = StrUtil.ToByteArray(data); zs.Write(buf, 0, buf.Length); zs.Flush(); return(Encode(ms.ToArray())); }
//------------------------------------------------------------------------- public static string CompressGZIPBase64(string strValue, out bool bSuccess) { bSuccess = false; if (true == string.IsNullOrEmpty(strValue)) { Console.WriteLine("CompressGZIPBase64 strValue is nil"); return(string.Empty); } string str = strValue; try { byte[] buf = Encoding.UTF8.GetBytes(strValue); MemoryStream ms = new MemoryStream(); GZipOutputStream outStream = new GZipOutputStream(ms); outStream.Write(buf, 0, buf.Length); outStream.Flush(); outStream.Finish(); byte[] bufCompress = ms.GetBuffer(); //说明:过滤掉尾部无效0数据 Array.Resize(ref bufCompress, (int)outStream.Length); // 0X1F 0X8B 0X08 0X00 // 31 139 8 0 if (0x1F == bufCompress[0] && 0x8B == bufCompress[1] /* && 0x08 == bufCompress[2] && 0x00 == bufCompress[3]*/) { str = Convert.ToBase64String(bufCompress); bSuccess = true; } else { Console.WriteLine("CompressGZIPBase64 ERROR:" + bufCompress[0] + "," + bufCompress[1] + "," + bufCompress[2] + "," + bufCompress[3]); } //LoggerHelper.Error("CompressGZIPBase64 0 :" + bufCompress[0]); //LoggerHelper.Error("CompressGZIPBase64 1 :" + bufCompress[1]); //LoggerHelper.Error("CompressGZIPBase64 2 :" + bufCompress[2]); //LoggerHelper.Error("CompressGZIPBase64 3 :" + bufCompress[3]); // byte[] bufCompress2 = Convert.FromBase64String(str); outStream.Close(); ms.Close(); } catch (Exception ex) { Console.WriteLine("CompressGZIPBase64 错误:" + ex.Message); } return(str); }
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]); } }
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()); }
static void Compress(Stream inputFs, Stream outputFs) { using (GZipOutputStream stream = new GZipOutputStream(outputFs)) { stream.SetLevel(CompressLevel); int size; while ((size = inputFs.Read(MBytesCache, 0, MBytesCache.Length)) > 0) { stream.Write(MBytesCache, 0, size); } stream.Flush(); } }
/// <summary> /// 压缩二进制数据 /// </summary> /// <param name="_from">原始数据输入流</param> /// <param name="_to">压缩文件输出流</param> /// <returns></returns> public static void GZipCompress(Stream _from, Stream _to, FileCompressProgress fcp = null) { using (var gz = new GZipOutputStream(_to)) { var buffer = new byte[_buffer]; int i; while ((i = _from.Read(buffer, 0, buffer.Length)) > 0) { gz.Write(buffer, 0, i); fcp?.BeginInvoke("gz", _from.Length, _from.Position, null, null); } gz.Flush(); } }
// Compress method // This function has two parameters. The first one is the data // That is being compressed and the second is for writing the // compressed data into output stream. public void Compress(byte[] data, Stream output) { //Initializes the stream needed to store the compressed data GZipOutputStream zipStream = new GZipOutputStream(output); //Compresses a block of bytes to this stream zipStream.Write(data, 0, data.Length); //Clears all buffers for this stream zipStream.Flush(); //Closes and releases any resources associated with the stream zipStream.Close(); }
public void FlushToUnderlyingStream() { var ms = new MemoryStream(); var outStream = new GZipOutputStream(ms); byte[] buf = new byte[100000]; var rnd = new Random(); rnd.NextBytes(buf); outStream.Write(buf, 0, buf.Length); // Flush output stream but don't finish it yet outStream.Flush(); ms.Seek(0, SeekOrigin.Begin); var inStream = new GZipInputStream(ms); byte[] buf2 = new byte[buf.Length]; int currentIndex = 0; int count = buf2.Length; while (true) { try { int numRead = inStream.Read(buf2, currentIndex, count); if (numRead <= 0) { break; } currentIndex += numRead; count -= numRead; } catch (GZipException) { // We should get an unexpected EOF exception once we've read all // data as the stream isn't yet finished. break; } } Assert.AreEqual(0, count); for (int i = 0; i < buf.Length; ++i) { Assert.AreEqual(buf2[i], buf[i]); } }
public static byte[] Compress(string content) { MemoryStream memoryStream = new MemoryStream(); using (GZipOutputStream outStream = new GZipOutputStream(memoryStream)) { var data = Encoding.UTF8.GetBytes(content); outStream.IsStreamOwner = false; outStream.SetLevel(4); outStream.Write(data, 0, data.Length); outStream.Flush(); outStream.Finish(); } return(memoryStream.GetBuffer()); }
/// <summary> Gzip 压缩算法 比上面快10 多倍 /// 压缩文件 /// </summary> /// <param name="fileToCompress"></param> public static void CompressFileGz(string inputPath, string outPath) { using (FileStream inputStream = new FileStream(inputPath, FileMode.Open)) { using (FileStream outputStream = new FileStream(outPath, FileMode.Create)) { using (GZipOutputStream zipStream = new GZipOutputStream(outputStream)) { CopyStream(inputStream, zipStream); zipStream.Flush(); zipStream.Close(); } } } }
public static string TestZip(string data) { FileStream fs = new FileStream("D:\\test.zip", FileMode.Create); GZipOutputStream zos = new GZipOutputStream(fs); byte[] buf = StrUtil.ToByteArray(data); zos.Write(buf, 0, buf.Length); zos.Flush(); fs.Flush(); //FileStream fs2 = new FileStream("D:\\test2.zip", FileMode.Create); //StreamUtils.Copy(fs,fs2,new byte[1024]); //fs2.Flush(); //fs2.Close(); MemoryStream s1 = new MemoryStream(); //StreamUtils.Copy(fs, s1, new byte[1024]); fs.CopyTo(s1); s1.Seek(0, SeekOrigin.Begin); zos.Close(); fs.Close(); //fs = new FileStream("D:\\test.zip", FileMode.Open); //MemoryStream s1 = new MemoryStream(); //StreamUtils.Copy(fs,s1,new byte[1024]); //s1.Seek(0, SeekOrigin.Begin); GZipInputStream zis = new GZipInputStream(s1); MemoryStream rs = new MemoryStream(); buf = new byte[1024]; int size = 0; while ((size = zis.Read(buf, 0, buf.Length)) > 0) { rs.Write(buf, 0, size); } zis.Close(); fs.Close(); return(StrUtil.FromByteArray(rs.ToArray())); }