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

MemoryStream VS FileStream which one is better to use in Webservice for GZipStream decompress?

I have a asp.net webservice. some functionality of this webservice is to decompress clients request first. for this i have wrote 2 methods one uses MemoryStream and other uses FileStream. When using MemoryStream sometiomes it get…
Rezoan
  • 1,745
  • 22
  • 51
4
votes
3 answers

How to Decompress nested GZip (TGZ) files in C#

I am receiving a TGZ file that will contain one plain text file along with possibly one or more nested TGZ files. I have figured out how to decompress the main TGZ file and read the plain text file contained in it, but I have not been able to…
Flahrty
  • 168
  • 1
  • 2
  • 7
3
votes
0 answers

Ksoap exception in j2me

I am create one J2ME application which call to .NET WCF web service and me used ksoap. I am using netbeans IDE. But when I run that application then gives run time exception is org.xmlpull.v1.XmlPullParserException: Invalid stream or encoding:…
RAJ PATEL
  • 554
  • 4
  • 12
3
votes
1 answer

'System.IO.Compression.GZipStream' cannot be serialized. Consider marking it with the DataContractAttribute attribute

I am trying to use WCF service to upload GZip files. Im trying to compress files using Gzip and then passing it through to WCF service to be uploaded to server. Every time I'm running the code I get this error message…
user929153
  • 475
  • 2
  • 11
  • 25
3
votes
2 answers

GZIP output stream

I am trying to GZIP some XML that gets streamed over HTTP (not a web service) if (ZipOutput) { output = new GZipStream(Context.Response.OutputStream, CompressionMode.Compress); Context.Response.AppendHeader("Content-Encoding",…
Matt Evans
  • 7,113
  • 7
  • 32
  • 64
3
votes
2 answers

Reading compressed file and writing to new file will not allow decompression

I have a test program that demonstrates the end result that I am hoping for (even though in this test program the steps may seem unnecessary). The program compresses data to a file using GZipStream. The resulting compressed file is C:\mydata.dat. I…
jkh
  • 3,618
  • 8
  • 38
  • 66
3
votes
1 answer

GzipStream (.net 4.0) 4GB problem

I am having trouble programmatically unzipping a 3GB (7GB uncompressed) gzip file using the built in .net 4.0 Gzip and Deflate Classes. My understanding is that they should both support files over 4GB, but they seem to not be working. When I…
Glenn
  • 1,234
  • 2
  • 19
  • 33
3
votes
1 answer

Compressing and decompressing very large files using System.IO.Compressing.Gzip

My problem can be described with following statements: I would like my program to be able to compress and decompress selected files I have very large files (20 GB+). It is safe to assume that the size will never fit into the memory Even after…
3
votes
3 answers

Is there a problem with IO.Compression?

I've just started compressing file in VB.Net, using the following code. Since I'm targeting Fx 2.0, I can't use the Stream.CopyTo method. My code, however, gives extremely poor results compared to the gzip Normal compression profile in 7-zip. For…
Clément
  • 12,299
  • 15
  • 75
  • 115
3
votes
1 answer

"Invalid use of response filter" when compressing response from an IHttpHandler

I have an IHttpHandler returning a file. When the response stream is compressed, either automatically using Telerik RadCompression or by explicitly setting a filter using context.Response.Filter = new GZipStream(context.Response.Filter,…
mhenry1384
  • 7,538
  • 5
  • 55
  • 74
3
votes
4 answers

Computing progress (bar) using GZipStream

I'm reading a .gz file from some slow source (like FTP Server) and am processing the received data right away. Looks something like this: FtpWebResponse response = ftpclientRequest.GetResponse() as FtpWebResponse; using (Stream ftpStream =…
Sam
  • 28,421
  • 49
  • 167
  • 247
3
votes
1 answer

GZIPStream Compression Always Returns 10 Bytes

I'm trying to compress some text in my UWP application. I created this method to make it easier later on: public static byte[] Compress(this string s) { var b = Encoding.UTF8.GetBytes(s); using (MemoryStream ms = new MemoryStream()) …
Reynevan
  • 1,475
  • 1
  • 18
  • 35
3
votes
2 answers

Can GZip compression (via .net) increase file size?

I keep track of the original size of the files that I'm compressing using .Net's GZipStream class, and it seems like the file that I thought I was compressing has increased in size. Is that possible? This is how I'm doing the compression: Byte[]…
Mike Pateras
  • 14,715
  • 30
  • 97
  • 137
3
votes
2 answers

error: a SafeHandle or CriticalHandle of type ZLibStreamHandle failed to properly release

I haven't worked much with streams, so I assume there's a coding error here on my part, somewhere. public static SqlBytes Compress(SqlBytes input) { byte[] data = (byte[])input.Value; using (MemoryStream memstream = new…
Tim
  • 8,669
  • 31
  • 105
  • 183
3
votes
2 answers

I have trouble understanding a GZipStream ASP.Net Core Middleware

I consider myself pretty good in C#, but I am facing trouble in understanding the following piece of code: using (var memoryStream = new MemoryStream()) { var responseStream = httpContext.Response.Body; httpContext.Response.Body =…
CodeAddict
  • 43
  • 5