GZipStream not compressing?

天大地大妈咪最大 提交于 2019-12-12 23:19:50

问题


I'm trying to zip a memory stream into another memory stream so I can upload to a rest API. image is the initial memory stream containing a tif image.

WebRequest request = CreateWebRequest(...);
request.ContentType = "application/zip";
MemoryStream zip = new MemoryStream();
GZipStream zipper = new GZipStream(zip, CompressionMode.Compress);
image.CopyTo(zipper);
zipper.Flush();
request.ContentLength = zip.Length; // zip.Length is returning 0
Stream reqStream = request.GetRequestStream();
zip.CopyTo(reqStream);
request.GetResponse().Close();
zip.Close();

To my understand, anything I write to the GZipStream will be compressed and written to whatever stream was passed into it's constructor. When I copy the image stream into zipper, it appears nothing is actually copied (image is 200+ MB). This is my first experience with GZipStream so it's likely I'm missing something, any advice as to what would be greatly appreciated.

EDIT: Something I should note that was a problem for me, in the above code, image's position was at the very end of the stream... Thus when I called image.CopyTo(zipper); nothing was copied due to the position.


回答1:


[Edited: to remove incorrect info on GZipStream and it's constructor args, and updated with the real answer :) ]

After you've copied to the zipper, you need to shift the position of the MemoryStream back to zero, as the process of the zipper writing to the memory stream advances it's "cursor" as well as the stream being read:

WebRequest request = CreateWebRequest(...);
request.ContentType = "application/zip";
MemoryStream zip = new MemoryStream();
GZipStream zipper = new GZipStream(zip, CompressionMode.Compress);
image.CopyTo(zipper);
zipper.Flush();
zip.Position = 0; // reset the zip position as this will have advanced when written to.
...

One other thing to note is that the GZipStream is not seekable, so calling .Length will throw an exception.




回答2:


See this example: http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.flush.aspx#Y300

You shouldn't be calling flush on the stream.




回答3:


I don't know anything about C# and its libraries, but I would try to use Close instead of (or after) Flush first.

(Java's GZipOutputStream has the same problem that it doesn't properly flush, until Java 7.)



来源:https://stackoverflow.com/questions/6255253/gzipstream-not-compressing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!