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
35
votes
3 answers

How to pipe one readable stream into two writable streams at once in Node.js?

The goal is to: Create a file read stream. Pipe it to gzip (zlib.createGzip()) Then pipe the read stream of zlib output to: 1) HTTP response object 2) and writable file stream to save the gzipped output. Now I can do down to 3.1: var gzip =…
esengineer
  • 9,514
  • 7
  • 45
  • 69
34
votes
5 answers

Why does my C# gzip produce a larger file than Fiddler or PHP?

If I GZip this text: Hello World through C# using this code: Stream stream = new MemoryStream(Encoding.Default.GetBytes("Hello World")); var compressedMemoryStream = new MemoryStream(); using (var gzipStream = new…
Martin Harris
  • 28,277
  • 7
  • 90
  • 101
31
votes
4 answers

Why does C# memory stream reserve so much memory?

Our software is decompressing certain byte data through a GZipStream, which reads data from a MemoryStream. These data are decompressed in blocks of 4KB and written into another MemoryStream. We've realized that the memory the process allocates is…
Tim Meyer
  • 12,210
  • 8
  • 64
  • 97
17
votes
2 answers

GZipStream and decompression

I have code that should do the compression: FileStream fs = new FileStream("g:\\gj.txt", FileMode.Open); FileStream fd = new FileStream("g:\\gj.zip", FileMode.Create); GZipStream csStream = new GZipStream(fd, CompressionMode.Compress); byte[]…
user191612
16
votes
5 answers

How do I read / write gzipped files in C++?

How do I read / write gzipped files in C++? The iostream wrapper classes here look good, and here is a simple usage example: gz::igzstream in(filename); std::string line; while(std::getline(in, line)){ std::cout << line << std::endl; } But I…
Frank
  • 64,140
  • 93
  • 237
  • 324
16
votes
5 answers

Sending gzipped data in WebRequest?

I have a large amount of data (~100k) that my C# app is sending to my Apache server with mod_gzip installed. I'm attempting to gzip the data first using System.IO.Compression.GZipStream. PHP receives the raw gzipped data, so Apache is not…
Charlie
15
votes
5 answers

GZipStream machine dependence

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…
Freek
  • 1,506
  • 1
  • 11
  • 25
15
votes
4 answers

.NET GZipStream compress and decompress

What is wrong with this code below. I always get FALSE, meaning after compression, decompressed data does not match original value. public static bool Test() { string sample = "This is a compression test of microsoft .net gzip…
MehdiAnis
  • 253
  • 1
  • 2
  • 7
14
votes
8 answers

How can I verify that web pages are being gzipped?

I plan to configure weblogic's gzip servlet filter (using weblogicx-gzip.jar) to gzip my web pages. How can I verify that the pages are being sent to the client gzipped?
BestPractices
  • 12,738
  • 29
  • 96
  • 140
13
votes
5 answers

How to serialize object + compress it and then decompress + deserialize without third-party library?

I have a big object in memory which I want to save as a blob into database. I want to compress it before saving because database server is usually not local. This is what I have at the moment: using (var memoryStream = new MemoryStream()) { using…
Marek
  • 2,419
  • 6
  • 34
  • 38
12
votes
3 answers

.NET GZipStream decompress producing empty stream

I'm trying to serialize and compress a WPF FlowDocument, and then do the reverse - decompress the byte array and deserialize to recreate the FlowDocument - using the .NET GZipStream class. I'm following the example described on MSDN and I have the…
Tom Hunter
  • 5,714
  • 10
  • 51
  • 76
11
votes
2 answers

GZipStream works but extension is lost

I am using following code to zip a file and it works fine but when I decompress with WinRar I get the original file name without the extension, any clue why if filename is myReport.xls when I decompress I get only myReport ? using (var fs = new…
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
11
votes
1 answer

Decompress byte array to string via BinaryReader yields empty string

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 =…
jkh
  • 3,618
  • 8
  • 38
  • 66
10
votes
4 answers

GZipStream not reading the whole file

I have some code that downloads gzipped files, and decompresses them. The problem is, I can't get it to decompress the whole file, it only reads the first 4096 bytes and then about 500 more. Byte[] buffer = new Byte[4096]; int count = 0; FileStream…
Edgar
  • 4,348
  • 4
  • 40
  • 59
10
votes
2 answers

GZipStream complains magic number in header is not correct

I'm attempting to use National Weather Service (U.S.) data, but something has changed recently and the GZip file no longer opens. .NET 4.5 complains that... Message=The magic number in GZip header is not correct. Make sure you are passing in a GZip…
jocull
  • 20,008
  • 22
  • 105
  • 149
1
2 3
21 22