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

How to decompress with GZipStream in C#?

My problem is that I can't find a solution to decompress a file. Compressing a file works without error messages, but I don't know if that's right. Here is my code for compressing a file: using (StreamReader sr = new StreamReader(File.Open(srcFile,…
Cubi73
  • 1,891
  • 3
  • 31
  • 52
2
votes
1 answer

Writing compressed gzipstream to a file

I have a gzipstream that is compressed and I want to write it to a file. Now the problem is that the Read is not supported on the gzipstream that is compressed. Below is my code where the gzipstream reads the stream from a memorystream and then I…
Rob Schneider
  • 679
  • 4
  • 13
  • 27
2
votes
3 answers

gzip and return as string

I want to compress some files with gzip in PHP.. It works as it should when the output file is saved into a file.. When the file is opened it looks like this But not when the output is returned as a string.. Then the opened file looks like this..…
clarkk
  • 27,151
  • 72
  • 200
  • 340
2
votes
3 answers

Unzipping a File in vb.net

I have an embedded resource in my .exe that is a zip file, I would like to move it out of the resources and unzip it to a specific folder. Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btn_Install.Click Dim Dir_File As…
Spartin503
  • 47
  • 3
  • 9
2
votes
0 answers

GZipStream in C# from JQuery Ajax

I have a question for you regarding GZip. I have some dynamic forms created in .Net. Now I would like to have my form data (html + java script) gzipped and kept in database. so when the user request the form (JQuery Ajax) i can serve the gzipped…
Shiras
  • 115
  • 1
  • 1
  • 7
2
votes
2 answers

Write to gzip file from MemoryStream using XmlWriter and GZipStream

I am tring to write generate a gzipped XML file from a MemoryStream. Here is what I have so far - XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; MemoryStream ms = new MemoryStream(); using (XmlWriter writer =…
A Bogus
  • 3,852
  • 11
  • 39
  • 58
2
votes
1 answer

using gZipStream with one or two memory streams makes a big difference

I am definitely missing something very obvious but can anyone explain why there is a lot better compression rate in second case?! Case 1: very low compression and sometimes even growth in size. using (var memoryStream = new…
Marek
  • 2,419
  • 6
  • 34
  • 38
2
votes
1 answer

Error with decompression/compression error

I have the error while decompressing "The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters." It compresses fine but doesn't…
GANI
  • 2,013
  • 4
  • 35
  • 69
1
vote
2 answers

How do I determine how large to make my buffer when using GzipStream?

I'm trying to decompress a GZipStream. The problem is that the "Length" property on the stream throws a "NotSupported" exception. How do I know what size to make my buffer when I'm reading the bytes from the stream? Since it's compressed I don't…
Micah
  • 111,873
  • 86
  • 233
  • 325
1
vote
1 answer

C# .NET GZipStream returning inconsistent file lengths

I've noticed that each time I run the same file through GZipStream the file size differs. When I look at the file through Windows explorer, the size is different, but the Size on disk is always the same. Is this expected behaviour.
poy
  • 10,063
  • 9
  • 49
  • 74
1
vote
1 answer

Can I better compress multiple SQLite3 DB's individually or altogether?

I am working on a program in C# that deals with multiple database files at once to create a sort of cache that a user can choose later to compress and archive. I am using SQLite3 for these databases. However, when it came to compression, I noticed…
grg-n-sox
  • 717
  • 2
  • 5
  • 25
1
vote
1 answer

Why is my GZipStream not writeable?

I have some GZ compressed resources in my program and I need to be able to write them out to temporary files for use. I wrote the following function to write the files out and return true on success or false on failure. In addition, I've put a…
Ozzah
  • 10,631
  • 16
  • 77
  • 116
1
vote
2 answers

Non-static error when trying to load filestream to RichTextBox from Gzip decompress procedure

Basically all I want is to load a Gziped file into a rich text box. I found some code on the MS .NET site for decompressing the file. Now I want to point that stream to a rich text box, but I keep getting the error "An object reference is required…
Rob
  • 2,363
  • 7
  • 36
  • 54
1
vote
0 answers

GZipStream over .NET webservice causes invalid character issue

I'm working on a complex .NET client server architecture customization and I absolutely have no control over request/reply sent or received except that I can add my stuff to the request/reply objects. I've a dataset on server side to be sent to…
CoCoCoder
  • 13
  • 3
1
vote
1 answer

Using GZipStream not behaving how I would expect

I have the following code: string testString = "test abc"; var bytes = Encoding.UTF8.GetBytes(testString); using var memoryStream = new MemoryStream(); using var gzipStream = new GZipStream(memoryStream,…
sid
  • 35
  • 4