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
6
votes
4 answers

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

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)
stackoverflowuser
  • 22,212
  • 29
  • 67
  • 92
6
votes
2 answers

GZipStream does gzipping but ungzipping file end up with "Unexpected end of data"

Does anyone know why I'm getting the "Unexpected end of data" error message when un-gzipping the gzip file? To verify the bytes data is not corrupted, I use the FooTest4.csv to write to file and was able to opened the file successfully. Both…
fletchsod
  • 3,560
  • 7
  • 39
  • 65
6
votes
1 answer

GZipStream with StreamReader.ReadLine only reads first line

I have a gzip file containing a txt file that needs to be cleaned up. I would like to read from the GZipped file line by line and then write the cleaned content to an output GZIP file all in one shot like this: void ExtractAndFix(string…
bruiseruser
  • 343
  • 4
  • 12
6
votes
2 answers

GZipStream - write not writing all compressed data even with flush?

I've got a pesky problem with gzipstream targeting .Net 3.5. This is my first time working with gzipstream, however I have modeled after a number of tutorials including here and I'm still stuck. My app serializes a datatable to xml and inserts into…
longofest
  • 616
  • 5
  • 16
6
votes
1 answer

How to upload GZip compressed data using System.Net.WebClient in C#

In my project I need to upload large JSON data from a desktop app. So, I need it to be compressed. I searched everywhere but I did not find a complex solution to my problem. So, I put several snippets together. See the answer below. I hope you find…
petriq
  • 969
  • 1
  • 11
  • 24
6
votes
4 answers

GZipStream decompression performance is poor

I have a .NET 2.0 WinForms app that connects to a backend WAS server. I am using GZipStream to decode data coming back from a HttpWebRequest call made to the server. The data returned is compressed CSV, which Apache is compressing. The entire…
Armbrat
  • 2,295
  • 1
  • 23
  • 32
6
votes
2 answers

GZipStream on large data

I am attempting to compress a large amount of data, sometimes in the region of 100GB, when i run the routine i have written it appears the file comes out exactly the same size as the previous size. Has anyone else had this issue with the…
Skintkingle
  • 1,579
  • 3
  • 16
  • 28
5
votes
1 answer

GZipStream doesn't detect corrupt data (even CRC32 passes)?

I'm using GZipStream to compress / decompress data. I chose this over DeflateStream since the documentation states that GZipStream also adds a CRC to detect corrupt data, which is another feature I wanted. My "positive" unit tests are working well…
Andrew Teare
  • 51
  • 2
  • 2
5
votes
2 answers

GZipStream.Write Method

I have been reading for a short while about GZipStream and its Write method. What I am attempting to do is convert the compressed data from the stream and put it in a byte array. I will leave you with my code below as I believe it will help…
user725913
5
votes
1 answer

Using .NET GZipStream Class with Mono

I'm trying to build an example from GZipStream Class. With the command gmcs gzip.cs, I got error messages. gzip.cs is the same source from the msdn. It seems that I need to add reference when compiling. What's missing? gzip.cs(57,32): error CS1061:…
prosseek
  • 182,215
  • 215
  • 566
  • 871
5
votes
5 answers

Compressing and Decompressing Folders in C#

I want to compress and decompress a folder using C#. The problem with GZipStream is that it takes filenames and hence I need to write a recursive logic. Can I somehow do it like, give source folder name and destination filename to compress the…
Saubhagya
  • 1,075
  • 1
  • 9
  • 12
5
votes
1 answer

Is it possible to uncompress a gzipped file as it is downloading?

I want to progammatically download a gzipped file and uncompress it, but instead of waiting for it to fully download before uncompressing it, I want to unzip it while its downloading, that is, unzip it on the fly. Is this even possible, or the the…
pythonic
  • 20,589
  • 43
  • 136
  • 219
5
votes
1 answer

gzipstream memory stream to file

I am trying to compress JSON files using Gzip compression to be sent to another location. It needs to process 5,000 - 10,000 files daily, and I don't need the compressed version of the file on the local machine (they are actually being transferred…
smullan
  • 132
  • 3
  • 9
5
votes
5 answers

Resteasy generally enable GZIP

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…
wutzebaer
  • 14,365
  • 19
  • 99
  • 170
5
votes
4 answers

How to read from file containing multiple GzipStreams

I've got a file created with code which looks like this: using (var fs=File.OpenWrite("tmp")) { using (GZipStream gs=new GZipStream(fs,CompressionMode.Compress,true)) { using (StreamWriter…
Arsen Zahray
  • 24,367
  • 48
  • 131
  • 224
1 2
3
21 22