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
3
votes
1 answer

GZipStream: why do we convert to base 64 after compression?

I was just looking at a code sample for compressing a string. I find that using the GZipStream class suffices. But I don't understand why we have to convert it to base 64 string as shown in the example. using System.IO.Compression; using…
deostroll
  • 11,661
  • 21
  • 90
  • 161
3
votes
3 answers

GZipStream: Compressed file bigger than original

I'm trying to use gzip streams in C# but it appears that the files after compression are bigger than before. It's occurs when I working with .avi and .mkv files. But if I working with .txt and .html compressed file smaller than original. using…
3
votes
2 answers

C# - GZipStream magic number incorrect?

So, I'm trying to make a program which turns a computer into a proxy using this. It all works well, except for gzip/deflate pages. Whenever I try to uncompress, I get an InvalidDataException stating the magic number in the GzipHeader is incorrect. I…
Adam M
  • 113
  • 3
  • 13
3
votes
1 answer

alternative for GZIPSTREAM in WP8

I'm working on a WP8 application that uses a Webservice that is shared with a WP6 application This webservice uses GZipStream to compress the communicated answers. Now it seems that WP8 doesn't support this way of compression Does someone know how i…
3
votes
3 answers

System.OutofMemoryException throw while doing GZipStream Compression

I am working in win forms. Getting errors while doing following operation. It shows me System.OutOfMemoryException error when i try to run the operation around 2-3 times continuously. Seems .NET is not able to free the resouces used in operation.…
amit patel
  • 2,287
  • 8
  • 31
  • 45
3
votes
1 answer

Decompressing using GZipStream returns only the first line

I’ve been working on a function parsing 3rd party fms logs. The logs are in Gzip, so I use a decompressing function that works for any other Gzip files we use. When decompressing these files I only get the first line of the compressed file, there’s…
user1482107
  • 33
  • 1
  • 3
3
votes
0 answers

boost: Uncompress http response using gzip failed

I'm trying to uncompress http body response using boost gzip filters. I'm using the standard code example provided everywhere: std::string source = "c:\\install\\data.gz"; std::string destination = "c:\\install\\data.txt"; using namespace…
2
votes
1 answer

C# GZipStream - Zipping MemoryStreams

I have 5 MemoryStreams. I want to create a new zip (Which will also be a Stream) while each of the 5 MemoryStreams I have, will respresnt a file. I do have this code about how to Zip a string/1 MemoryStream. public static string Zip(string value) { …
user779444
  • 1,365
  • 4
  • 21
  • 38
2
votes
1 answer

C# networkstream compression - Sharpziplib, DotNetZip, gzipstream all give errors on my stream

I have a pair of C# client-server programs that communicate using a networkstream. Everything works fine as it is without compression. Now I'd like to get the bandwidth-usage down, so I want to use a compressing wrapperstream around my…
Pygmy
  • 1,268
  • 17
  • 33
2
votes
1 answer

Why does HttpWebResponse try to decompress the stream using GZip when Content-Encoding is empty?

I've looked at a lot of Q&As around this subject and got to the point where I use the following code in order to get the bytes from a given URI: var request = (HttpWebRequest)WebRequest.Create(uri); request.AutomaticDecompression =…
nir.arazi
  • 63
  • 10
2
votes
1 answer

Zlib decompression throws header error - Ruby

I need help on decompressing chunk data from a stream api. I am connecting to Gnip stream api which returns json output using gzip compression. When I try to decompress the output data, it throws me the following error "Zlib::DataError: incorrect…
Veeraa
  • 29
  • 2
2
votes
1 answer

Convert file to Gzip memory stream, then convert that to Base64 before exporting to text file (or var)

How do I change the below so i can use the Gzip data with [System.Convert]::ToBase64String() instead of an outfile then send THAT output(the b64) to an outfile **I Received these answers from help on a previous post from an awesome member. It was…
2
votes
3 answers

Send/Receive GZip compressed MSMQ messages in C#

I am trying to send large objects (>30MB) to a MSMQ queue. Due to the large amount of data we are are tring to send the idea was to GZip the objects prior to sending them, then unzipping them on the receiving end. However, writing the compressed…
John
  • 3,591
  • 8
  • 44
  • 72
2
votes
1 answer

How can I open a PDF then ZIP it and then create a Base64 encoded string in C#

I have a task to embed a Zipped PDF retrieved from the file system into an XML document in C#. The specification states: For instance, retrieve the file ‘INV001.pdf’ Compress it to get a ‘INV001.zip’ file. 2/ Encode each .zip to Base64 format The…
Edwardo
  • 872
  • 1
  • 11
  • 24
2
votes
1 answer

Java vs C# GZip Compression

Any idea why Java's GZIPOutputStream compressed string is different from my .NET's GZIP compressed string? Java Code: package com.company; import java.io.IOException; import java.nio.ByteBuffer; import java.util.Base64; public class Main { …
John
  • 75
  • 1
  • 6