gzipstream

GZipStream machine dependence

我的梦境 提交于 2019-12-06 17:43:18
问题 I'm running into some strange machine/OS dependent GZipStream behavior in .NET 4.0. This is the relevant code: public static string Compress(string input) { using(var ms = new MemoryStream(Encoding.UTF8.GetBytes(input))) using(var os = new MemoryStream()) { using(var gz = new GZipStream(os,CompressionMode.Compress,true)) { ms.CopyTo(gz); } return string.Join("",os.ToArray().Select(b=>b.ToString("X2"))); } } Running Compress("freek") gives me 1F8B08000000000004004B2B4A4DCD06001E33909D05000000

GZipStream: why do we convert to base 64 after compression?

北战南征 提交于 2019-12-06 03:52:28
I was just looking at a code sample for compressing a string. I find that using the GZipStream class suffices. But I don't understand why we have to convert it to base 64 string as shown in the example. using System.IO.Compression; using System.Text; using System.IO; public static string Compress(string text) { byte[] buffer = Encoding.UTF8.GetBytes(text); MemoryStream ms = new MemoryStream(); using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true)) { zip.Write(buffer, 0, buffer.Length); } ms.Position = 0; MemoryStream outStream = new MemoryStream(); byte[] compressed = new

C# to Java: Base64String, MemoryStream, GZipStream

十年热恋 提交于 2019-12-06 03:06:53
问题 I have a Base64 string that's been gzipped in .NET and I would like to convert it back into a string in Java. I'm looking for some Java equivalents to the C# syntax, particularly: Convert.FromBase64String MemoryStream GZipStream Here's the method I'd like to convert: public static string Decompress(string zipText) { byte[] gzipBuff = Convert.FromBase64String(zipText); using (MemoryStream memstream = new MemoryStream()) { int msgLength = BitConverter.ToInt32(gzipBuff, 0); memstream.Write

Is there a way to know if the byte[] has been compressed by gzipstream?

北城以北 提交于 2019-12-05 19:13:17
Is there a way to know if the byte[] has been compressed (or not) by GzipStream .net class? EDIT: Just want to know if the byte[] array has been compressed (since I will always be using GzipStream to compress and decompress) A GZipStream is a DeflateStream with an additional header and trailer. The format is specified in RFC 1952 . The .NET 4.0 GZipStream class writes the following bytes as header: byte[] headerBytes = new byte[] { 0x1f, 0x8b, 8, 0, 0, 0, 0, 0, 4, 0 }; if (compressionLevel == 10) { headerBytes[8] = 2; } The trailer consists of a CRC32 checksum and the length of the

Resteasy generally enable GZIP

我怕爱的太早我们不能终老 提交于 2019-12-05 10:37:05
I have a RestEasy + Java EE application. When I add @GZIP to a component class, the server-answer is gzipped, if the client sends "accepts:gzip" Is there a way to generally enable gzip for all components? I don't like to add the annotation to every class. I'm using RestEasy JAX-RS 3.0.1 No, there is no way with annotations to enable gzip for all resources. If you wanted to forego adding the annotation to every class you could create a servlet filter that looks at the incoming headers and gzips the response on the way out. if you are implementing your API behind an interface, so all your

GZipStream effectivness

泪湿孤枕 提交于 2019-12-04 21:35:07
问题 I am trying to save big UInt16 array into a file. positionCnt is about 50000, stationCnt is about 2500. Saved directly, without GZipStream, the file is about 250MB which can be compressed by external zip program to 19MB. With the following code the file is 507MB. What do I do wrong? GZipStream cmp = new GZipStream(File.Open(cacheFileName, FileMode.Create), CompressionMode.Compress); BinaryWriter fs = new BinaryWriter(cmp); fs.Write((Int32)(positionCnt * stationCnt)); for (int p = 0; p <

Decompress byte array to string via BinaryReader yields empty string

一笑奈何 提交于 2019-12-04 21:22:19
问题 I am trying to decompress a byte array and get it into a string using a binary reader. When the following code executes, the inStream position changes from 0 to the length of the array, but str is always an empty string. BinaryReader br = null; string str = String.Empty; using (MemoryStream inStream = new MemoryStream(pByteArray)) { GZipStream zipStream = new GZipStream(inStream, CompressionMode.Decompress); BinaryReader br = new BinaryReader(zipStream); str = br.ReadString(); inStream.Close(

Gzip compression not working ASP.net MVC5

混江龙づ霸主 提交于 2019-12-04 09:32:53
问题 I want to compress my web application with Gzip and I am using following class compression filter public class CompressFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { HttpRequestBase request = filterContext.HttpContext.Request; string acceptEncoding = request.Headers["Accept-Encoding"]; if (string.IsNullOrEmpty(acceptEncoding)) return; acceptEncoding = acceptEncoding.ToUpperInvariant(); HttpResponseBase response = filterContext

C# to Java: Base64String, MemoryStream, GZipStream

不羁岁月 提交于 2019-12-04 07:14:15
I have a Base64 string that's been gzipped in .NET and I would like to convert it back into a string in Java. I'm looking for some Java equivalents to the C# syntax, particularly: Convert.FromBase64String MemoryStream GZipStream Here's the method I'd like to convert: public static string Decompress(string zipText) { byte[] gzipBuff = Convert.FromBase64String(zipText); using (MemoryStream memstream = new MemoryStream()) { int msgLength = BitConverter.ToInt32(gzipBuff, 0); memstream.Write(gzipBuff, 4, gzipBuff.Length - 4); byte[] buffer = new byte[msgLength]; memstream.Position = 0; using

GZipStream is cutting off last part of XML

ⅰ亾dé卋堺 提交于 2019-12-03 21:18:04
问题 I have created an extension method called AddGZip which looks like the following: public static void AddGZip(this HttpResponse response) { response.Filter = new GZipStream(response.Filter, CompressionMode.Compress); response.AppendHeader("Content-Encoding", "gzip"); } This is a very cut down version of the code: var response = HttpContext.Current.Response; var request = HttpContext.Current.Request; var result = File.ReadAllText(path); if (request.SupportsGZip) { response.AddGZip(); } response