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

Crash safe on-the-fly compression with GZipStream

I'm compressing a log file as data is written to it, something like: using (var fs = new FileStream("Test.gz", FileMode.Create, FileAccess.Write, FileShare.None)) { using (var compress = new GZipStream(fs, CompressionMode.Compress)) { for…
Cocowalla
  • 13,822
  • 6
  • 66
  • 112
0
votes
1 answer

Is it practical to concatenate GZipStreams?

I conceived this idea to merge an arbitrary number of small text file into 1 single zip file with GZipStream class. I spent several nights to make it work, but the outcome is that the final zip file ended up being bigger than if the text files had…
Haoest
  • 13,610
  • 29
  • 89
  • 105
0
votes
1 answer

Gzip is messing AjaxControlToolKit

There is a form which contain a tabcontainer and watermark control from ajaxcontroltoolkit. It's Giving java error: Sys.Extended is undefined when using the following gzip procedure. The following code is from Global.asax file : void…
Zubarik
  • 107
  • 1
  • 2
  • 9
0
votes
1 answer

Gzipstream over tcp not decompressing

I am trying to send serialized,compressed data over a tcp connection using protobuf-net and GzipStream The deserializing or reading from the zipstream just blocks and does not complete. To test it I opted to try using a simpler FileStream to see…
Vort3x
  • 1,778
  • 2
  • 20
  • 37
0
votes
2 answers

Compressing and decompressing a string yields only the first letter of the original string?

I'm compressing a string with Gzip using this code: public static String Compress(String decompressed) { byte[] data = Encoding.Unicode.GetBytes(decompressed); using (var input = new MemoryStream(data)) using (var output…
Ricardo Altamirano
  • 14,650
  • 21
  • 72
  • 105
0
votes
2 answers

Why does one method of string compression return an empty string but another one doesn't?

I'm using GZipStream to compress a string, and I've modified two different examples to see what works. The first code snippet, which is a heavily modified version of the example in the documentation, simply returns an empty string. public static…
Ricardo Altamirano
  • 14,650
  • 21
  • 72
  • 105
0
votes
2 answers

What should I use for zipping instead of GZipStream in Windows Mobile?

I am developing an application for Windows Mobile 6 in Microsoft Visual Studio 2005. I have to unzip a text received by web service. But when I use GZipStream I get the following error: Error 1 The type or namespace name 'GZipStream' could not…
Bob
  • 22,810
  • 38
  • 143
  • 225
0
votes
1 answer

The stream size in GZip footer does not match the real stream size

I'm getting an IO exception "The stream size in GZip footer does not match the real stream size" when decompressing from GzipStream. This error is occurring 100% of the time on multiple files so I don't believe that this is a "real" corrupt file…
Johnv2020
  • 2,088
  • 3
  • 20
  • 36
0
votes
2 answers

Compress BitArray with GZip in C#

I have BitArray with 100M elements .This is about 12.5M . I have to compress this array . I use GZipStream of Framework . public static byte[] Compress(byte[] bytData) { try { MemoryStream ms = new MemoryStream(); …
Leonid
  • 81
  • 11
0
votes
2 answers

Compression stream appears empty

I need to compress a file using GZip by batch of specified size (not in a whole). I can successfuly fill the byte[] buffer, but after copying it into the compression stream, it just leaves the output stream empty. public void Compress(string source,…
Ondrej Sotolar
  • 1,352
  • 1
  • 19
  • 29
-1
votes
1 answer

Read from a compressing GZipStream

I'm exploring how to implement an HTTP server in C#. (And before you ask, I know there is Kestrel (and nothing else that isn't obsolete), and I want a much, much smaller application.) So, the response could be a Stream that cannot be seeked and has…
ygoe
  • 18,655
  • 23
  • 113
  • 210
-1
votes
1 answer

How to get uncompressed length of GZIP-compressed string?

Is there any reliable way of getting the uncompressed length of a compressed string (specifically compressed with GZIP) without decompressing the string? I have no control over the compression process, i.e. no agreed upon flags or tail data. I've…
gwow12345
  • 311
  • 3
  • 12
-1
votes
1 answer

How can I Trim a Stream C#

string hexstr = http.Body.ToString(); if (hexstr.Contains("1f8b")) { Stream str = http.Body.ToMemoryStream(); str.Position = str.Seek(0x1f8b, SeekOrigin.Begin); using (var zipStream = new GZipStream(str, CompressionMode.Decompress)) …
-1
votes
1 answer

Can GZipStream class in .net 4.5 create larger sized files?

I am using compression in my winform application to compress files before uploading. Previously we used .net framework 3.5 and the GZipStream class in that produced larger sized files in most of the cases.But, after upgrading to .net framework 4.5,…
V K
  • 1,645
  • 3
  • 26
  • 57
-1
votes
2 answers

Compression is working but decompression is not

I am writing some code to compress a byte array into a smaller byte array. Then I would like to decompress it: ''' ''' Receives bytes, returns compressed bytes. ''' Function Compress(ByRef raw() As Byte) As Byte() Using…
E.S.
  • 2,733
  • 6
  • 36
  • 71
1 2 3
21
22