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

Decompressing a base 64 encoded GZip with Powershell

I have the following python code which works: import sys import base64 import gzip from io import BytesIO def deserialize(encoded_string): # decode and decompress the base64 string compressed_json = base64.b64decode(encoded_string) …
Eitan Blumin
  • 113
  • 1
  • 8
0
votes
0 answers

Download and Decompress zip file error: The magic number in GZip header is not correct. Make sure you are passing in a GZip stream

I'm connecting to a REST API that returns a zip file with a small size (10k) with an XML file inside. When I copy it to a MemoryString and then Decompress it, I get the following error: System.IO.InvalidDataException: 'The magic number in GZip…
0
votes
0 answers

Powershell - gzip large file and load to s3 using stream

I'm trying to compress some csv files using gzip and then upload them to S3. I need to use streams to compress and load because the files could be very large and I don't want to write the file back to disk before loading it to s3. I'm new to using…
JeffR
  • 1,782
  • 1
  • 15
  • 19
0
votes
1 answer

GZipStream exception in Server side when decompressing JSZip compressed file

I am working on ASP.net core Web API in server side and Angular 8 on Client side. I need to compress a list of files and send to server and decompress in server. I use JSZip in client side to compress and my client side code…
0
votes
1 answer

How do you store bytes to a var from a loop so that web response can be decompressed?

I have been building my own HTTP/1.0 proxy server using HTTPWebRequest/HTTPWebResponse. Part of the original code streamed the response from the remote server directly to the client like so: if (response != null) { List>…
James
  • 656
  • 2
  • 10
  • 24
0
votes
1 answer

Not able to Decompress the byte array of new secure QR code of Aadhar

I am trying to decompress the byte array using GZipStream but getting error of "Found invalid data while decoding." // getting aadhaar sample qr code data from // https://uidai.gov.in/images/resource/User_manulal_QR_Code_15032019.pdf static void…
Paresh
  • 1
  • 3
0
votes
1 answer

Powershell GzipStream, "Cannot find an overload for "GZipStream" and the argument count: "2"

Trying to get GzipStream going in Posh. Have this: [System.IO.MemoryStream] $output = New-Object System.IO.MemoryStream $gzipStream = New-Object System.IO.Compression.GzipStream $output, ([IO.Compression.CompressionMode]::Optimal) Getting error on…
user1443098
  • 6,487
  • 5
  • 38
  • 67
0
votes
1 answer

How can I decompress a stream in c# like this java snippet code?

I'm trying to convert this java snippet code in c# but I'm a bit confused about it. This is the java code: My try is the following, but there are some errors in gis.Read, because it wants a char* and not a byte[] and in the String constructor for…
Giuseppe Pennisi
  • 396
  • 3
  • 22
0
votes
2 answers

How can i read the data using the GZipStream.Read method

I can't read the data using the GZipStream.Read method. But I can read directly from MemoryStream. What am I doing wrong? public static void Main(string[] args) { var memStr = new MemoryStream(); //Write var data =…
all zoom
  • 13
  • 2
0
votes
2 answers

GZipStream not working

I am using the following C# code to compress a file: // Open the stream we want to compress FileStream fs = File.Create(@"C:\Projects\Samples\test\compressed.zip", 0); // Creates the GZipStream GZipStream gzip = new GZipStream(fs,…
0
votes
0 answers

GZipStream -- the decompressed file is missing data

I need to get a file from my server via FTP into a memory stream and then decompress it so I can further work with it. I do the below but the decompressed file is truncated every time. I can see that the FTP part is working correctly (I checked that…
Matt M.
  • 812
  • 3
  • 16
  • 24
0
votes
1 answer

Reading from wrapped GZipStream

I am trying to read compressed gzip data like this: using (var input = new BinaryReader(new GZipStream(fileStream, CompressionMode.Decompress))) { while(input.PeekChar() != -1) { //use BinaryReader methods I need } } But…
Okumo
  • 169
  • 1
  • 12
0
votes
1 answer

XmlSerialize directly to GZipStream throws magic number exception on Decompression

I am trying to Serialize an object to XML however my object is a generic list containing many records and causes the serializer to consume lots of memory. So I tried to serialize directly to a GZipStream with the following code: Dim formatter As…
Seph
  • 8,472
  • 10
  • 63
  • 94
0
votes
1 answer

Using GZIP compression stream in android

I want to compress data in android devices and send it to an ASP.net server-side, but I don't want to consume much power and sacrifice device resources. I've tried compressing data using Deflate stream on android-side and decompressing using the…
A.J
  • 333
  • 1
  • 2
  • 15
0
votes
0 answers

gz-file generated programatically in C# differs from on generated manually

I'm doing an analysis of a filevault belonging to an application which gz-compress and adds a custom header to every file uploaded to it. The header is ASCII-text consisting of header start element, containing a sha-1 checksum of the compressed…
bjelleklang
  • 476
  • 3
  • 8
  • 25