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

effective approach to writing an compressed XML document and tracing it's current size

My application produces a compressed (gzip) xml messages to transfer over https to a webserver. It receives up to 10,000 data packets/sec from client apps through TCP/IP. Size of each packet could be from 500 to 2000 bytes. It should add a received…
0
votes
1 answer

write compressed xml data to a memory stream using gzipstream

I am trying to write a compressed xml data to a memory stream. Here is my test console app code: var ms = new MemoryStream(); var gstream = new GZipStream(ms, CompressionMode.Compress); var settings = new XmlWriterSettings(); settings.Indent =…
0
votes
1 answer

Send image byte changes via TCP socket

I have and application that uses TCP Sockets to send data over to a second app, in this case images in form of byte[], I'm already compressing the bytes, but I'm wondering if there is a way to cache the bytes and compare the new bytes changes, and…
AJ152
  • 671
  • 2
  • 12
  • 28
0
votes
2 answers

GZipStream in HttpHandler: What am I doing wrong?

I'm writing a HttpHandler, which sends zipped file to the client on GET requests. This code works well and sends unzipped data using (var mem = new MemoryStream()) { WriteMyDataToStream(mem); context.Response.AddHeader("Content-Type",…
skaeff
  • 753
  • 2
  • 13
  • 25
0
votes
1 answer

How to serialize object to xml and compress in memory

In my application I need serialize large object to xml string. But then serialized throw System.OutOfMemory exception. How can I serialized object without exception and with compression? public static string GenerateXMLData(T data) { …
zrabzdn
  • 995
  • 2
  • 14
  • 33
0
votes
2 answers

'GZipStream' could not be found - reference issue

I can't get to use GZipStream class in my C# ASP.NET 4.5 application. I get the error: The type or namespace name 'GZipStream' could not be found (are you missing a using directive or an assembly reference?) I tried using using System.IO; but the…
Liron Harel
  • 10,819
  • 26
  • 118
  • 217
0
votes
1 answer

Errow while uploading gzip via http POST

So trying I'm POSTing a compressed file via httplib2 in Python 3.2. I get the following error: io.UnsupportedOperation: fileno I used to post just an xml file but since those files are getting too big I want to compress them inside the memory…
pypat
  • 1,096
  • 1
  • 9
  • 19
0
votes
2 answers

Why does reordering gzip packets damages the output?

I'm using the idea of the gzip code posted in zlib. For initialization I use deflateInit2(p_strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (15+16), 8, Z_DEFAULT_STRATEGY). I'm zipping a stream. Each packet with Z_FULL_FLUSH, except from the last which I…
hudac
  • 2,584
  • 6
  • 34
  • 57
0
votes
1 answer

How to specify xml file extension while serializing Xml to a GZipStream

I am serializing an object with the following code, which uses GZip and Xml: FileStream fs = new FileStream(destinationfolder + "/myFileName.gz", FileMode.Create, FileAccess.Write); using (var…
heltonbiker
  • 26,657
  • 28
  • 137
  • 252
0
votes
1 answer

How to correctly read zipped data from NetworkStream with BeginRead/EndRead?

Here is the AsyncCallback that I'm using to read data from GZipStream created on top of NetworkStream: void ReadCompressedDataCallback(IAsyncResult ar) { var state = ar.AsyncState as ReadCompressedDataState; try { …
Arsen Zahray
  • 24,367
  • 48
  • 131
  • 224
0
votes
1 answer

Timestamp of compressed file in c# GzipStream

I am using the gzipstream to compress my file in c#. While the gz file has the proper timestamp, the actual file shows the UTC time stamp when decompressed. Is there a way to get the current system timezone and set it on the file to be compressed…
wishy
  • 1,748
  • 2
  • 12
  • 14
0
votes
1 answer

Unzip a mail attachment

The following line gives problems content = new StreamReader(new GZipStream(new MemoryStream(a.RawBytes), CompressionMode.Decompress)).ReadToEnd(); InvalidDataException occurred: The magic number in GZip header is not correct. Make sure you are…
Kasper Hansen
  • 6,307
  • 21
  • 70
  • 106
0
votes
2 answers

Unzipping ZLIB compressed portions of a binary file

I'm reading a file(a flash swf) from .Net 3.5 that has a header which states whether the body of the file/Stream is compressed or not. However, I'm having problems-after I rewrap the basic File stream in a GZipStream, I get an exception stating…
Michael Kohout
  • 1,073
  • 1
  • 14
  • 26
0
votes
3 answers

GZipStream corrupts file if i pack it in parts (Flush doesn't work)

I've tried to use this code to compress a file in parts using (var fsIn = new FileStream("test.avi", FileMode.Open)) { using (var fsOut = new FileStream("test.avi.gz", FileMode.Create)) { var buf = new byte[1024 * 1024]; …
0
votes
3 answers

compressing pdf file makes it bigger

I compress the uploaded .pdf files and save them to server's file system. Everything works well, but the file gets bigger, from 30kb to 48kb. What could I be doing wrong? Here's the code part I compress the uploaded file: FileStream sourceFile =…
İsmet Alkan
  • 5,361
  • 3
  • 41
  • 64