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
2
votes
2 answers

What is the difference between zlib's gzip compression and the compression used by .NET's GZipStream?

Having an odd problem - one of my app suites has to read/write gzip-compressed files that are used on both Windows and Linux, and I am finding that the files I generate using zlib on Linux are 2-3 times larger than those I generate using GZipStream…
Will
  • 3,500
  • 4
  • 30
  • 38
2
votes
1 answer

Decompress body request with C# in asp.net mvc already compressed with pako library JS

I have an asp.net mvc app and want to compress body of the request in the view with Pako library (using gzip and deflate encoding) and decompress the body request before arriving to the controller using OnActionExecuting and…
2
votes
1 answer

GZipStream.Close causes ObjectDisposedExeption: Why?

Can anyone explain this?
richard
  • 12,263
  • 23
  • 95
  • 151
2
votes
4 answers

Gzip a directory that has subdirectories using GZipStream class with C#?

This MSDN site has an example to gzip a file. Then, how can I gzip a whole directory with sub directories in it?
prosseek
  • 182,215
  • 215
  • 566
  • 871
2
votes
2 answers

How to create a zip file using encoded string in C#

I'm new to C# and using C#.Net 2.0 with Visual Studio 2005. How can I create a zip file from a string using GZipStream. (I don't want to use any third party libraries and doing this purely using C#.) FYI: Scenario is this. Already there is a zip…
chatura
  • 51
  • 1
  • 5
2
votes
1 answer

How to replicate GZipStream.Write() in JavaScript?

I have this piece of C# code: public static byte[] TestGzip(string text) { byte[] bytes = Encoding.UTF8.GetBytes(text); MemoryStream memoryStream1 = new MemoryStream(); using (GZipStream gzipStream = new…
user2263572
  • 35
  • 1
  • 9
2
votes
0 answers

Calculate percent of bytes read from compressed HTTP response stream

In my desktop application I process HTTP response from the server and I want to provide a progress of processed bytes relatively to the total response length. I know the content length from the HTTP header but the problem is that a compression…
Igor B
  • 98
  • 7
2
votes
3 answers

Properly Compressing a CSV utilizing GZIP Stream and Memory Stream

I am compressing a CSV file utilizing GZIPStream and MemoryStream, and noticing something weird with the result file. It seems like the CSV is not properly recognized. This shows when the file is attached to an email, but works fine when saved on a…
GeorgeU
  • 7,819
  • 8
  • 26
  • 38
2
votes
1 answer

GZipStream in PHP

I am converting c# application into php. In that application GZipStream is used but i dont know how to compress and decompress same thing in php. I am able to compress and zip using php but the problem is, zip file of php and zip file of c# both are…
Juned Ansari
  • 5,035
  • 7
  • 56
  • 89
2
votes
1 answer

Losing special unicode characters in encryption (C#)

I am having an issue losing the µ character during encryption/decryption. I convert the string to bytes with Encoding.UTF8.GetBytes and write this to a stream with GZipStream.Write(), and from there to Blob. I noticed that µ is converted to 2 bytes…
2
votes
1 answer

Split a GZipStream result into given sized chunks keeping it valid

I have a bunch of data in a byte[], I compress it using a GZipStream like this. byte[] input = ...; var zipped = new MemoryStream(); using (var zipper = new GZipStream(zipped, CompressionMode.Compress, true)) { zipper.Write(input, 0,…
Zoltán Tamási
  • 12,249
  • 8
  • 65
  • 93
2
votes
2 answers

How to compress or Zip whole folder using GZipStream

Any idea how I can do this? I am able to compress a single file.
BreakHead
  • 10,480
  • 36
  • 112
  • 165
2
votes
1 answer

.Net Web API 2 POST consuming gzip compressed content

I'm writing a .Net Web Api (2) that have this one POST method. This method is currently deserializing it's only parameter by using the standard JSON formatter. We are also writing the Client that will consume this Api a C# Client using…
JosephS
  • 744
  • 5
  • 22
2
votes
1 answer

How to use one StreamWriter to write to multiple underlying streams?

I am writing text to a System.IO.StreamWriter. The underlying stream (specified in new StreamWriter(underlyingStream)) writes to a remote file. (I don't think its type is relevant, but for completeness' sake I'll mention it's a microsoft azure…
David S.
  • 5,965
  • 2
  • 40
  • 77
2
votes
1 answer

GZip decompression stops at arbitrary point

I'm using the .Net GZipStream class to compress and decompress files. After I do the decompression, the data seems fine, but then turns to nothing but zeros after a certain, seemingly arbitrary, point. For example, after decompressing a file, it is…
Mike Pateras
  • 14,715
  • 30
  • 97
  • 137