Questions tagged [gzipstream]

GZipStream is a .NET 2.0+ class for compression and decompression using gzip format.

GZipStream is a .NET 2.0+ class for compression and decompression using gzip format., while including a redundancy check value for detecting data corruption. Compressed GZipStream object written to a .gz file can be decompressed using WinZip, 7zip, etc, but GZipStream does NOT provide functionality for adding or extracting files from .zip archives.

The compression functionality in DeflateStream and GZipStream is exposed as a stream. Data is read in on a byte-by-byte basis, so it is not possible to perform multiple passes to determine the best method for compressing entire files or large blocks of data. The DeflateStream and GZipStream classes are best used on uncompressed sources of data. If the source data is already compressed, using these classes may actually increase the size of the stream.

//Path to directory of files to compress and decompress.
string dirpath = @"c:\users\public\reports";

DirectoryInfo di = new DirectoryInfo(dirpath);

// Compress the directory's files.
foreach (FileInfo fi in di.GetFiles()){
    Compress(fi);
}

public static void Compress(FileInfo fi){
  // Get the stream of the source file.
  using (FileStream inFile = fi.OpenRead())
  {
    // Prevent compressing hidden and already compressed files.
    if ((File.GetAttributes(fi.FullName) & FileAttributes.Hidden)
       != FileAttributes.Hidden & fi.Extension != ".gz")
    {
      // Create the compressed file.
      using (FileStream outFile=File.Create(fi.FullName + ".gz"))
      {
          using (GZipStream Compress=new GZipStream(outFile,CompressionMode.Compress))
          {
              // Copy the source file into the compression stream.
              inFile.CopyTo(Compress);
              Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
                  fi.Name, fi.Length.ToString(), outFile.Length.ToString());
           }
       }
     }
  } 

http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx

Do not confuse with that is a Java class.

319 questions
9
votes
1 answer

How to decompress GZip in Stream (C#)?

This code receives the GZip-encoded string. How can I decode it? Stream stream = ret.GetResponseStream(); System.IO.StreamReader reader = new System.IO.StreamReader(stream, Encoding.Default); string answer = reader.ReadToEnd();//answer is GZip…
xieergai
  • 165
  • 2
  • 6
9
votes
1 answer

GZipStream compression problem ( Lost Byte )

I've got some strange problem with GZip Serializer. Trying serializing object with data in it. Following code give results(at POINT1 in debug): ms.Length = 100028 and uncompressedStream.Length=100027 After POINT1 there is exception "End of Stream…
Yfer
  • 93
  • 1
  • 3
9
votes
5 answers

GZipStream and DeflateStream produce bigger files

I'm trying to use deflate/gzip streams in C# but it appears that the files after compression are bigger than before. For example, I compress a docx file of 900ko, but it produce a 1.4Mo one ! And it does it for every file I tried. May be I am wrong…
kite
  • 679
  • 2
  • 8
  • 19
9
votes
2 answers

Using GZipStream to compress empty input results in an invalid gz file in C#

I am using the C# GZipStream class to compress some input data. The problem is when that input is empty. In that scenario, it ends up creating a 0 byte file. When I try to use 7zip to unzip the resulting .gz file, it gives an error saying the format…
Gadzair
  • 1,221
  • 14
  • 21
8
votes
3 answers

How to compress multiple files in a GZip file with the GZipStream class?

I am simply looking for a way to compress multiple files in a GZip file with the GZipStream class. Anybody has an idea how to do that?
Martin
  • 39,309
  • 62
  • 192
  • 278
8
votes
1 answer

GZipStream works when writing to FileStream, but not MemoryStream

If compress some json text, and write that it to a file using a FileStream I get the expected results. However, I do not want to write to disk. I simply want to memorystream of the compressed data. Compression to FileStream: string json =…
Dave
  • 1,645
  • 2
  • 23
  • 39
8
votes
2 answers

Gzip compression not working ASP.net MVC5

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) { …
aadi1295
  • 982
  • 3
  • 19
  • 47
8
votes
3 answers

Writing to the compression stream is not supported. Using System.IO.GZipStream

I get an exception when trying to decompress a (.gz) file using the GZipStream class that is included in the .NET framework. I am using the MSDN documentation. This is the exception: Writing to the compression stream is not supported. Here is the…
Nick
  • 19,198
  • 51
  • 185
  • 312
7
votes
2 answers

GZipStream effectivness

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…
danatel
  • 4,844
  • 11
  • 48
  • 62
7
votes
1 answer

Thread locking when flushing jsp file

Under heavy load I see lot of threads getting locked when GZipping and decompressing the JSP file. The thread dump looks like below. Seems to be coming from "header.jsp" which is of size 14Kb. http-0.0.0.0-8080-304" daemon prio=3…
7
votes
2 answers

Can I decompress and deserialize a file using streams?

My application serializes an object using Json.Net, compresses the resulting JSON, then saves this to file. Additionally the application can load an object from one of these files. These objects can be tens of Mb in size and I'm concerned about…
Andrew Stephens
  • 9,413
  • 6
  • 76
  • 152
7
votes
1 answer

GZipStream compression not working

I'm trying to read in a file and compress it using GZipStream, like this: using (var outStream = new MemoryStream()) { using (var fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read)) { using (var gzipStream = new…
Andrew Stephens
  • 9,413
  • 6
  • 76
  • 152
7
votes
4 answers

GZIP decompression C# OutOfMemory

I have many large gzip files (approximately 10MB - 200MB) that I downloaded from ftp to be decompressed. So I tried to google and find some solution for gzip decompression. static byte[] Decompress(byte[] gzip) { using (GZipStream…
William Calvin
  • 625
  • 6
  • 19
7
votes
1 answer

IIS Compression with in code GZipping?

I am adding in gzipping to all my static content, and html outputs from my .net 4 site. I also have compression enabled in IIS 7.5 (both static and dynamic), and what I am finding is that enabling compression in IIS, overwrites my Vary:…
Kevin
  • 2,684
  • 6
  • 35
  • 64
6
votes
3 answers

C# HttpListener Response + GZipStream

I use HttpListener for my own http server (I do not use IIS). I want to compress my OutputStream by GZip compression: byte[] refBuffer = Encoding.UTF8.GetBytes(...some data source...); var varByteStream = new…
Edward83
  • 6,664
  • 14
  • 74
  • 102
1
2
3
21 22