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
5
votes
2 answers

decompress a .gz file using GZipStream

I have several .gz files, and I want to decompress them one by one. I have writen a simple code using GzipStream in C#, but got failed. I wonder a correct and useful method to achieve what I want. Thanks a lot. private string Extrgz(string…
Jun HU
  • 3,176
  • 6
  • 18
  • 21
5
votes
2 answers

C# to Java: Base64String, MemoryStream, GZipStream

I have a Base64 string that's been gzipped in .NET and I would like to convert it back into a string in Java. I'm looking for some Java equivalents to the C# syntax, particularly: Convert.FromBase64String MemoryStream GZipStream Here's the method…
Zero Cool
  • 1,866
  • 4
  • 19
  • 28
5
votes
1 answer

RavenDB client onlinux connecting to windows server using mono http

I've just tried connecting to my RavenDB windows instance from linux using mono. I'm getting a bizarre error with it, that seems to be mono related rather than raven related. Here is my recreate code (works on windows): using System; using…
Chris
  • 1,241
  • 1
  • 14
  • 33
4
votes
1 answer

Add compression to B64 conversion for powershell. The conversion works fine I'm not sure how to approach adding gzip or similar compression

I can convert files to B64 then back again, i would like to compress the data before converting to B64. Gzip doesnt have to be used, any type of compression or size reduction is fine, I think compression is the answer if I can get it somewhere in…
4
votes
3 answers

Decompress a Stream

I'm trying to decompress a stream, using a GZipStream and BinaryStream, but I'm failing. Can you help me? public static LicenseOwnerRoot GetLicenseFromStream(Stream stream) { using (BinaryReader br = new BinaryReader(stream)) …
Guilherme Oliveira
  • 2,008
  • 3
  • 27
  • 44
4
votes
2 answers

HttpResponseMessage - Decompression of Gzip respone from an API Call throws the following exceptiong - The response ended prematurely

I am actually consuming a REST Service from which I receive the response compressed as Gzip so, following is my code HttpClientHandler handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip |…
4
votes
2 answers

GZipStream is cutting off last part of XML

I have created an extension method called AddGZip which looks like the following: public static void AddGZip(this HttpResponse response) { response.Filter = new GZipStream(response.Filter, CompressionMode.Compress); …
CraftyFella
  • 7,520
  • 7
  • 47
  • 61
4
votes
2 answers

Android: Gzip/Http supported by default?

I am using the code shown below to get Data from our server where Gzip is turned on. Does my Code already support Gzip (maybe this is already done by android and not by my java program) or do I have to add/change smth.? How can I check that it's…
OneWorld
  • 17,512
  • 21
  • 86
  • 136
4
votes
1 answer

JsonSerializer failing to write to a GZipStream

I'm trying to serialize very large object directly to a zip stream. I manage to do this by serializing to a file stream in an intermediate step, loading it back and then compressing it. I've also tried compressing directly to a memory stream and it…
pnsm
  • 41
  • 1
  • 2
4
votes
2 answers

GZIP Java vs .NET

Using the following Java code to compress/decompress bytes[] to/from GZIP. First text bytes to gzip bytes: public static byte[] fromByteToGByte(byte[] bytes) { ByteArrayOutputStream baos = null; try { ByteArrayInputStream bais =…
Jim Jones
  • 287
  • 3
  • 14
4
votes
3 answers

compressing with GZIPOutputStream to upload without creating a gzip file locally

I'm trying to compress the file in java before I upload the file with GZIPOutputStream. Is there a way to just store the gzipped file in memory for the upload and not have it generate a gzipped file on the local computer? Thanks!
4
votes
3 answers

Extracted file changes Date modified

When I tried to extract my file using winrar, It retains the date modified of the file inside a .gz. But when I extracted it using a code that is working (I got from other blogs): public static void Decompress(FileInfo fileToDecompress) { …
Kuriyama Mirai
  • 867
  • 5
  • 17
  • 37
4
votes
3 answers

zipping with c#

i am trying to use GZipStream to create a gz file using c#. my problem is that i have a list that contains strings. and i need to create a password protected zip file, and put in it a text file containing the strings. i don't want to create the…
scatman
  • 14,109
  • 22
  • 70
  • 93
4
votes
1 answer

Uploading GzipStream to SFTP using SSH.Net

I am trying to upload a compressed GZipStream to sftp server using ssh.net library. The problem is that when I create the GZipStream, it can not be read any more. Below is my code: using (SftpClient client = new SftpClient(connectionInfo)) { …
Rob Schneider
  • 679
  • 4
  • 13
  • 27
4
votes
1 answer

How to handle Compressed Request in WCF REST service

We have a WCF REST service hosted on IIS 7 with .NET Framework 4.5. The client is sending data in GZip compressed format with request headers: Content-Encoding:gzip Content-Type: application/xml But we are getting bad request from the server, if…
neo
  • 6,131
  • 6
  • 22
  • 27